Incompatible idioms: relative imports, top-level program file

Ethan Furman ethan at stoneleaf.us
Fri Feb 6 20:12:44 EST 2015


On 02/06/2015 04:44 PM, Ben Finney wrote:
> Ethan Furman <ethan at stoneleaf.us> writes:
> 
>> On 02/06/2015 02:56 PM, Ben Finney wrote:
>>> It is a deliberate design decision that direct import of a module
>>> makes that module blind to its location in the package hierarchy.
>>>
>>> That's a design decision I deplore, because it makes something that
>>> should be easy (write a command directly into a file and invoke that
>>> file as an executable program) tortuously difficult when the program
>>> comprises several modules.
>>
>> Can you explain that a bit more?
> 
> A program will often have enough complexity that its implementation
> occupies several sub-modules. There's no need to explose those in a site
> package, they normally only need to be local to the application.

If they are not in the Python module path, how are they imported at all?

> Python deliberately divorces the top-level module from its package, so
> it can't access its own relative modules! The relative import fails with
> an ImportError “Attempted relative import in non-package”.

My understanding is that imports is for libraries [1], and libraries must be in sys.path.  Are you saying that after
placing your top-level file's path in sys.path, that relative imports do not work?

--
~Ethan~

[1] which can be a single module

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20150206/80ad4414/attachment.sig>


More information about the Python-list mailing list