Search code examples
pythonmodulepackagingpythonpath

Developing and using the same Python on the same computer


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?


Solution

  • 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