Organising packages/modules - importing functions from a common.py in a separate directory?

Peter Otten __peter__ at web.de
Wed Oct 30 05:48:28 EDT 2013


Victor Hooi wrote:

> Wait - err, subpackage != module, right? Do you think you could explain
> what a sub-package is please? I tried Googling, and couldn't seem to find
> the term in this context.

In analogy to subdirectory I em_load and pg_load -- and common if you add an 
__init__.py would be sub-packages, provided only the parent of foo_loading 
is in sys.path and you import them with

import foo_loading.pg_load

etc.

> Also, so you're saying to put the actual script that I want to invoke
> *outside* the Python package.
> 
> Do you mean something like this:
> 
>> sync_em.py
>> sync_pg.py
>> foo_loading/
>>     __init__.py
>>     common/
           __init__.py
>>         common_foo.py
>>     em_load/
>>         __init__.py
>>         config.yaml
>>         em.py
>>     pg_load/
>>         __init__.py
>>         config.yaml
>>         pg.py
> 
> and the sync_em.py and sync_pg.py would just be thin wrappers pulling in
> things from em.py and pg.py? Is that a recommended approach to organise
> the code?

I don't know. I prefer it that way.
 
> Would it make any difference if I actually packaged it up so you could
> install it in site-packages? Could I then call modules from other modules
> within the package?

If you mean "import", yes, installing is one way to get it into sys.path.





More information about the Python-list mailing list