在给定的conda环境中执行python脚本的最佳方法 [英] Best way to execute a python script in a given conda environment

查看:3357
本文介绍了在给定的conda环境中执行python脚本的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用子进程包从另一个python脚本trigger.py执行一个python脚本executed.py. executed.py必须在与trigger.py不同的conda环境中运行(假设 executed_env trigger_env ).最好的方法是什么?我当前的trigger.py代码是:

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)

由于在 trigger_env 环境中运行了executed.py,因此返回错误.

It returns an error since executed.py is ran in the trigger_env environment.

推荐答案

我对任何conda内置方法进行了研究,发现了以下内容.第一个是解决方法,第二个是最终解决方案内置解决方案(在撰写本文时不可用).

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).

  1. conda包装器. Guilherme Melo在conda环境中为python可执行文件创建了包装.如果您在IDE中将其设置为python解释器,例如PyCharm,它将激活从其调用的conda环境,然后调用python解释器. 在创建conda包装器"部分中查看以下内容: https://pypi.org/project/exec-wrappers/

  1. 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运行. 在在环境内执行命令的一种标准且快速的方法上,在conda github页面上进行了长时间的讨论导致了新命令的实现(实际上是之前的重新调用): conda run

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

它在 issue#7320 中进行了描述,并将在 conda-4.6 希望在2018年10月!

It is described in issue #7320 and will be released in conda-4.6 hopefully in October 2018!

这篇关于在给定的conda环境中执行python脚本的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆