how to structure a directory with many scripts and shared code

Benedict Verheyen benedict.verheyen at gmail.com
Tue Feb 16 09:28:12 CET 2010


Steven D'Aprano wrote:
> On Mon, 15 Feb 2010 16:29:05 +0100, Benedict Verheyen wrote:
> 
>> 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
> 
> Sounds like you need to ensure that the top level directory needs to be 
> added to your PYTHONPATH.
> 
> Do you need instructions to do this?
> 

Hi Steve,


thanks, i think i know how to add the top level directory to the PYTHONPATH.

I'm however looking further into the suggestion of ssteinerX to use a
setup.py file to add the tools utility scripts to the pythonpath.
In the near future, another person will be helping me with programming
and thus, i want to make sure the structure is usable by other people
than myself.

Furthermore, i'm thinking if it wouldn't be cleaner to make such a setup.py
file per script, at least the bigger, more important scripts.
Now my structure is like this:

python_scripts
|
|-->trunk
......|-----> all scripts
......|-----> tools

The python_scripts directory and subdirs are versioned via SVN.
I think these steps would help:

1. Make a package of tools.
2. Put the bigger scripts in a seperate directory also using a setup.py file.
3. If possible, put the other scripts that somehow belong together in a seperate
directory. Names need to be more clear but i'm just illustrating a point.

python_scripts
|
|-->trunk
......|-----> my big script 1
................|-----> setup.py
......|-----> my big script 2
................|-----> setup.py
......|-----> database
................|-----> database script 1
................|-----> database script 2
......|-----> tools
................|-----> setup.py

Does that look like a clear structure?

Thanks,
Benedict





More information about the Python-list mailing list