Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the pythonic way to share common files in multiple projects?

Tags:

python

Lets say I have projects x and y in brother directories: projects/x and projects/y.
There are some utility funcs common to both projects in myutils.py and some db stuff in mydbstuff.py, etc.
Those are minor common goodies, so I don't want to create a single package for them.

Questions arise about the whereabouts of such files, possible changes to PYTHONPATH, proper way to import, etc.

What is the 'pythonic way' to use such files?

like image 482
Paul Oyster Avatar asked Mar 30 '09 11:03

Paul Oyster


2 Answers

The pythonic way is to create a single extra package for them.

Why don't you want to create a package? You can distribute this package with both projects, and the effect would be the same.

You'll never do it right for all instalation scenarios and platforms if you do it by mangling with PYTHONPATH and custom imports.

Just create another package and be done in no time.

like image 113
nosklo Avatar answered Oct 14 '22 17:10

nosklo


You can add path to shared files to sys.path either directly by sys.path.append(pathToShared) or by defining .pth files and add them to with site.addsitedir. Path files (.pth) are simple text files with a path in each line.

like image 26
vartec Avatar answered Oct 14 '22 18:10

vartec