天天看點

python調用os.system啟動anaconda環境,如何調用anaconda環境以通過os.system()從其他python(3.7)腳本運作特定的python(2.7)包?...

python調用os.system啟動anaconda環境,如何調用anaconda環境以通過os.system()從其他python(3.7)腳本運作特定的python(2.7)包?...

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

In folder B the anaconda environment is activated.

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.

解決方案

Assuming your conda env is name python2.7. Change

os.system('python ../folderb/ex2.py')

to

os.system('conda activate python2.7 && python ../folderb/ex2.py')

This should execute your ex2.py within the conda env.