[Python-Dev] unexpected import behaviour (original) (raw)

Daniel Waterworth da.waterworth at gmail.com
Fri Jul 30 20:46:44 CEST 2010


On 30 July 2010 18:32, Michael Foord <fuzzyman at voidspace.org.uk> wrote:

On 30/07/2010 17:59, Oleg Broytman wrote:

On Fri, Jul 30, 2010 at 07:26:26AM +0100, Daniel Waterworth wrote:

@Oleg: ... This is purely CPython bug-fixing/the discussion of implementation choices.  I am not sure it's a bug. It isn't a bug but it's a very common cause of bugs, even for relatively experienced Python programmers (this exchange being another case in point). Michael By manipulating sys.path (or symlinks in the FS) one can import the same file as different modules as many times as [s]he wants. Should this be fixed for main? I doubt it. Instead of making main a special case follow the rule: don't import the same module under different paths/names.  Make your script simply from test import main main() Oleg. -- http://www.ironpythoninaction.com/ http://www.voidspace.org.uk/blog READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/da.waterworth%40googlemail.com

Having thought it through thoroughly, my preference is for a warning.

I don't think it's a good practise to import the main module by filename, as renaming the file will break the code. I got stung after, having dropped into a python interpreter shell and imported the module, I executed a function that uses isinstance.

If a warning showed up after importing the module, explaining the problem and suggested that I use import('main') instead, I would have saved myself a fair amount of time debugging code. This is another case of "Explicit is better than implicit.".

It also means that code that relies on the current behaviour will not be broken.

@Oleg: yes, but in the main case, it's more difficult to tell that you are importing something under a different name. I suppose the proof is in the pudding, can anyone think of a case where someone has been annoyed that, having imported that same module twice via symlinks, they have had problems relating to modules being independent instances?

Thanks,

Daniel

-- active-thought.com



More information about the Python-Dev mailing list