Issue 29821: importing module shutil executes file 'copy.py' (original) (raw)

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:44

admin

set

github: 74007

2017-03-15 23:41:18

eryksun

set

nosy: + eryksun
messages: +

2017-03-15 21:08:42

vstinner

set

nosy: + vstinner
messages: +

2017-03-15 21:06:17

ammar2

set

status: open -> closed
resolution: not a bug
messages: +

stage: resolved

2017-03-15 20:50:48

ammar2

set

nosy: + ammar2

messages: +
versions: + Python 3.3, Python 3.4, - Python 3.5

2017-03-15 16:35:14

Oliver Etchebarne (drmad)

create