467,920 Members | 1,240 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,920 developers. It's quick & easy.

why _import__ only works from interactive interpreter?

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
import test


What am I doing wrong?

May 7 '06 #1
  • viewed: 1517
Share:
5 Replies
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

May 7 '06 #2
It's not that, it looks like __import__looks into sys.path, so
if use an absolute path
it cannot work.

I found this thread and it seems they are right:
http://groups.google.com/group/comp....7790b4ed62771b

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?

Thanks
Pau

May 7 '06 #3
I V
On Sun, 07 May 2006 16:21:01 -0700, li******@gmail.com wrote:
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?

import sys
print sys.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')


May 8 '06 #4
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?

May 8 '06 #5
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.

May 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

147 posts views Thread by Sateesh | last post: by
6 posts views Thread by Avi Berkovich | last post: by
2 posts views Thread by Miki Tebeka | last post: by
reply views Thread by Simon Eves | last post: by
3 posts views Thread by R. Bernstein | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.