1

I have a collection of libraries in python that I need to have in a single zip file.

The structure is:

rootlib
   |
   --- lib_a
   |     |
   |     --- lib_a.py
   |
   --- lib_b
         |
         --- lib_b.py

I want to be able to reference lib_b from lib_a. When unzipped, I can load lib_b from lib_a.lib_a.py simply by doing:

from lib_b import lib_b

but when I zip up the rootlib folder and use it in a script like this:

import sys
sys.path.insert(0, '/home/myuser/rootlib.zip')
from rootlib.lib_a.lib_a import LibA

I get an error that says:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    from rootlib.lib_a.lib_a import LibA
  File "/home/user/rootlib.zip/rootlib/lib_a/lib_a.py", line 3, in <module>
ImportError: No module named lib_b.lib_b

How do I reference/import the adjacent lib_b library from lib_a?

whisperstream
  • 1,897
  • 3
  • 20
  • 25
  • possible duplicate of [How to do relative imports in Python?](http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python) – pacholik May 29 '15 at 07:47
  • Add a `__init__.py` to the rootlib folder. Does it work? – User May 29 '15 at 09:26

1 Answers1

0

Turns out that having the rootlib included in the zip file is a problem

   --- lib_a
   |     |
   |     --- lib_a.py
   |
   --- lib_b
         |
         --- lib_b.py

Everything works fine when you don't have a root directory.

whisperstream
  • 1,897
  • 3
  • 20
  • 25