Reputation: 140
I am trying to use a python subprocess to execute a script, which interests me to be able to do an import of my project. When running in another process, I only have the typical modules, and not those of my project when doing an import. How can I get my modules imported?
Example:
first_script.py
import subprocess
from my_project.any_module import any_module
def __init__(self):
subprocess.call(['python', 'path/to/exec/second_script.py'])
second_script.py
from my_project.any_module import any_module
def __init__(self):
print any_module.argument
In the first script, import any_module works, in the second it does not.
Any ideas? Thx.
Upvotes: 4
Views: 8871
Reputation: 146
Another potential cause of this is if you call python script.py
. It could default to python2
, instead of the usual python3
, which would have different dependencies.
Make sure you specify the python version when using subprocess.call(['python3', 'script.py'])
Upvotes: 0
Reputation: 10245
The my_project
module needs to be in your PYTHONPATH so Python can find it. PYTHONPATH includes your current working directory, which is why it works in your first script when you run it. But when you invoke a subprocess, the cwd is different. So you need to add the path to my_project
to PYTHONPATH and specify PYTHONPATH explicitly with the env
argument to subprocess.call()
.
However, running Python code this way is awkward. Unless you have specific requirements that prevent this, I would suggest using the multiprocessing package instead to run Python code in a separate process.
Upvotes: 3