Basic questions about packages/modules.

John Abel jabel at plus.net
Fri Jun 10 16:47:00 CEST 2005


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



More information about the Python-list mailing list