This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: name shadowing while a module tries to import another
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: ksriram, pmpp, xtreak
Priority: normal Keywords:

Created on 2018-12-02 10:11 by ksriram, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg330874 - (view) Author: Sriram Krishna (ksriram) Date: 2018-12-02 10:11
Suppose I have a file profile.py in the same directory as the file I am running (say test.py)

Let the contents of the files be:

profile.py:
raise Exception

test.py:
import cProfile

now if I run test.py

$ python test.py
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import cProfile
  File "/usr/lib/python3.7/cProfile.py", line 10, in <module>
    import profile as _pyprofile
  File "/home/username/profile.py", line 1, in <module>
    raise Exception
Exception

The file profile.py in '/usr/lib/python3.7' should have been loaded. This would also happen if test.py imported a module or package which imported cProfile.

The only possible way of avoiding this problem completely is by ensuring that the name of any the python files don't match a builtin python file or the name of any installed package.

A python user can't be expected to know the name of every possible file in the Python standard library. Maybe the current working directory should be removed from sys.path when importing from within another module not in the same directory.
msg330876 - (view) Author: pmp-p (pmpp) * Date: 2018-12-02 10:35
hi you are obsverving that because current working directory is set *first* in sys.path as a commodity

you can avoid that with

import sys,os
sys.path.remove( os.getcwd() )

at the start of your program. 

otherwise usually it is wise to put user module in a package and choose its name with care.
msg330877 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-12-02 10:43
> A python user can't be expected to know the name of every possible file in the Python standard library. Maybe the current working directory should be removed from sys.path when importing from within another module not in the same directory.

I think there was a similar discussion about this since Perl did a related change : https://mail.python.org/pipermail/python-ideas/2017-June/045842.html
msg330880 - (view) Author: Sriram Krishna (ksriram) Date: 2018-12-02 11:27
My contention is that this behaviour breaks the Principle of Least Astonishment.

In the particular example I gave, the user doesn't know (and can't be expected to know) the existence of a module called profile in the standard library. Here the user is not explicitly importing profile. The user can't be expected to know which submodules are imported by the modules they use. Hence they wouldn't expect another file sitting in the same directory to get imported when they are not explicitly importing it.

on 2018-12-02 10:35, pmpp said:
> you can avoid that with

> import sys,os
> sys.path.remove( os.getcwd() )

> at the start of your program. 

completely removing cwd from sys.path would disable the option of importing user modules.

Any method of fixing this would be backwards incompatible, and will break some (most likely badly written) code.

My hack solution was to have cwd in sys.path only if __name__ is '__main__'.
msg330882 - (view) Author: Sriram Krishna (ksriram) Date: 2018-12-02 13:04
Already covered in issue29929: https://bugs.python.org/issue29929.

Thanks xtreak for the link to the message thread.

PEP 432 seems to have methods to resolve this.

Currently if the code doesn't access to .local, one can use the -I commandline option and use from . import module for local imports.
History
Date User Action Args
2022-04-11 14:59:08adminsetgithub: 79556
2018-12-02 13:04:04ksriramsetstatus: open -> closed
resolution: duplicate
messages: + msg330882

stage: resolved
2018-12-02 11:27:56ksriramsetmessages: + msg330880
2018-12-02 10:43:14xtreaksetnosy: + xtreak
messages: + msg330877
2018-12-02 10:35:10pmppsetnosy: + pmpp
messages: + msg330876
2018-12-02 10:11:52ksriramcreate