467,920 Members | 1,183 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.

ImportError raised in script, not interactive session.

I'm writing a set of modules to monitor remote system services, and I'm
having a problem running my test scripts. When I pass the scripts into
python, like so:

--
$ PYTHONPATH="${TARGET_DIR}" python test.py
--

I get an ImportError:

--
Traceback (most recent call last):
File "./test.py", line 6, in ?
from overwatch.view import console
ImportError: No module named view
--

However, when I load up an interactive Python session:

--
$ PYTHONPATH="${TARGET_DIR}" python
--

I can do the exact same import statement with no problem:

--
Python 2.4.3 (#1, Jul 28 2006, 09:40:08)
[GCC 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>from overwatch.view import console
dir(console)
['Daemon', 'UI', '__builtins__', '__doc__', '__file__', '__name__',
'datetime']
--

My package directory is arranged under ${TARGET_DIR} as prescribed in
the Python docs:
overwatch/
...__init__.py
...[etc.]
...view/
.....__init__.py
.....console.py
.....[etc.]

All modules involved are my own, and I'm setting PYTHONPATH so I can
test with the package installed in my home directory (which I use
distutils to do). I've checked online, and double-checked the Python
docs to make sure my modules are arranged properly, and it all looks
alright. Any ideas?

Jul 31 '06 #1
  • viewed: 1860
Share:
2 Replies
Assuming your setting the target directory to the overwatch folder, and you are starting the interactive session in your home directory, this is what is happening. The folder containing your package must be in the python path, not the folder itself. Try "PYTHONPATH=/home/directory python test.py". The reason it works in the interactive session, assuming you are starting it from your home directory, is that the current directory of the session is placed at the front of the import path; the value of PYTHONPATH is merely added after the current directory, and before the standard directories.

If this isn't the case, what are you using for TARGET_DIR?

Jordan

-----Original Message-----
From: py*******************************************@pyth on.org on behalf of Adam Blinkinsop
Sent: Mon 7/31/2006 5:42 PM
To: py*********@python.org
Subject: ImportError raised in script, not interactive session.

I'm writing a set of modules to monitor remote system services, and I'm
having a problem running my test scripts. When I pass the scripts into
python, like so:

--
$ PYTHONPATH="${TARGET_DIR}" python test.py
--

I get an ImportError:

--
Traceback (most recent call last):
File "./test.py", line 6, in ?
from overwatch.view import console
ImportError: No module named view
--

However, when I load up an interactive Python session:

--
$ PYTHONPATH="${TARGET_DIR}" python
--

I can do the exact same import statement with no problem:

--
Python 2.4.3 (#1, Jul 28 2006, 09:40:08)
[GCC 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>from overwatch.view import console
dir(console)
['Daemon', 'UI', '__builtins__', '__doc__', '__file__', '__name__',
'datetime']
--

My package directory is arranged under ${TARGET_DIR} as prescribed in
the Python docs:
overwatch/
...__init__.py
...[etc.]
...view/
.....__init__.py
.....console.py
.....[etc.]

All modules involved are my own, and I'm setting PYTHONPATH so I can
test with the package installed in my home directory (which I use
distutils to do). I've checked online, and double-checked the Python
docs to make sure my modules are arranged properly, and it all looks
alright. Any ideas?

--
http://mail.python.org/mailman/listinfo/python-list
Jul 31 '06 #2
Jordan R McCoy wrote:
If this isn't the case, what are you using for TARGET_DIR?
${TARGET_DIR} = /home/blinks/projects/overwatch/target/lib/python
I've started the interactive session from several different
directories, and never had a problem.

My make output (note the second item in sys.path):
--
cd /home/blinks/projects/overwatch/target/bin &&
PYTHONPATH="/home/blinks/projects/overwatch/target/lib/python"
../test.py
*** sys.path: ['/home/blinks/projects/overwatch/target/bin',
'/home/blinks/projects/overwatch/target/lib/python',
'/usr/lib/python24.zip', '/usr/lib/python2.4',
'/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk',
'/usr/lib64/python2.4/lib-dynload', '/usr/lib/portage/pym',
'/usr/lib64/python2.4/site-packages',
'/usr/lib/python2.4/site-packages']
Traceback (most recent call last):
File "./test.py", line 6, in ?
from overwatch.view import console
ImportError: No module named view
make: *** [test] Error 1
--

Oh, and:
--
$ ls /home/blinks/projects/overwatch/target/lib/python/
overwatch
--

Thanks for the help! If you want a copy of the project to reproduce,
it's on Subversion at http://tools.assembla.com/svn/Overwatch/trunk
(should be readable).

Jul 31 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by cepl | last post: by
3 posts views Thread by Tim | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.