在给定 conda 环境中执行 python 脚本的最佳方法

发布于 2025-01-16 04:18:36 字数 549 浏览 4 评论 0原文

我想使用 subprocess 包从另一个 python 脚本 trigger.py 执行 python 脚本 executed.pyexecuted.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖树树初阳… 2025-01-23 04:18:37

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

  1. conda 包装器
    Guilherme Melo 在 conda 环境中为 python 可执行文件创建了一个包装器。如果您在 IDE(例如 PyCharm)中将其设置为 python 解释器,它将激活调用它的 conda 环境,然后调用 python 解释器。
    请参阅“创建 conda 包装器”部分:
    https://pypi.org/project/exec-wrappers/

  2. 康达运行。
    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).

  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/

  2. 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!

木落 2025-01-23 04:18:37

如果您只需要使用其他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

  • 创建 py27 环境 conda create -n py27 python=2.7 这是 trigger_env
  • 创建 py35 环境 conda create -n py35 python=3.5 这是 executed_env
  • 通过运行 source activate py35which python 获取 py35 的完整 python 路径(在本说明中,我们将其称为 EXECUTED_PYTHON)。通过source deactivate停用。
  • 然后我们创建executed.py

    导入系统
    打印(系统版本)
    
  • 然后创建 trigger.py (包含参数,但它们什么也没做)

    command =“EXECUTED_PYTHON执行.py --option1 -dir /path/to/dir”
    args = shlex.split(命令)
    my_subprocess = subprocess.Popen(args)
    
  • 现在让我们在 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 do which python. Let's assume that returns HOME/.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". This

For example, let's run executed.py in a different python version.

  • Create your py27 environment conda create -n py27 python=2.7 this is the trigger_env.
  • Create your py35 environment conda create -n py35 python=3.5 this is the executed_env.
  • Get the full python path to py35 by running source activate py35 and then which python (let's call that EXECUTED_PYTHON for this description). Deactivate via source deactivate.
  • Then we create executed.py

    import sys
    print(sys.version)
    
  • Then the trigger.py (included the arguments but they are doing nothing)

    command = "EXECUTED_PYTHON executed.py --option1 -dir /path/to/dir"
    args = shlex.split(command)
    my_subprocess = subprocess.Popen(args)
    
  • Now let's run it in the trigger_env -- source activate py27 and python trigger.py.

  • This prints 3.5.2 |Continuum Analytics, Inc. [...] (3.5 being the important part). You can see it is using the other python in the executed_env.
一袭水袖舞倾城 2025-01-23 04:18:37

在 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

C:\ProgramData\Anaconda3\python.exe C:\ProgramData\Anaconda3\cwp.py C:\Users\ismael.serrano\.conda\envs\AutomateXXReporting C:\Users\ismael.serrano\.conda\envs\AutomateXXReporting\python.exe 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

C:\ProgramData\Anaconda3\python.exe C:\ProgramData\Anaconda3\cwp.py C:\Users\ismael.serrano\.conda\envs\AutomateXXReporting C:\Users\ismael.serrano\.conda\envs\AutomateXXReporting\python.exe 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文