简体   繁体   English

从 C++/Qt 程序运行 Windows 中的 Anaconda 脚本

[英]Run Anaconda script in Windows from C++/Qt program

Okay, weird thing.好吧,奇怪的事情。 This works perfectly fine in Linux, but I cannot get it to work in Windows. Problem might be that I'm not working in Windows a lot these days...这在 Linux 中工作得很好,但我无法在 Windows 中工作。问题可能是我这些天在 Windows 中工作不多......

I've got a Python script that I want to run from a C++/Qt program.我有一个 Python 脚本,我想从 C++/Qt 程序运行它。 So I installed Anaconda and within the "Anaconda Prompt" I installed the package containing the script via pip. I've got the full path to the Python script (defined as an entry_point in the packages's setup.py, so it's actually an exe file) and try to start it like this:所以我安装了 Anaconda 并在“Anaconda Prompt”中安装了包含脚本的 package 通过 pip。我有 Python 脚本的完整路径(在包的 setup.py 中定义为入口点,所以它实际上是一个 exe 文件) 并尝试像这样启动它:

proc = new QProcess(this);
connect(proc, &QProcess::readyReadStandardError, this, &MainWindow::receivedText);
connect(proc, &QProcess::readyReadStandardOutput, this, &MainWindow::receivedText);
proc->start(python_script, arguments);

Where python_script and arguments are the full path to the python script and its arguments. When I run this from the "Anaconda Prompt", it works fine, but from my Qt program I get a "NumPy not installed" error, although it's definitely installed.其中 python_script 和 arguments 是 python 脚本及其 arguments 的完整路径。当我从“Anaconda Prompt”运行它时,它工作正常,但是从我的 Qt 程序中我得到一个“NumPy 未安装”错误,尽管它确实已安装. Could it be that I have to run "conda activate" somehow first?难道我必须先以某种方式运行“conda activate”吗? Sorry, I'm not really familiar with Anaconda, only using it in Windows... How would I do that?抱歉,我对 Anaconda 不是很熟悉,只在 Windows 中使用它......我该怎么做? Thanks!谢谢!

can you open a dos prompt shell from your code?你能从你的代码中打开一个 dos 提示符 shell 吗? If so, you can get anaconda to start within that shell and then run your conda activate commands, etc.如果是这样,您可以获得 anaconda 以在该 shell 中启动,然后运行您的 conda activate 命令等。

Try this command to see if this is an option, you may need to change the paths from miniconda3 depending on what anaconda app you are using:试试这个命令看看这是否是一个选项,您可能需要根据您使用的 anaconda 应用程序更改 miniconda3 的路径:

%windir%\System32\cmd.exe "/K" C:\Users\user\miniconda3\Scripts\activate.bat C:\Users\user\miniconda3

Found a method that works: instead of calling the script directly as "script.exe arguments", I can run it through the Python interpreter as "python.exe script.exe arguments", which seems to work nicely.找到了一种有效的方法:我可以通过 Python 解释器将其作为“python.exe script.exe 参数”运行,而不是直接将脚本作为“script.exe 参数”调用,这似乎工作得很好。

I keep having the same issue when I switch servers, so posting a solution which always works for me:我在切换服务器时一直遇到同样的问题,因此发布一个对我来说总是有效的解决方案:

conda install -c conda-forge implicit conda install -c conda-forge 隐式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM