如何调用anaconda环境以通过os.system()从其他python(3.7)脚本运行特定的python(2.7)包? [英] How to call anaconda environment to run specific package of python(2.7) from other python(3.7) script via os.system()?
问题描述
我有两个相同级别的目录.例如-文件夹A和文件夹B.文件夹A使用python3.7,文件夹B具有anaconda环境,该环境使用python2.7.我运行os.system("python ../folder b/ex2.py").
I have two directory of same level. For example - Folder A and Folder B. Folder A use python3.7 and Folder B have anaconda environment which use python2.7. I run os.system("python ../folder b/ex2.py").
这里是图像的可视化. 在此处输入图片描述
Here is visualize image. enter image description here
在文件夹B中,anaconda环境已激活.
In folder B the anaconda environment is activated.
问题是当我从终端运行时,python脚本(python ../folder b/ex2.py)成功运行,但是当我从文件夹A脚本调用该脚本时,它不使用anaconda环境.
The problem is when I run from terminal , the python script(python ../folder b/ex2.py) run successfully but when I call the script from folder A script, it doesn't use anaconda environment.
推荐答案
假定您的conda env名称为python2.7
.更改
Assuming your conda env is name python2.7
. Change
os.system('python ../folderb/ex2.py')
到
os.system('conda activate python2.7 && python ../folderb/ex2.py')
这应该在conda环境中执行ex2.py.
This should execute your ex2.py within the conda env.
这篇关于如何调用anaconda环境以通过os.system()从其他python(3.7)脚本运行特定的python(2.7)包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!