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: importing module shutil executes file 'copy.py'
Type: behavior Stage: resolved
Components: Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Oliver Etchebarne (drmad), ammar2, eryksun, vstinner
Priority: normal Keywords:

Created on 2017-03-15 16:35 by Oliver Etchebarne (drmad), last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg289681 - (view) Author: Oliver Etchebarne (drmad) (Oliver Etchebarne (drmad)) Date: 2017-03-15 16:35
I didn't research this issue further. Create a file 'test.py', and write only 'import shutil'. Then create a file 'copy.py' in the same directory, and write something inside, like 'print ("OH NO")'.

When you run test.py, 'copy.py' is executed, and prints the string. Tested with python 3.5 and 3.6. Works as expected (test.py doing nothing) in python 2.7
msg289696 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2017-03-15 20:50
I was only able to recreate this under 3.4 and 3.3, and both of them are under "security" status on https://docs.python.org/devguide/#status-of-python-branches

Should this be marked as won't fix?
msg289698 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2017-03-15 21:06
As per discussion with haypo on irc, this is not a bug since essentially you've made a file which shadows the following stdlib module https://docs.python.org/3/library/copy.html

When shutil goes to import the copy module, your copy module is given higher priority in the import machinery which is why your code gets executed. You can read up more about this here: http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-name-shadowing-trap
msg289699 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-03-15 21:08
Run "python3 -I script.py" to not insert '' (current directory) at the first position of sys.path.
https://docs.python.org/dev/using/cmdline.html#cmdoption-I
msg289700 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-03-15 23:41
> Run "python3 -I script.py" to not insert '' (current directory) 
> at the first position of sys.path.

That should be "script directory", not "current directory". We only add the current directory to sys.path in interactive mode, and for -c and -m. Adding the current directory to sys.path when running a script would be silly and cause endless grief.
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 74007
2017-03-15 23:41:18eryksunsetnosy: + eryksun
messages: + msg289700
2017-03-15 21:08:42vstinnersetnosy: + vstinner
messages: + msg289699
2017-03-15 21:06:17ammar2setstatus: open -> closed
resolution: not a bug
messages: + msg289698

stage: resolved
2017-03-15 20:50:48ammar2setnosy: + ammar2

messages: + msg289696
versions: + Python 3.3, Python 3.4, - Python 3.5
2017-03-15 16:35:14Oliver Etchebarne (drmad)create