I'm developing a Python utility module to help with file downloads, archives, etc. I have a project set up in a virtual environment along with my unit tests. When I want to use this module on the same computer (essentially as "Production"), I move the files to the mymodule directory in the ~/dev/modules/mymodule
I keep all 3rd-party modules under ~/dev/modules/contrib. This contrib path is on my PYTHONPATH, but mymodule is NOT because I've noticed that if mymodule is on my PYTHONPATH, my unit tests cannot distinguish between the "Development" version and the "Production" version. But now if I want to use this common utility module, I have to manually add it to the PYTHONPATH.
This works, but I'm sure there's a better, more automated way.
What is the best way to have a Development and Production module on the same computer? For instance, is there a way to set PYTHONPATH dynamically?
I'm guessing by virtual environment you mean the virtualenv package?
http://pypi.python.org/pypi/virtualenv
What I'd try (and apologies if I've not understood the question right) is:
Then, for testing:
And, for production:
That said, I write this with the confidence of someone who's not actually tried setting using virtualenv in anger and the hope I've vaguely understood your question... ;)
You can add/modify python paths at
sys.path
, just make sure that the first path is the current directory"."
, because some third-party modules rely on importing from the directory of the current module.More information on python paths: http://djangotricks.blogspot.com/2008/09/note-on-python-paths.html
You could set the
PYTHONPATH
as a global environment variable pointing to your Production code, and then in any shell in which you want to use the Development code, change thePYTHONPATH
to point to that code.(Is that too simplistic? Have I missed something?)