5

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?

asked Mar 30, 2009 at 11:12

5 Answers 5

10

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.

answered Mar 30, 2009 at 11:24
Sign up to request clarification or add additional context in comments.

1 Comment

+1: Avoid modifying sys.path dynamically; use packages or external PYTHONPATH setting.
1

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.

answered Mar 30, 2009 at 11:25

Comments

0

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)

answered Mar 30, 2009 at 12:39

Comments

0

I agree with 'create a package'.

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

answered Mar 30, 2009 at 12:50

Comments

0

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.

answered Mar 30, 2009 at 12:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.