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

Jp Calderone exarkun at divmod.com
Sat Jul 31 21:14:32 EDT 2004


C. Barnes wrote:
> 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:

   touch python/__init__.py python/lib/__init__.py
   from python.lib import X

Contortions to make this work some other way are possible, but 
unnecessary.  There has been discussion about allowing absolute and 
relative imports to be disambiguated, but if you restrict yourself to 
absolute imports from packages, there is no difficulty.

   Jp



More information about the Python-list mailing list