Search code examples
pythonsubprocesspycharmvirtualenvpopen

Pycharm subprocess.Popen python under virtualenv


Calling subprocess.Popen(["python"]) spawns a process as if calling the global 2.7 python, instead of spawning a 3.6 version as defined in the virtualenv. subprocess.Popen documentation says that execvp is called, but it seems to act as if a new clean shell is calling it. Is there a way to enforce calling system calls within the code but under the virtualenv?

Running on macOS, project in a virtualenv directory which is recognized by Pycharm (Preferences->Project->Project Interpreter points to the python in the virtualenv). My global python command launches the 2.7 interpreter, but in the virtualenv python will launch the 3.6 interpreter.


Solution

  • subprocess.Popen([sys.executable])

    sys.executable is the Python interpreter running the current script. Using it to run a new script with the same interpreter is pretty natural.