Command works in Terminal, but not in subprocess.run()

Hi, I’ve been working on this bug for over 5 hours and I’m not sure how to solve it. I have a Python script which I use to setup an Anaconda environment and one of the libraries in that environment which needs to be setup, has to be compiled.

In the Terminal, there is no problem. g++11 is needed to compile so I simply run conda install -y gxx_linux-64=11.2.0 and then the compilation script python compile_library.py. The compile completes successfully and everything is setup.

With subprocess.run however, the install completes, but the subsequent compilation script says it is unable to compile because it can’t find g++11.

Here is my call to subprocess.run:

subprocess.run("conda install -y gxx_linux-64=11.2.0; python compile_library.py", shell=True)

I figured it out. Even though I was able to use conda, I was never in a conda environment. Conda does not support subshells so every time a new subshell is created, the conda environment has to be activated in order to be in the environment. Finally!

This is how it looks now:

subprocess.run("conda install -y gxx_linux-64=11.2.0", shell=True)
subprocess.run(". <conda path>/etc/profile.d/conda.sh && conda activate <conda env> && python compile_library.py", shell=True)