469,312 Members | 2,464 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

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


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
Jul 18 '05 #1
0 888

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.