Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing utilities modules across python projects

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

like image 844
Ηλίας Avatar asked Nov 30 '09 11:11

Ηλίας


3 Answers

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 Using .pth files for Python development.

like image 184
unutbu Avatar answered Nov 18 '22 16:11

unutbu


Directory structure:

project1/foo.py
sharedUtils/bar.py

With the directories as you've shown them, from foo.py inside the project1 directory you can add the relative path to sharedUtils as follows:

import sys
sys.path.append("../sharedUtils")
import bar

This avoids hardcoding a C:/../sharedUtils path, and will work as long as you don't change the directory structure.

like image 38
daedalus12 Avatar answered Nov 18 '22 17:11

daedalus12


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.

like image 6
jldupont Avatar answered Nov 18 '22 17:11

jldupont