how to structure a directory with many scripts and shared code

ssteinerX@gmail.com ssteinerx at gmail.com
Tue Feb 16 15:51:34 CET 2010


On Feb 16, 2010, at 3:28 AM, Benedict Verheyen wrote:

> 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?

No.

Make one setup.py at the top, put related scripts (like database) into separate sub-modules, so they can all be imported off a common 'trunk' as you have it above i.e. as trunk.database.xxx.

Then use entry points for any command line scripts.

Slightly harder, to setup initially but much cleaner to use and maintain and, it all installs with one call to setup.py develop.

S




More information about the Python-list mailing list