python subprocess doesn't inherit virtual environment

Viewed 558

When operating with a venv on W10 if invoke a subprocess from a file in the directory, the subprocess does not seem to have access to the venv. Is there a way to make it work?

Ideally I would like the approach to be portable to Linux but I'll take whatever gets the project running.

Here is my test:

  • main.py uses Popen to invoke sub_proc.py.
  • sub_proc.py imports uuid_shortener, which has been installed in the virtual environment.

If I run sub_proc.py directly it runs without an error.

However, if I run main.py I see an error on the import statement for uuid_shortener.

main.py

import subprocess
import time

print(subprocess.Popen(['python', 'sub_proc.py']))
time.sleep(1)

sub_proc.py

import uuid_shortener

Here is the output from running the code.

(venv) PS C:\Users\...\popenvenv> python .\sub_proc.py

(no error above)

(venv) PS C:\Users\...\popenvenv> python .\main.py
<Popen: returncode: None args: ['python', 'sub_proc.py']>
Traceback (most recent call last):
  File "C:\Users\...\popenvenv\sub_proc.py", line 1, in <module>
    import uuid_shortener
ModuleNotFoundError: No module named 'uuid_shortener'
(venv) PS C:\Users\...\popenvenv>
1 Answers

use sys.executable in place of 'python'. sys.executable refers to the executable you're running with

this will preserve access to the virtualenv in subprocesses

Related