Ned Deily <n...@python.org> added the comment:

Python is behaving as documented here. The problem is that you have a package 
named 'xml' which conflicts with the package of the same name in the standard 
library (https://docs.python.org/3/library/xml.html) which may cause conflicts 
for other packages depending on the import search order. The documentation for 
sys.path (https://docs.python.org/3/library/sys.html#sys.path) explains what 
you are seeing:

'As initialized upon program startup, the first item of this list, path[0], is 
the directory containing the script that was used to invoke the Python 
interpreter. If the script directory is not available (e.g. if the interpreter 
is invoked interactively or if the script is read from standard input), path[0] 
is the empty string, which directs Python to search modules in the current 
directory first. Notice that the script directory is inserted before the 
entries inserted as a result of PYTHONPATH."

So, in your first example, "testdriver" will be the first entry on sys.path 
because the script was invoked from there; this mean 'xml' will be imported 
from the "testdriver" directory. But, in your second example, the script is 
read from the command line via the -c argument so the first sys.path entry will 
be '' and the current directory is 'python_bug' so now your 'xml' package is no 
longer found first.

This is a common pitfall and is sometimes called the "name shadowing trap" (see 
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-name-shadowing-trap).
  The easiest and most robust solution is to avoid using package and module 
names that conflict with those in the standard library.

----------
nosy: +ned.deily
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44132>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to