I apologize if what I'm trying to achieve is not Pythonic - I recently moved to the language.
I have a project directory structured like so:
root
--proj1
----venv
----main.py
--proj2
----venv
----main.py
Both proj1 and proj2 run under their own virtual environments. I am trying to call proj2/main.py from proj1/main.py, whilst executing proj2/main.py under its own venv. I have tried:
import subprocess
s2_out = subprocess.check_output([sys.executable, r"..\proj2\__main__.py", "arg"])
This invokes successfully, but I am getting all manner of not found exceptions, etc. I am guessing this is the reason why.
Please let me know if there is a better approach!
You can do this:
import subprocess
subprocess.call(["python_interpreter location (python.exe)", "python file"])
So you could do:
import subprocess
subprocess.call(["../proj2/bin/python.exe", "proj2/main.py"])
For Mac OS and Linux, the python
interpreter path for a venv would be folder/bin/python.exe
, or in your case ../proj2/bin/python.exe
.
For Windows, the python
interpreter path for a venv would be folder/scripts/python.exe
.
You may need to include the full paths.
Another way to do this could be using subprocess.call
, if you need the output:
import subprocess
output = subprocess.call("%s %s" %("../proj2/bin/python.exe", "proj2/main.py"))
print(output)
Both ways will work just fine :)