[Python-Dev] PEP 328 - Relative Imports (original) (raw)

M.-A. Lemburg mal at egenix.com
Fri Sep 10 11:05:58 CEST 2004


Guido van Rossum wrote:

I know that this has been discussed a few times in the past, but the more I have to deal with building applications using third-party libs or packages, the more I get the feeling that the choice of making "import module" absolute is the wrong path to follow.

The typical scenario goes like this: * you build an application that uses various third-party packages and has to maintain them inside another package, e.g. ThirdPartyCode * you don't have access to the (third-party) package source code or it's not feasable to make changes to it for maintenance reasons Another common case is that you have to deal with third-party code that is not properly packaged as Python package, but comes as a set of top-level modules. In this scenario you typically put all those files into a newly created Python package directory and access the modules in that directory using the package name. In Python 2.3 and 2.4 (as well as all previous versions), both scenarios can easily be implemented without having to change the third-party code. The PEP however suggests that starting with 2.5, the interpreter will issue a warning and 2.6 should default to absolute paths. I'd like to request that the latter change be postponed to Python 3k, or that some other way of supporting the above scenarios is provided that can be enabled in the application. Please remember that changes to application code are well possible. What's not possible is making changes to the packaged third-party code. As long as it's clear that this is a compatibility requirement only I think it's a good idea to support this way of developing apps (even though I think that clever sys.path manipulation can probably get around it, it's not worth breaking existing approaches). All new apps should however use relative imports to reference their own code, so the problem won't be repeated in the future.

I have my doubts that this is going to happen.

People are more likely going to make all imports absolute (like they already do in Java and other languages) - which is good, since it makes reading code much easier and allows for writing packages which are compatible to older Python version, but it also prevent developing applications using the above approach.

I also don't think that extension writers will care enough to make their packages fully relocateable by using relative imports all over - these are hard to read and don't buy the developer of the extension anything.

Anyway, what should the strategy for the PEP look like ?

  1. postpone the defaulting to absolute until P3k

  2. provide a way to customize the behaviour using e.g. a sys function

-- Marc-Andre Lemburg eGenix.com

Professional Python Services directly from the Source (#1, Sep 10 2004)

Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/


::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::



More information about the Python-Dev mailing list