[New-bugs-announce] [issue34414] Absolute imports conflict with local files

Jonathan Hadida report at bugs.python.org
Thu Aug 16 12:45:25 EDT 2018


New submission from Jonathan Hadida <ariel.hadida at gmail.com>:

This submission follows a post on StackOverflow: https://stackoverflow.com/q/51878397/472610

I have reproduced the unexpected behaviour with multiple python versions, either with a Homebrew install, or using Anaconda/Miniconda. Note that comments to the post on StackOverflow indicate that this behaviour could only be reproduced on Windows and Linux using conda with late versions of Python.


THE ISSUE
---------

Absolute imports seem to conflict with local files (not in any module). 
This is at odds with the documented behaviour (https://docs.python.org/3/tutorial/modules.html#the-module-search-path):

"When a module named spam is imported, the interpreter first searches for a built-in module with that name."


STEPS TO REPRODUCE
------------------

STEP 1:
On OSX, use either:

 - A Homebrew-installed version (e.g. /usr/local/bin/python2 or 3)
 - A Miniconda2 or 3 installation
 - An Anaconda3 v5.2.0 installation (not tested with other versions, nor Anaconda2)

NOTE: in cases 1 and 2, you need to install numpy manually.


STEP 2:
Create a directory structure as follows:

  .
  └── foo
      ├── a.py
      └── math.py

  1 directory, 2 files

where a.py contains "import numpy", and math.py contains "x++" (intentionally invalid).
For example, the following bash code sets this up in a temporary folder:

  D=$(mktemp -d)
  mkdir "$D/foo"
  echo "import numpy" >| "$D/foo/a.py"
  echo "x++" >| "$D/foo/math.py"


STEP 3:
Go to that directory (the one containing folder "foo"), and run:

  <PythonExecutableFromStep1> foo/a.py

The previous command causes the following error, for example using /usr/local/bin/python3 (Homebrew):

Traceback (most recent call last):
  File "foo/a.py", line 1, in <module>
    import numpy
  File "/usr/local/lib/python3.6/site-packages/numpy/__init__.py", line 142, in <module>
    from . import add_newdocs
  File "/usr/local/lib/python3.6/site-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/local/lib/python3.6/site-packages/numpy/lib/__init__.py", line 3, in <module>
    import math
  File "/private/var/folders/j7/kd8mc69j25j0yw50q_08wmlm0000gt/T/tmp.FfJzdVuG/foo/math.py", line 1
    x++
      ^
SyntaxError: invalid syntax


PROBLEM:
The statement "import math" in numpy/lib/__init__.py should not resolve to foo/math.py, but rather, it should find the standard module "math".


ADDITIONAL INFO
---------------

Although I do not know what this list should look like, I would expect the list of builtin modules to be larger than this:

> python3 -c "import sys; print(sys.builtin_module_names)"
('_ast', '_codecs', '_collections', '_functools', '_imp', '_io', '_locale', '_operator', '_signal', '_sre', '_stat', '_string', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', 'atexit', 'builtins', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'posix', 'pwd', 'sys', 'time', 'xxsubtype', 'zipimport')

> python2 -c "import sys; print sys.builtin_module_names"
('__builtin__', '__main__', '_ast', '_codecs', '_sre', '_symtable', '_warnings', '_weakref', 'errno', 'exceptions', 'gc', 'imp', 'marshal', 'posix', 'pwd', 'signal', 'sys', 'thread', 'xxsubtype', 'zipimport')

----------
components: macOS
messages: 323609
nosy: jhadida, ned.deily, ronaldoussoren
priority: normal
severity: normal
status: open
title: Absolute imports conflict with local files
type: behavior
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34414>
_______________________________________


More information about the New-bugs-announce mailing list