From fdrake@beopen.com Tue Oct 3 00:34:10 2000 From: fdrake@beopen.com (Fred L. Drake, Jr.) Date: Mon, 2 Oct 2000 19:34:10 -0400 (EDT) Subject: [Import-sig] Why add '' to sys.path if there's an __init__.py? Message-ID: <14809.7026.641214.971383@cj42289-a.reston1.va.home.com> This relates to SourceForge bug report #115528: http://sourceforge.net/bugs/?func=detailbug&bug_id=115528&group_id=5470 This reports a problem that I vaguely recall having heard complaints about before. Essentially, the user is running Python with a script stored in the same directory as the contents of a package: foo/__init__.py module.py script.py The complaint is that "module" can be imported as "module" (in the top-level "unnamed" package), and as "foo.module", where it should be. I don't think this is actually a bug (which is why I've closed the report), but it is clearly something that gets in the way of real users. I think we get better behavior if we remove '' from sys.path if there's an __init__.py file present; importing foo.module would still work, but import module would raise an ImportError exception. Comments please! -Fred -- Fred L. Drake, Jr. BeOpen PythonLabs Team Member From guido@python.org Tue Oct 3 02:18:20 2000 From: guido@python.org (Guido van Rossum) Date: Mon, 02 Oct 2000 20:18:20 -0500 Subject: [Import-sig] Why add '' to sys.path if there's an __init__.py? In-Reply-To: Your message of "Mon, 02 Oct 2000 19:34:10 -0400." <14809.7026.641214.971383@cj42289-a.reston1.va.home.com> References: <14809.7026.641214.971383@cj42289-a.reston1.va.home.com> Message-ID: <200010030118.UAA02203@cj20424-a.reston1.va.home.com> > This relates to SourceForge bug report #115528: > > http://sourceforge.net/bugs/?func=detailbug&bug_id=115528&group_id=5470 > > This reports a problem that I vaguely recall having heard complaints > about before. Essentially, the user is running Python with a script > stored in the same directory as the contents of a package: > > foo/__init__.py > module.py > script.py > > The complaint is that "module" can be imported as "module" (in the > top-level "unnamed" package), and as "foo.module", where it should > be. I don't think this is actually a bug (which is why I've closed > the report), but it is clearly something that gets in the way of real > users. > I think we get better behavior if we remove '' from sys.path if > there's an __init__.py file present; importing foo.module would still > work, but import module would raise an ImportError exception. > Comments please! Very clever. But what if the package's (grand*)parent is *not* on sys.path? I say it's a user error and that's that. --Guido van Rossum (home page: http://www.python.org/~guido/) From fdrake@beopen.com Tue Oct 3 01:18:24 2000 From: fdrake@beopen.com (Fred L. Drake, Jr.) Date: Mon, 2 Oct 2000 20:18:24 -0400 (EDT) Subject: [Import-sig] Why add '' to sys.path if there's an __init__.py? In-Reply-To: <200010030118.UAA02203@cj20424-a.reston1.va.home.com> References: <14809.7026.641214.971383@cj42289-a.reston1.va.home.com> <200010030118.UAA02203@cj20424-a.reston1.va.home.com> Message-ID: <14809.9680.498531.373658@cj42289-a.reston1.va.home.com> Guido van Rossum writes: > Very clever. But what if the package's (grand*)parent is *not* on > sys.path? I say it's a user error and that's that. In that case, the problem (file importable by more than one module name) does not exist, and the ImportError is raised. The solution I proposed doesn't mask the error in any way, it clarifies it by raising an ImportError. I think it improves the situation by producing less user confusion. -Fred -- Fred L. Drake, Jr. BeOpen PythonLabs Team Member From guido@python.org Tue Oct 3 02:27:13 2000 From: guido@python.org (Guido van Rossum) Date: Mon, 02 Oct 2000 20:27:13 -0500 Subject: [Import-sig] Why add '' to sys.path if there's an __init__.py? In-Reply-To: Your message of "Mon, 02 Oct 2000 20:18:24 -0400." <14809.9680.498531.373658@cj42289-a.reston1.va.home.com> References: <14809.7026.641214.971383@cj42289-a.reston1.va.home.com> <200010030118.UAA02203@cj20424-a.reston1.va.home.com> <14809.9680.498531.373658@cj42289-a.reston1.va.home.com> Message-ID: <200010030127.UAA02274@cj20424-a.reston1.va.home.com> > > Very clever. But what if the package's (grand*)parent is *not* on > > sys.path? I say it's a user error and that's that. > > In that case, the problem (file importable by more than one module > name) does not exist, and the ImportError is raised. But the expectation is that you can import stuff from the current directory, and that's violated here. That's what I meant. > The solution I proposed doesn't mask the error in any way, it > clarifies it by raising an ImportError. I think it improves the > situation by producing less user confusion. Usually, yes -- that's why I said "very clever". But if I chdir into somebody else's Lib/test directory, I expect that I can do this: $ python >>> import autotest test_grammar test_opcodes test_operations test_builtin . . . Your change would break this. --Guido van Rossum (home page: http://www.python.org/~guido/) From fdrake@beopen.com Tue Oct 3 01:26:59 2000 From: fdrake@beopen.com (Fred L. Drake, Jr.) Date: Mon, 2 Oct 2000 20:26:59 -0400 (EDT) Subject: [Import-sig] Why add '' to sys.path if there's an __init__.py? In-Reply-To: <200010030127.UAA02274@cj20424-a.reston1.va.home.com> References: <14809.7026.641214.971383@cj42289-a.reston1.va.home.com> <200010030118.UAA02203@cj20424-a.reston1.va.home.com> <14809.9680.498531.373658@cj42289-a.reston1.va.home.com> <200010030127.UAA02274@cj20424-a.reston1.va.home.com> Message-ID: <14809.10195.867824.306336@cj42289-a.reston1.va.home.com> Guido van Rossum writes: > But the expectation is that you can import stuff from the current > directory, and that's violated here. That's what I meant. I think that expectation is broken if the current directory is inside a package -- it was originally set up before we had packages. > But if I chdir into somebody else's Lib/test directory, I expect that > I can do this: And I say the expectation is wrong. I can't think of anything but Lib/test/ that works this way, and we've decided (in python-dev as I recall) that package-relative imports should be discouraged. Instead, cd to Lib/ and import test.autotest. -Fred -- Fred L. Drake, Jr. BeOpen PythonLabs Team Member