views:

114

answers:

2

Hi,

What would be the best directory structure strategy to share a utilities module across my python projects? As the common modules would be updated with new functions I would not want to put them in the python install directory.

project1/
project2/
sharedUtils/

From project1 I can not use "import ..\sharedUtils", is there any other way? I would rather not hardcode the "sharedUtils" location

Thanks in advance

+4  A: 

Make a separate standalone package? And put it in the /site-packages of your python install?

There is also my personal favorite when it comes to development mode: use of symlinks and/or *.pth files.

jldupont
Interesting use of question marks...
tgray
Since there are many possibilities... I use the "Socrates approach": try to make people reflect on the possibilities.
jldupont
How do you deal with revision control this way? When you commit your project, do you also go manually to the site-packages directory and do the same?
Ηλίας
@iKarampa: I am not quite sure I am following you. Could you rephrase and/or start a new question?
jldupont
Hi, sorry to be a pain! I think my question is answered. Just to clear this out:When you use a standalone package on the /site-packages, each time you commit the project, do you commit the helper module as well? Since they are in different locations how do you sort it out? (windows user, so symlinks no good here!)
Ηλίας
If the helper module is a project on its own then it is up to you to decide I guess. On Windows, you could use the `*.pth` files instead of symlinks.
jldupont
+1  A: 

Suppose you have sharedUtils/utils_foo and sharedUtils/utils_bar. You could edit your PYTHONPATH to include sharedUtils, then import them in project1 and project2 using

import utils_foo
import utils_bar
etc.

In linux you could do that be editing ~/.profile with something like this:

PYTHONPATH=/path/to/sharedUtils:/other/paths
export PYTHONPATH

Using the PYTHONPATH environment variable affects the directories that python searches when looking for modules. Since every user can set his own PYTHONPATH, this solution is good for personal projects.

If you want all users on the machine to be able to import modules in sharedUtils, then you can achieve this by using a .pth file. Exactly where you put the .pth file may depend on your python distribution. See http://bob.pythonmac.org/archives/2005/02/06/using-pth-files-for-python-development/

unutbu
Sorry but new to python, would you hard code the full path in PYTHONPATH or use "../" ?
Ηλίας
@iKarampa, no problem. PYTHONPATH requires an full (absolute) path.
unutbu
When you distribute your file, you need somehow to concatenate all utils in one directory and standard imports will work as normal. Ok got it!Thanks
Ηλίας