[英]How to launch a python script in a virtual environment from a C++ program on Windows?
So far I am using a 2-step approach and launch cmd.exe
rather than python.exe
.到目前为止,我使用的是两步方法并启动
cmd.exe
而不是python.exe
。 But I don't like cmd.exe
in between the process tree for several reasons (eg I don't receive the python process handle or ID, but rather the one of cmd.exe
).但我不喜欢
cmd.exe
在进程树之间有几个原因(例如,我没有收到 python 进程句柄或 ID,而是cmd.exe
之一)。
string sCmd =
"cmd.exe /C venv\\SomeEnv\\Scripts\\activate.bat && python.exe Subprograms\\my_script.py";
CreateProcessA(nullptr,
sCmd.data(),
...);
I believe that to activate the virtual environment I need to put it at the beginning of the PATH
environment variable and set some other variables like PYTHONHOME
and PYTHONPATH
, but I'm not sure whether this is sufficient and what values to set there on Windows.我相信要激活虚拟环境,我需要将它放在
PATH
环境变量的开头并设置一些其他变量,如PYTHONHOME
和PYTHONPATH
,但我不确定这是否足够以及在 Windows 上设置什么值。
If you want to do it using standard C++, you can go for std::system()
std::system("command_to_run_with_space_separated_params_if_any");
If you want to use windows ways, you can go for followings...
ShellExecuteEx()
ShellExecute()
CreateProcessA()
CreateProcessW()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.