PYTHONPATH and modules

J

Juan

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.
 
B

Bruno Desthuilliers

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
 

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,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top