How to import X from Python library when X is defined in a package

C. Barnes connellybarnes at yahoo.com
Sat Jul 31 19:11:08 EDT 2004


OK, here's the setup:

  package/
    __init__.py
    X.py

  python/lib/
    X.py

Now inside package, we want to import the copy
of X.py defined in python/lib/.

In C one would do #include <X>

In Python, the easiest solution is:

Make a subpackage called pylibs

  package/
    __init__.py
    X.py
    pylibs/
      __init__.py

In package/pylibs/__init__.py, do import X.

Then to get X from within package, just do

  import package.pylibs.X

The more obvious solutions (such as doing
sys.path.reverse(); import X; sys.path.reverse()
inside the package) do not work, because Python
ignores sys.path 
when inside a package (ie it always looks in the 
package directory even when told explicitly not to).

Anyway, this is the workaround that I found, and it
works fine.

 - Connelly



		
__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail



More information about the Python-list mailing list