Python interactive shell running copy.py on current directory?
Question: I have a script named `copy.py` on my current directory with the following
content:

    
    
    #!/usr/bin/env python3
    
    print("Ahoy, matey!")
    

If I run Python interactive shell and do some action which raise an exception
(e.g. referring to non-existent variable), to my surprise, the sentence "Ahoy,
matey!" got printed.

When I rename the `copy.py` script to anything else, e.g. `script.py`, it no
longer behaves like that. My question is, why the interactive shell have to
call `copy.py` on error? Is this behavior expected and/or documented
somewhere?

Thanks!


Answer: When Python imports a module, the [module search path
order](http://docs.python.org/3.3/tutorial/modules.html#the-module-search-
path) is:

  * the directory containing the input script (or the current directory).
  * PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  * the installation-dependent default.

Now, when raising an exception, Python runs specific code. Any code that tries
to import the `copy` Python module will instead import your module and print
the string in it. This is why you shouldn't use names that are also names of
standard Python modules.

