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:
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>
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.
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