By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 2,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

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

P: n/a

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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.