Paul Oyster
Paul Oyster

Reputation: 1248

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

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?

Upvotes: 5

Views: 1140

Answers (5)

Jason Baker
Jason Baker

Reputation: 198517

I'd advise using setuptools for this. It allows you to set dependencies so you can make sure all of these packages/individual modules are on the sys.path before installing a package. If you want to install something that's just a single source file, it has support for automagically generating a simple setup.py for it. This may be useful if you decide not to go the package route.

If you plan on deploying this on multiple computers, I will usually set up a webserver with all the dependencies I plan on using so it can install them for you automatically.

I've also heard good things about paver, but haven't used it myself.

Upvotes: 0

codeape
codeape

Reputation: 100756

I agree with 'create a package'.

If you cannot do that, how about using symbolic links/junctions (ln -s on Linux, linkd on Windows)?

Upvotes: 0

Jason Coon
Jason Coon

Reputation: 18421

You can also create a .pth file, which will store the directory(ies) that you want added to your PYTHONPATH. .pth files are copied to the Python/lib/site-packages directory, and any directory in that file will be added to your PYTHONPATH at runtime.

http://docs.python.org/library/site.html
StackOVerflow question (see accepted solution)

Upvotes: 0

vartec
vartec

Reputation: 134551

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.

Upvotes: 1

nosklo
nosklo

Reputation: 222792

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.

Upvotes: 9

Related Questions