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

C

C. Barnes

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top