473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
5 1696
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

147
by: Sateesh | last post by:
Hi, I am a beginner in Python, and am wondering what is it about the indentation in Python, without which python scripts do not work properly. Why can't the indentation not so strict so as to give...
6
by: Avi Berkovich | last post by:
Hello, I was unable to use popen2.popen4 to grab python.exe's (2.3) output, for starts, it doesn't show the version information at the beginning and won't return anything when writing to the...
2
by: Miki Tebeka | last post by:
Hello All, If there a way a script can tell Python to enter interactive mode even if the -i command line switch was not given? I want py2exe to create an interactive session, without writing...
2
by: siggy2 | last post by:
Hi All, (sorry for my bad english) I wrote a __tiny__ and __stupid__ recursive script directly into pythonwin interactive window with a time.sleep(1) and a print before each recursion... I...
13
by: dmh2000 | last post by:
I am experimenting with the interactive interpreter environments of Python and Ruby and I ran into what seems to be a fundamental difference. However I may be doing something wrong in Python....
3
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
0
by: Simon Eves | last post by:
I am trying to write a Python module to embed the functionality of Maya (the 3D modelling and animation application from Autodesk, formerly Alias) for doing scripted scene manipulation and...
3
by: R. Bernstein | last post by:
The next release of pydb will have the ability to go into ipython from inside the debugger. Sort of like how in ruby-debug you can go into irb :-) For ipython, this can be done pretty simply;...
4
by: skip | last post by:
Is there a reliable way (this is on Solaris if that matters) to tell if I'm running in the interactive interpreter as opposed to in a script? I think examining sys.argv works, but wanted to double...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.