1

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

5
  • That should all work just fine; you can also use sys.executable instead of /usr/bin/python to make sure you are using the exact same Python binary. Commented Aug 11, 2013 at 22:05
  • What does import sys do? If that works, what does print sys.path give you? Commented Aug 11, 2013 at 22:05
  • import sys works and the print sys.path gives the following result ['/home/transfer/scripts/common', '/usr/bin/python/lib64/python26.zip', '/usr/bin/python/lib64/python2.6/', '/usr/bin/python/lib64/python2.6/plat-linux2', '/usr/bin/python/lib64/python2.6/lib-tk', '/usr/bin/python/lib64/python2.6/lib-old', '/usr/bin/python/lib64/python2.6/lib-dynload'] Commented Aug 11, 2013 at 22:08
  • Your import exception makes no sense. Are you 100% certain that os fails to import? Does import subprocess work? Does import urllib work? Commented Aug 11, 2013 at 22:11
  • Yes, all the modules I've mentioned in the inner.py fails except import sys. So far I've import os, import datetime in the inner.py. As per your suggestion I just added import subprocess and even that failed Commented Aug 11, 2013 at 22:16

1 Answer 1

1

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"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.