18

I want to create a "library" of Python modules which I will be able to access from several separate project folders.

For example, I want the Python scripts in /proj1/ and /proj2/ to have access to /lib/.

/lib/help.py
/lib/more_help.py

/proj1/script.py
/proj1/script2.py

/proj2/this_script.py
/proj2/another_script.py

I don't want a single directory with all the Python scripts, as this seems rather disorganized. I also definitely don't want to copy the same /lib/ script into each of the different projects.

What is the ideal way to handle this in Python? Is it appending to Python's path? Or is this more of a hack? This seems to have the disadvantage of making the files less portable. Or is it this question/answer about using relative paths? Or something else?

I should add that I'm interested in Python 2.x rather than 3.x, if it matters.

Community
  • 1
  • 1
David C
  • 7,204
  • 5
  • 46
  • 65

3 Answers3

13

Turn lib/ into a package, then put it in one of the directories in sys.path (or add a new entry). You can then import e.g. lib.help into your projects.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
3

Follow the standard road that everyone takes: make your code a proper Python package with a proper setup.py. The benefits are: easy_install'able, easy distributable, easy generation of command line script (through console_scripts entry point) etc....

2

I think the best tool you can use to keep under control the environment of your project is virtualenv. You create a new virtual environment and install there your packages, then you run your project using the python executable that virtualenv provides you.

Probably you should use distutils in your library, in this way installing it with virtualenv is really easy, because virtualenv installs setuptools or distribute in the virtual environment, thus allowing you to install in the virtual environment packages from pypi or from your local machine.

There is also the possibility to create a custom bootstrap script that automatically installs some libraries after the virtual environment creation.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231