I use Python 3.6.6 :: Anaconda, Inc.
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
My local python get caught
I also tried
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
Subprocess in Python is a module used to run new codes and applications by creating new processes. It lets you start new applications right from the Python program you are currently writing. So, if you want to run external programs from a git repository or codes from C or C++ programs, you can use subprocess in Python.
The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.
The subprocess module present in Python(both 2. x and 3. x) is used to run new applications or programs through Python code by creating new processes. It also helps to obtain the input/output/error pipes as well as the exit codes of various commands.
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)
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...
Note: For newest conda versions, the provided code will work, but there are also these options that work similarly:
conda deactivate
:
subprocess.run('bash -c "conda deactivate; python -V"', shell=True)
conda activate root
or base
:
subprocess.run('bash -c "conda activate root; python -V"', shell=True)
subprocess.run('bash -c "conda activate base; python -V"', shell=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With