Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-21 Thread Nick Coghlan
On 21 March 2018 at 10:01, Chris Billington wrote: > > > On Wed, Mar 21, 2018 at 10:58 AM, Chris Billington < > chrisjbilling...@gmail.com> wrote: > >> I don't think that's true: >> >> >> >> On Wed, Mar 21, 2018 at 10:51 AM, Greg Ewing > > wrote: >> >>> Chris Billington wrote: >>> I wonder h

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-21 Thread Nick Coghlan
On 20 March 2018 at 16:25, Gregory P. Smith wrote: > On Thu, Mar 15, 2018 at 3:26 AM Nick Coghlan wrote: > >> If we did head in this direction, then we'd also need to accept & >> implement PEP 499 [1] (which proposes aliasing __main__ as >> __main__.__spec__.name in sys.modules when executed wit

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-20 Thread Danilo J. S. Bellini
On 20 March 2018 at 07:23, Chris Billington wrote: > It seems like running from within a package directory is bad news mostly > *because* of the double import problem, [...] > I think the main issue regarding loading a module from within a package directory is about the paths, non-absolute non-r

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-20 Thread Chris Billington
On Wed, Mar 21, 2018 at 10:58 AM, Chris Billington < chrisjbilling...@gmail.com> wrote: > I don't think that's true: > > > > On Wed, Mar 21, 2018 at 10:51 AM, Greg Ewing > wrote: > >> Chris Billington wrote: >> >>> I wonder how mercurial gets around the fact that its own imports might >>> be shad

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-20 Thread Chris Billington
I don't think that's true: On Wed, Mar 21, 2018 at 10:51 AM, Greg Ewing wrote: > Chris Billington wrote: > >> I wonder how mercurial gets around the fact that its own imports might be >> shadowed by whatever's in the current working directory. >> > > The cwd is only added to sys.path in the in

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-20 Thread Greg Ewing
Chris Billington wrote: I wonder how mercurial gets around the fact that its own imports might be shadowed by whatever's in the current working directory. The cwd is only added to sys.path in the interactive interpreter, not when you run "python something.py". So it's not usually a problem for

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-20 Thread Chris Billington
On Tue, Mar 20, 2018 at 8:06 PM, Steven D'Aprano wrote: > On Wed, Mar 14, 2018 at 12:09:55PM -0700, Guido van Rossum wrote: > > > Yeah, one should never add a module to sys.path that has a __init__.py > file. > > Should import raise a warning in that case? > > I wouldn't want an outright error. I

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-20 Thread Steven D'Aprano
On Wed, Mar 14, 2018 at 12:09:55PM -0700, Guido van Rossum wrote: > Yeah, one should never add a module to sys.path that has a __init__.py file. Should import raise a warning in that case? I wouldn't want an outright error. I've cd'ed into a package directory in the shell, then run a python mod

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-19 Thread Gregory P. Smith
On Thu, Mar 15, 2018 at 3:26 AM Nick Coghlan wrote: > On 14 March 2018 at 15:20, Chris Billington > wrote: >> >> I wonder if there's any reason something like this shouldn't be built >> into Python's default import system. >> > > There are two main challenges with enforcing such a check, one aff

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-15 Thread Nick Coghlan
On 14 March 2018 at 15:20, Chris Billington wrote: > > I wonder if there's any reason something like this shouldn't be built into > Python's default import system. > There are two main challenges with enforcing such a check, one affecting end users in general, one affecting standard library maint

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-14 Thread Guido van Rossum
Yeah, one should never add a module to sys.path that has a __init__.py file. On Wed, Mar 14, 2018 at 11:25 AM, Brendan Barnwell wrote: > On 2018-03-14 04:18, Steven D'Aprano wrote: > >> Apart from intentionally manipulating sys.modules or the import system, >> or playing file system tricks like

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-14 Thread Chris Angelico
On Thu, Mar 15, 2018 at 5:25 AM, Brendan Barnwell wrote: > On 2018-03-14 04:18, Steven D'Aprano wrote: >> >> Apart from intentionally manipulating sys.modules or the import system, >> or playing file system tricks like hard-linking your files, under what >> circumstances can this occur by accident

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-14 Thread Brendan Barnwell
On 2018-03-14 04:18, Steven D'Aprano wrote: Apart from intentionally manipulating sys.modules or the import system, or playing file system tricks like hard-linking your files, under what circumstances can this occur by accident? It can occur if a given directory winds up appearing twice on the

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-14 Thread Chris Billington
The (perhaps minor) problem with simply having the same module object have two entries in sys.modules is that at least one of them will have a different __name__ attribute than the corresponding key in sys.modules. It's not the end of the world (os.path.__name__ is 'posixpath' on linux, not 'os.pat

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-14 Thread Steven D'Aprano
On Wed, Mar 14, 2018 at 05:06:02PM +1100, Chris Angelico wrote: > On Wed, Mar 14, 2018 at 4:58 PM, Steven D'Aprano wrote: > > On Wed, Mar 14, 2018 at 04:20:20PM +1100, Chris Billington wrote: > > > >> Instead, maybe a user should just get a big fat error if they try to import > >> the same file tw

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-13 Thread Chris Billington
Exactly. The example in the "if __name__ == '__main__'" block of my module I linked imports numpy as np, and then adds the numpy package directory to sys.path and imports linalg. This is detected as numpy.linalg being imported twice, once as numpy.linalg, and once as linalg. The output error is be

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-13 Thread Chris Angelico
On Wed, Mar 14, 2018 at 4:58 PM, Steven D'Aprano wrote: > On Wed, Mar 14, 2018 at 04:20:20PM +1100, Chris Billington wrote: > >> Instead, maybe a user should just get a big fat error if they try to import >> the same file twice under different names. > > Absolutely not. > > Suppose I import a libr

Re: [Python-ideas] Disallow importing the same module under multiple names

2018-03-13 Thread Steven D'Aprano
On Wed, Mar 14, 2018 at 04:20:20PM +1100, Chris Billington wrote: > Instead, maybe a user should just get a big fat error if they try to import > the same file twice under different names. Absolutely not. Suppose I import a library, Spam, which does "import numpy". Now I try to "import numpy as

[Python-ideas] Disallow importing the same module under multiple names

2018-03-13 Thread Chris Billington
The Python "double import" problem (discussed here ) leads to subtle bugs in code that change depending on what directory the code was run from. It's hard to think of reasons why importing the same filepath mult