Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python subprocess doesn't inherit virtual environment

When operating with a venv on Windoes 10 if I 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>
like image 745
WesR Avatar asked Dec 30 '25 17:12

WesR


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.

like image 134
Anthony Sottile Avatar answered Jan 02 '26 06:01

Anthony Sottile



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!