why _import__ only works from interactive interpreter?

L

linuxnow

I don't know what I'm doing wrong, but the result is that _import_ only
works from a python shell, but not when I call a python script.

$ cat test.py
mod = __import__("/home/me/tests/test_imp")

Called from a script it does not work:
$ python /home/me/test.py
Traceback (most recent call last):
File "/home/pau/test.py", line 1, in ?
mod = __import__("/home/me/tests/test_imp")
ImportError: No module named /home/me/tests/test_imp

But if I import it from a python shell, then no problem:
$ python
What am I doing wrong?
 
B

BartlebyScrivener

I'm on Windows, but I think you need the shebang thing at the top of
your script, right?

Then, put whatever you want to import in a script or module called
testmod.py.

Then you should be able to import either into a script or at the
command line using simply:

import testmod

If it doesn't work then you need to make sure the script is in your
PythonPath.

It looks like you are using the built-in function version of import.

http://docs.python.org/dev/lib/built-in-funcs.html

Is there a reason? Otherwise, do it the easy way:

rick
 
I

I V

So, if this is right, I need to put the .py file to be imported inside
sys.path!! And the relative path will be usedto find the module.

Can I __import__ providing the absolute path?
['', '/usr/lib/python24.zip', '/usr/lib/python2.4',
'/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk',
'/usr/lib/python2.4/lib-dynload',
'/usr/local/lib/python2.4/site-packages',
'/usr/lib/python2.4/site-packages',
'/usr/lib/python2.4/site-packages/HTMLgen',
'/usr/lib/python2.4/site-packages/Numeric',
'/usr/lib/python2.4/site-packages/PIL',
'/usr/lib/python2.4/site-packages/cairo',
'/usr/lib/python2.4/site-packages/gst-0.10',
'/usr/lib/python2.4/site-packages/gtk-2.0',
'/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode',
'/usr/lib/site-python']

Compare with:

tim@october:~/src/tests$ cat > test.py
import sys
print sys.path
tim@october:~/src/tests$ python test.py
['/home/tim/src/tests', '/usr/lib/python24.zip', '/usr/lib/python2.4',
'/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk',
'/usr/lib/python2.4/lib-dynload',
'/usr/local/lib/python2.4/site-packages',
'/usr/lib/python2.4/site-packages',
'/usr/lib/python2.4/site-packages/HTMLgen',
'/usr/lib/python2.4/site-packages/Numeric',
'/usr/lib/python2.4/site-packages/PIL',
'/usr/lib/python2.4/site-packages/cairo',
'/usr/lib/python2.4/site-packages/gst-0.10',
'/usr/lib/python2.4/site-packages/gtk-2.0',
'/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode',
'/usr/lib/site-python']

Note that the first entry in sys.path when run from the interactive
intepreter is a blank string, which doesn't appear in the output from the
script - I assume that's what is allowing the interactive version to
import via an absolute path. I'm not sure if this is the intended
behavior or a bug.

You could make your script work the same way as the interactive
interpreter by doing:

import sys
sys.path.append('')
mod = __import__('/your/absolute/path/module')

But it might be better to just add the particular path:

sys.path.append('/your/absolute/path')
mod = __import__('module')
 
L

linuxnow

You are absolutely right, it's that empty entry that allows the
absolute path to work.
I'll probably add the path to sys.path, but as this is only a config
file from which I need just a few vars, I'll try to use execfile,
looking in past threads it looks like a better option for this use, I
really don't need the new path, it could introduce subtle bugs as it is
not something expected.

Any equivalent way of doing the same with execfile?
 
L

linuxnow

Sorry to follow up myself, I've finally used the execfile approach,
passing an empty dict for capturing locals and then just processing it:

new_settings = {}
execfile(self.SETTINGS_MODULE, new_settings) # returns its locals in
new_settings

# assign UPPER_CASE vars
for setting in new_settings.keys():
setting_value = new_settings[setting] # do what you want with the
vars

It looks like a better solution than polluting the namespace and allows
to use any file as a config file withou the need to have it on
sys.path.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top