user1345260
user1345260

Reputation: 2259

Python Script unable to import installed modules when called as a subprocess

I've two Python scripts as given below

inner.py

#!/usr/bin/python  
import os  
import datetime  
# <---- Some Code--->  

main.py

#!/usr/bin/python  
import os  
import datetime  

# <---- Some Code--->  
subprocess.call(["/usr/bin/python",inner.py])  

The problem is when the inner.py script is called from the main.py script it doesn't import any modules. For example it says

ImportError: No module named os

But when the script is executed standalone it works fine. Please help

Upvotes: 1

Views: 985

Answers (1)

hfaran
hfaran

Reputation: 544

The following works perfectly fine for me, and it's modified because some of your code seemed a little incomplete.

inner.py

#!/usr/bin/python
import os
import datetime

print os.getcwd()

main.py

#!/usr/bin/python
import os
import datetime
import subprocess
import sys

# <---- Some Code--->
subprocess.call([sys.executable, "inner.py"])

Upvotes: 1

Related Questions