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

Basic questions about packages/modules.

Hi, first of all sorry for boring you with a such simple request. I'm
using Python since few days, and I like it even if I'm not yet in
confidence. I'd like to organize my programs in hierarchical
structures, thus I thought that packages could be the solution,
however I have some difficulties understanding their utilization (even
after reading chapter 6 of the tutorial, about modules).

Now an example (silly) to clarify my doubts:

This is the structure:
main.py
importers/
__init__.py (empty)
importer1.py
config/
__init__.py (empty)
parameters.py

main.py
=======
from importers import importer1
print importer1.display()

importers/importer1.py
======================
from config import parameters
def display():
return '-' * parameters.counter

config/parameters.py
====================
counter = 5
All works fine when I run "python main.py", while I get an error
trying to run "python importers/importer1.py":

Traceback (most recent call last):
File "importers/importer1.py", line 1, in ?
from config import parameters
ImportError: No module named config

Can you explain why does that happen? It prevents me to test
importer1.py alone.

TIA,
negroup
Jul 19 '05 #1
2 1546
Negroup wrote:
Hi, first of all sorry for boring you with a such simple request. I'm
using Python since few days, and I like it even if I'm not yet in
confidence. I'd like to organize my programs in hierarchical
structures, thus I thought that packages could be the solution,
however I have some difficulties understanding their utilization (even
after reading chapter 6 of the tutorial, about modules).

Now an example (silly) to clarify my doubts:

This is the structure:
main.py
importers/
__init__.py (empty)
importer1.py
config/
__init__.py (empty)
parameters.py

main.py
=======
from importers import importer1
print importer1.display()

importers/importer1.py
======================
from config import parameters
def display():
return '-' * parameters.counter

config/parameters.py
====================
counter = 5
All works fine when I run "python main.py", while I get an error
trying to run "python importers/importer1.py":

Traceback (most recent call last):
File "importers/importer1.py", line 1, in ?
from config import parameters
ImportError: No module named config

Can you explain why does that happen? It prevents me to test
importer1.py alone.

TIA,
negroup

For your code to work, config would have to be a subdirectory under
importers. Unless, config had been installed in the site-packages.

J
Jul 19 '05 #2
Negroup schrieb:
Hi, first of all sorry for boring you with a such simple request. I'm
using Python since few days, and I like it even if I'm not yet in
confidence. I'd like to organize my programs in hierarchical
structures, thus I thought that packages could be the solution,
however I have some difficulties understanding their utilization (even
after reading chapter 6 of the tutorial, about modules).

Now an example (silly) to clarify my doubts:

This is the structure:
main.py
importers/
__init__.py (empty)
importer1.py
config/
__init__.py (empty)
parameters.py

main.py
=======
from importers import importer1
print importer1.display()

importers/importer1.py
======================
from config import parameters
def display():
return '-' * parameters.counter

config/parameters.py
====================
counter = 5
All works fine when I run "python main.py", while I get an error
trying to run "python importers/importer1.py":

Traceback (most recent call last):
File "importers/importer1.py", line 1, in ?
from config import parameters
ImportError: No module named config

Can you explain why does that happen? It prevents me to test
importer1.py alone.

TIA,
negroup


mainpackage/
main.py
importers/
__init__.py (empty)
importer1.py
config/
__init__.py (empty)
parameters.py

If this is the package structure and you run 'python main.py' from
mainpackage the config package is already in the search path. Therefore
importing config in subsequent modules succeeds.

If you run importer1.py on the level of importers config may not be in
the PYTHONPATH and the import fails.

Solution: reference config in the import statement so that the package
can be found.

from mainpackage.config import parameters

should be suffient if mainpackage is in the PYTHONPATH. But note that
you have to be consistent with your import statements because the
module will be cached in sys. modules using it's import path as key.
Doing both

from mainpackage.config import parameters

and

from config import parameters

in different modules leads to reimports and different modules. This may
cause trouble if you compare objects using class information because
the id's of the classes are different.

Kay

Jul 19 '05 #3

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

Similar topics

1
by: Edward C. Jones | last post by:
I compile and link Python extension modules using the script gcc -fPIC -g -I/usr/local/include/python2.3 \ -Wall -Wstrict-prototypes -c mymodule.c g++ -shared mymodule.o -L/usr/local/lib -o...
4
by: Robert Oschler | last post by:
Hello, (Note: if there is a FAQ that covers this issue just give me the URL. I checked python.org's General and Windows FAQ's and didn't see anything.) I am about to upgrade from Python 2.2...
0
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
1
by: Peter Saffrey | last post by:
I'm trying to write a python service, with an executable in /usr/local/bin, but modules that are kept in a sub-directory of /usr/lib/python/site-packages. Using apt-proxy as my template, I've...
1
by: Kwikrick | last post by:
When calling str() on a sequence or dict object, the elements of the sequence/dict will be represented as if their __repr__ method was called. Why is this? Wouldn't it be more consistent when...
3
by: Tinka | last post by:
Hi, I'm having trouble with my new directory hierarchy. My little python programme worked as long as I had all my modules in one big file. I have decided now that this was just to messy and I...
9
by: pythonewbie | last post by:
Hi all, I am newbie in Python, my wish would be to create python applications for both Linux/Win32. I am stucked on creating a function to get the Python install directory (and site-packages...
5
by: John Ladasky | last post by:
Hi folks, Running Python 2.5 on both a Windows XP laptop, and an Ubuntu Linux 7.04 desktop. I've gotten tired of maintaining multiple copies of my personal modules that I use over and over. ...
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.