how to structure a directory with many scripts and shared code

Jean-Michel Pichavant jeanmichel at sequans.com
Mon Feb 15 17:06:49 CET 2010


Benedict Verheyen wrote:
> Hi,
>
> i wanted to ask how you guys structure your code.
> I mainly have scripts that automate small tasks.
> These scripts use a common set of utility code.
> My code is structured like this, for example:
>
> script_1.py
> script_2.py
> script_3.py
> tools\
> |----->__init__.py
> |----->logutils.py
> |----->mailutils.py
>
> I was thinking of putting code in seperate directories because the number
> of scripts grows fast and i want to keep it clean.
> Something like this:
>
> script_1.py
> tools\
> |----->__init__.py
> |----->logutils.py
> |----->mailutils.py
> database\
> |----->__init__.py
> |----->script_2.py
> |----->script_3.py
>
> However, when i make a subdirectory, for example database and put a script in there,
> how would i import logutils or mailtutils from within the database subdirectory?
> This fails:
>     from tools.logutils import logger
>
>  Thanks,
>  Benedict
>
>   
Hi,

1/ Provide the output of the error so we can provide some useful help.
2/ Embed your structure into a package. Names like 'tools', 'lib', 
'mail', 'log' are too generic and will clash with other badly designed 
module/package names. Choose a name for you package, and use it with 
absolute imports e.g :
from myBeautifulPackage.tools.logutil import logger
3/ make sure your working copy of myBeautifulPackage is in your PYTHONPATH.

JM








More information about the Python-list mailing list