473,394 Members | 1,828 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,394 software developers and data experts.

PYTHONPATH and modules

Hi

I am programming a little script that makes use of a module I
developed before. The utils are inside the directory src of the
directory utils, and the package is nutum.utils. The script is in the
directory src inside the directory sysinfo, and the package is
nutum.sysinfo. Well, if not clear, this is the list of files:

ls -lR ~/workspace (imaginary output):

utils/src/nutum/__init__.py
utils/src/nutum/utils/__init__.py
utils/src/nutum/utils/general.py
utils/src/nutum/utils/elapsed_time.py
utils/src/nutum/utils/execute_command.py
utils/src/nutum/utils/size_units.py

sysinfo/src/nutum/__init__.py
sysinfo/src/nutum/sysinfo/__init__.py
sysinfo/src/nutum/sysinfo/sysinfo.py
sysinfo/src/nutum/sysinfo/modules/__init__.py
sysinfo/src/nutum/sysinfo/modules/base_module.py
sysinfo/src/nutum/sysinfo/modules/os.py

So, when from the home directory I run this command:

PYTHONPATH=workspace/utils/src/:workspace/sysinfo/src python -m
nutum.sysinfo.sysinfo --config-file /home/juan/sysinfo.config.ini

I get this output:

Traceback (most recent call last):
File "/usr/lib/python2.5/runpy.py", line 85, in run_module
loader = get_loader(mod_name)
File "/usr/lib/python2.5/pkgutil.py", line 456, in get_loader
return find_loader(fullname)
File "/usr/lib/python2.5/pkgutil.py", line 466, in find_loader
for importer in iter_importers(fullname):
File "/usr/lib/python2.5/pkgutil.py", line 422, in iter_importers
__import__(pkg)
ImportError: No module named sysinfo

Also, if I run this similar command:

PYTHONPATH=workspace/utils/src/:workspace/sysinfo/src python workspace/
sysinfo/src/nutum/sysinfo/sysinfo.py --config-file /home/juan/
sysinfo.config.ini

The output is this:

No module named sysinfo.modules.os
Traceback (most recent call last):
File "workspace/sysinfo/src/nutum/sysinfo/sysinfo.py", line 156, in
<module>
classinstance = get_class(class_name)()
File "/home/juan/workspace/utils/src/nutum/utils/general.py", line
44, in get_class
aClass = get_func(fullClassName)
File "/home/juan/workspace/utils/src/nutum/utils/general.py", line
27, in get_func
aMod = get_mod(modPath)
File "/home/juan/workspace/utils/src/nutum/utils/general.py", line
14, in get_mod
aMod = __import__(modulePath, globals(), locals(), [''])
ImportError: No module named sysinfo.modules.os

Why this does happens? Why Python does not find the modules if they
are already in the path? Why (in the second example) tries to find the
module "sysinfo.modules.os" when I am really importing the module
"nutum.sysinfo.modules.os"

Thanks in advance.
Aug 28 '08 #1
1 1865
Juan a écrit :
Hi

I am programming a little script that makes use of a module I
developed before. The utils are inside the directory src of the
directory utils, and the package is nutum.utils. The script is in the
directory src inside the directory sysinfo, and the package is
nutum.sysinfo.
Won't work, cf below.
Well, if not clear, this is the list of files:

ls -lR ~/workspace (imaginary output):

utils/src/nutum/__init__.py
utils/src/nutum/utils/__init__.py
utils/src/nutum/utils/general.py
utils/src/nutum/utils/elapsed_time.py
utils/src/nutum/utils/execute_command.py
utils/src/nutum/utils/size_units.py

sysinfo/src/nutum/__init__.py
sysinfo/src/nutum/sysinfo/__init__.py
sysinfo/src/nutum/sysinfo/sysinfo.py
sysinfo/src/nutum/sysinfo/modules/__init__.py
sysinfo/src/nutum/sysinfo/modules/base_module.py
sysinfo/src/nutum/sysinfo/modules/os.py
A Python package is a filesystem directory with an __init__.py file in
it. In your above layout, you have two *distinct, unrelated* packages
both named nutum. The first found in sys.path will shadow the second.

You either need to rename one, or "refactor" your layout to have utils
and sysinfo under the same root package, ie:

workspace/src/nutum/__init__.py
workspace/src/nutum/utils/__init__.py
workspace/src/nutum/utils/general.py
workspace/src/nutum/utils/elapsed_time.py
workspace/src/nutum/utils/execute_command.py
workspace/src/nutum/utils/size_units.py
workspace/src/nutum/sysinfo/__init__.py
workspace/src/nutum/sysinfo/sysinfo.py
workspace/src/nutum/sysinfo/modules/__init__.py
workspace/src/nutum/sysinfo/modules/base_module.py
workspace/src/nutum/sysinfo/modules/os.py

HTH
Aug 28 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tim Isakson | last post by:
OK - I just checked Programming Python out from the library, and am enjoying seeing the examples (I've dabbled in Python some previously, but an trying to expand my knowledge a bit). I'm having...
4
by: r.e.s. | last post by:
I have no PYTHONPATH nor any other python-related environment variables, yet everything seems fine. (I'm using PythonWin with winxp.) As long as modules are loaded through PythonWin, is...
5
by: pinkfloydhomer | last post by:
I have a Makefile target that uses a python script, like: %.abc: %.def python myscript.py The problem is that myscript.py and some modules that myscript.py imports are not in the current...
3
by: marc.wyburn | last post by:
I've downloaded the hypertext module and put it in the c:\python24\lib folder. I would have thought that since the lib directory is in the Pythonpath,I would be able to import python scripts from...
3
by: rh0dium | last post by:
Hi all, Can anyone help me out. I would like to have python automatically look in a path for modules similar to editing the PYTHONPATH but do it at compile time so every user doesn't have to do...
9
by: Stef Mientki | last post by:
Is it possible to change the searchpath for modules on the flight, under winXP ? Most preferred is some command to extend the searchpath. (the environment variable PYTHONPATH needs a reboot) ...
5
by: Rajesh | last post by:
Hi, The '-I<path>' option adds the path to the list of directories that contains modules that can be included in a script. I can use it as "#!/ usr/bin/perl -I<path_to_my_modules>" thereby not...
2
by: Sergio Correia | last post by:
Hi, I'm trying to add a personal folder to the path used by python in searching for packages and modules. This folder, "C:\docs\utils" , has some packages not yet ready for "site-packages". ...
1
by: joshetomlinson | last post by:
Hi all, I'm new to python, and am trying to determine if it's possible to do the following... I have a directory structure like this, with both 'dir1' and 'dir2' in my PYTHONPATH dir1/...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.