How to include a python .egg library that is in a subdirectory (relative location)?

Brian Jordan picture Brian Jordan · Jul 10, 2009 · Viewed 25.7k times · Source

How do you import python .egg files that are stored in a relative location to the .py code?

For example,

My Application/
My Application/library1.egg
My Application/libs/library2.egg
My Application/test.py

How do you import and use library1 and library2 from within test.py, while leaving the .egg libraries in-place?

Answer

Eli Bendersky picture Eli Bendersky · Jul 10, 2009

An .egg is just a .zip file that acts like a directory from which you can import stuff.

You can use the PYTHONPATH variable to add the .egg to your path, or append a directory to sys.path. Another option is to use a .pth file pointing to the eggs.

For more info see A Small Introduction to Python eggs, Python Eggs and All about eggs.

For example, if your library1.egg contains a package named foo, and you add library1.egg to PYTHONPATH, you can simply import foo

If you can't set PYTHONPATH, you can write:

import sys
sys.path.append("library1.egg")
import foo