在给定 conda 环境中执行 python 脚本的最佳方法
我想使用 subprocess 包从另一个 python 脚本 trigger.py
执行 python 脚本 executed.py
。 executed.py
必须在与 trigger.py
不同的 conda 环境中运行(假设 execulated_env
和 <强>trigger_env
)。最好的方法是什么?我当前的 trigger.py
代码是:
command = "python executed.py --option1 -dir /path/to/dir"
args = shlex.split(command)
my_subprocess = subprocess.Popen(args)
它返回一个错误,因为 executed.py
是在 trigger_env
中运行的环境。
I want to execute a python script executed.py
from another python script trigger.py
using the subprocess package. The executed.py
must be ran in a different conda environment than trigger.py
(let say executed_env
and trigger_env
). What is the best way to do that ? My current code of trigger.py
is:
command = "python executed.py --option1 -dir /path/to/dir"
args = shlex.split(command)
my_subprocess = subprocess.Popen(args)
It returns an error since executed.py
is ran in the trigger_env
environment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对 conda 内置方法进行了一些研究,发现了以下内容。第一个是解决方法,第二个是最终解决方案的内置解决方案(在撰写本文时不可用)。
conda 包装器。
Guilherme Melo 在 conda 环境中为 python 可执行文件创建了一个包装器。如果您在 IDE(例如 PyCharm)中将其设置为 python 解释器,它将激活调用它的 conda 环境,然后调用 python 解释器。
请参阅“创建 conda 包装器”部分:
https://pypi.org/project/exec-wrappers/
康达运行。
conda github 页面上关于在环境中执行命令的标准且快速的方法 导致执行一个新命令(实际上是重新调用之前可用的命令):
conda 运行
它在 issue #7320 中进行了描述,并将在希望在 2018 年 10 月在 conda-4.6 中发布!
I did some research on any conda built-in methods and found the following. The first is a workaround, and the second the final solution built-in solution (not available at time of writing this post).
conda-wrappers.
Guilherme Melo created a wrappers for the python executables within a conda environment. If you set it as the python interpreter in your IDE, e.g. PyCharm, it will activate the conda environment from which it is called, and then call the python interpreter.
Look here under section "Creating conda wrappers":
https://pypi.org/project/exec-wrappers/
conda run.
A long discussion on the conda github page on a standard and fast way to execute a command inside an environment led to the implementation of a new command (actually a re-invocation as it was available before):
conda run
It is described in issue #7320 and will be released in conda-4.6 hopefully in October 2018!
如果您只需要使用其他Python,那么我相信您只需要在
命令
中使用其他Python的完整路径。尝试进入您的
executed_env
(即source activateexeced_env
如果是 Linux)并执行which python
。假设现在返回 HOME/.conda/envs/execulated_env/bin/python ,它成为您在命令中使用的 python - 即command = "HOME/.conda/envs/ execute_env/bin/pythonexecuted.py --option1 -dir /path/to/dir"
。例如,让我们在不同的 python 版本中运行
executed.py
。conda create -n py27 python=2.7
这是trigger_env
。conda create -n py35 python=3.5
这是executed_env
。source activate py35
和which python
获取 py35 的完整 python 路径(在本说明中,我们将其称为 EXECUTED_PYTHON)。通过source deactivate
停用。然后我们创建
executed.py
然后创建
trigger.py
(包含参数,但它们什么也没做)现在让我们在
trigger_env
中运行它 -- < code>source activate py27 和python trigger.py
。3.5.2 |Continuum Analytics, Inc. [...]
(3.5 是重要部分)。您可以看到它正在使用executed_env
中的另一个 python。If you just need to use the other python then I believe you simply need to use the full path to the other python in your
command
.Try going into your
executed_env
(i.e.source activate executed_env
if Linux) and dowhich python
. Let's assume that returnsHOME/.conda/envs/executed_env/bin/python
now that becomes the python that you use in your command -- i.e.command = "HOME/.conda/envs/executed_env/bin/python executed.py --option1 -dir /path/to/dir"
. ThisFor example, let's run
executed.py
in a different python version.conda create -n py27 python=2.7
this is thetrigger_env
.conda create -n py35 python=3.5
this is theexecuted_env
.source activate py35
and thenwhich python
(let's call that EXECUTED_PYTHON for this description). Deactivate viasource deactivate
.Then we create
executed.py
Then the
trigger.py
(included the arguments but they are doing nothing)Now let's run it in the
trigger_env
--source activate py27
andpython trigger.py
.3.5.2 |Continuum Analytics, Inc. [...]
(3.5 being the important part). You can see it is using the other python in theexecuted_env
.在 Windows 环境中使用 conda,我只是复制了 conda 从不同环境启动不同 jupyter 笔记本的方式。所有快捷方式都使用“C:\ProgramData\Anaconda3\python.exe”调用“C:\ProgramData\Anaconda3\cwp.py”脚本,然后使用其他参数可以选择要执行的环境和文件。
就我而言,环境名称 = AutomateXXReporting,要执行的文件 = C:\Users\ismael.serrano\Documents\GIT\xxx\notebooks\exports\get_JIRA_user_streams.py
以前,我已按照 Paul 的指示成功进行,但遇到了以下情况但这还不够。完整路径中的 python 无法找到一些库,使用 cwp.py 它可以工作。
Using conda on windows environment, I just copied the way conda does for starting different jupyter notebooks each from a different environment. All shortcuts call to "C:\ProgramData\Anaconda3\cwp.py" script using "C:\ProgramData\Anaconda3\python.exe", then with additional parameters you can choose the environment and the file to be executed.
In my case, environment name = AutomateXXReporting, file to be executed = C:\Users\ismael.serrano\Documents\GIT\xxx\notebooks\exports\get_JIRA_user_streams.py
Previously I had proceed successfully as Paul indicated, but reached a case where it was not sufficient. The python from the full path was not able to find some libraries, using cwp.py it worked.