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

2

16

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?

Curson answered 10/7, 2009 at 8:29 Comment(0)
O
28

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
Osvaldooswal answered 10/7, 2009 at 8:32 Comment(2)
So: import sys sys.path.append("library1.egg") import fooCurson
hi, How can I load the module/library contained in egg dynamically and instantiate the class?Priming
D
2

You can include each egg on the sys.path, or create a .pth file that mentions each egg.

If you have many eggs that you need in your system I'd recommend using something like buildout, that will make the setup easily replicatable. It will handle the eggs for you.

http://pypi.python.org/pypi/zc.buildout/

Driskill answered 10/7, 2009 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.