[Tutor] problem with __import__()

shaleh at speakeasy.net shaleh at speakeasy.net
Wed Jul 26 01:36:29 CEST 2006


I am attempting to use __import__() to read a Python file. It is not working. 

What is particularly odd is that it works from the interactive prompt. Looking at sys.path I can see that in the interactive session path starts with an empty entry. Adding a similar entry to my script causes the import to work. Not sure why. Any clues?

Here the code:
import sys

try:
    f = __import__("/tmp/my/settings")
    print f.__name__
except:
    print "1. Failed"

sys.path.insert(0, '')

try:
    f = __import__("/tmp/my/settings")
    print f.__name__
except:
    print "2. Failed"





More information about the Tutor mailing list