[Python-bugs-list] [Bug #115528] import creates extra modules if in working directory

noreply@sourceforge.net noreply@sourceforge.net
Mon, 2 Oct 2000 16:44:03 -0700


Bug #115528, was updated on 2000-Sep-27 20:08
Here is a current snapshot of the bug.

Project: Python
Category: Core
Status: Closed
Resolution: Wont Fix
Bug Group: Not a Bug
Priority: 5
Summary: import creates extra modules if in working directory

Details: Given the directory structure:

lib/
    foo/
         __init__.py
         bar.py

and PYTHONPATH includes lib

If my current working directory is lib/foo, then
>>> import bar
>>> print bar.__file__
bar.py
>>> import foo.bar
>>> print foo.bar.__file__
lib/foo/bar.py
>>> print bar == foo.bar
0

I think that any one file should produce only one module at runtime and this behavior is wrong.

The reason I am trying to count on file-module mapping is because I was to index some behavior by class, and if I can't count on classes from the same file testing identical then I'm stuck.

Follow-Ups:

Date: 2000-Sep-28 06:15
By: fdrake

Comment:
This is a result of including both lib/ and . (as '') on sys.path; the file is legitimately found for two different module imports, so it is loaded for each.  We recommend that you do not run the Python interpreter from within a package; determining this situation from the interpreter at runtime would be prohibitively expensive and slow startup.
-------------------------------------------------------

Date: 2000-Sep-28 06:18
By: lemburg

Comment:
Perhaps modules should use an absolute file name in the __file__
attribute and also be stored under this name in the module
dictionary ?!
-------------------------------------------------------

Date: 2000-Sep-28 10:05
By: fdrake

Comment:
Storing modules by filename in a dictionary isn't quite enough.  If there isn't a .pyc file, or if it's out of date, an import will use the .py file, but the next import will use the .pyc file generated from the first import.

Perhaps the directory of the script shoudn't be added to sys.path if there's an __init__.py file in that directory?  In the specific case reported here, the first import would have raised an ImportError in that case, unless there really was a different bar module elsewhere.  That seems acceptable to me, but I'm not at all sure how much legacy code would break (though I suspect very little).

This should probably be discussed on the Import-SIG; I'll send something shortly.
-------------------------------------------------------

Date: 2000-Oct-02 16:44
By: fdrake

Comment:
I've just sent a note to the Import-SIG about this:

http://www.python.org/pipermail/import-sig/2000-October/000058.html

I will mark this as a "Won't Fix", since it's not actually a bug but simply a mis-use of the interpreter.  
-------------------------------------------------------

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=115528&group_id=5470