1

I have two directories at the same level. For example Folder A and Folder B. Folder A uses python3.7 and Folder B has an Anaconda environment which uses python2.7. I run os.system("python ../folder b/ex2.py").

Drawing of hierarchy explained above.

In folder B the Anaconda environment is activated.

The problem is when I run the python script from Terminal python ../folder b/ex2.py it runs successfully but when I call the script from folder A it doesn't use the Anaconda environment.

grg
  • 5,023
  • 3
  • 34
  • 50
Cloud
  • 15
  • 2
  • 6

2 Answers2

2

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.

cosmic_inquiry
  • 2,557
  • 11
  • 23
  • 1
    This may be correct answer. However, in my environment the conda activte doesn't work properly and the following error occured. ```CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell``` Even I run conda init , the error doesn't disappear. This may be I can't fully configure the conda and other ubuntu. But I can run the other file via the following command. ```os.system('conda run -n python ')``` – Cloud Apr 15 '19 at 05:39
0

run the other file via the following command.

os.system('conda run -n <env_name> python <path_to_other_script>')
hong xu
  • 81
  • 1
  • 1