在Anaconda环境中使用子流程 [英] Using subprocess in anaconda environment

查看:103
本文介绍了在Anaconda环境中使用子流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python 3.6.6 :: Anaconda,Inc.

I use Python 3.6.6 :: Anaconda, Inc.

我尝试使用子进程来调用其他python脚本.

I try to use subprocess to call other python script.

subprocess.run("python -V", shell=True)

我尝试了这段代码,但结果是

I tried this code but outcome is

Python 2.7.12

我的本​​地python被捕获

My local python get caught

我也尝试过

subprocess.run("bash -c 'source /home/hclee/anaconda3/envs/calamari/lib/python3.6/venv/scripts/common/activate
/home/hclee/anaconda3/envs/calamari && python -V && source deactivate'", shell=True)

但是我得到了相同的结果

but I got the same result

推荐答案

在运行代码之前,在Linux中运行source activate root或在Windows中运行activate root激活环境.

Run source activate root in linux, or activate root in Windows to activate the environment before running your code.

如果这对您没有帮助,请尝试进行快速而肮脏的修复,例如:

If this does not help you, try for a quick and dirty fix e.g.:

subprocess.run('bash -c "source activate root; python -V"', shell=True)

您需要调用bash的原因是python的运行不会调用bash环境,而是另一个受限制更大且不包含source的环境,因此在这里我们需要调用bash ...但是如上所述,如果您需要此命令,则说明您正在做一些特殊的事情,或者您的环境有问题...

The reason you need to call bash is that python's run will not call the bash environment, but another which is a bit more constrained and does not contain source, so here we need to call bash... But as mentioned, if you need this command, either you are doing something special, or something is wrong with your environment...

不需要停用,它不会执行任何操作,因为运行它的外壳会被破坏...

deactivate is not needed, it does nothing cause the shell it was run on will be destroyed...

这篇关于在Anaconda环境中使用子流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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