Search code examples
pythonnumpypipenv

Why does importing a numpy function first work in a Python shell, then not work in a Python file?


I'm using pipenv to install numpy into a virtual environment, enter the virtual environment, entering the Python shell. There I import a function with an alias and it works. I write the exact same line to a file and use the python command to execute the file. It fails, but then when I go back into the Python shell and do the exact same thing that worked before, I get the error there also.

Here's the terminal session. This is all on macOS Catalina.

I do see that the first traceback is showing references to the homebrew-installed Python, and I assume that's part of the problem. But why does it first work in the REPL, not work when executing the file, then not work in the REPL, and, of course, how do I fix this?

~ $ mkdir test_dir; cd test_dir

test_dir $ pipenv install numpy
Creating a virtualenv for this project…
Pipfile: /Users/chuck/test_dir/Pipfile
Using /usr/local/opt/python/bin/python3.7 (3.7.4) to create virtualenv…
⠹ Creating virtual environment...Already using interpreter /usr/local/opt/python/bin/python3.7
Using base prefix '/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/bin/python3.7
Also creating executable in /Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/bin/python
Installing setuptools, pip, wheel...
done.

✔ Successfully created virtual environment!
Virtualenv location: /Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW
Creating a Pipfile for this project…
Installing numpy…
Adding numpy to Pipfile's [packages]…
✔ Installation Succeeded
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success!
Updated Pipfile.lock (2cfc5e)!
Installing dependencies from Pipfile.lock (2cfc5e)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1/1 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

test_dir $ pipenv shell
Launching subshell in virtual environment…

test_dir $  . /Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/bin/activate
(test_dir)
test_dir $ which python
/Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/bin/python
(test_dir)
test_dir $ python
Python 3.7.4 (default, Oct 12 2019, 19:06:48)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> from numpy.random import choice as weighted_choice

>>> exit()
(test_dir)
test_dir $ echo "from numpy.random import choice as weighted_choice" > random.py
(test_dir)
test_dir $ ls -l
total 24
-rw-r--r--  1 chuck  staff   150 Oct 18 15:24 Pipfile
-rw-r--r--  1 chuck  staff  2499 Oct 18 15:24 Pipfile.lock
-rw-r--r--  1 chuck  staff    51 Oct 18 15:25 random.py
(test_dir)
test_dir $ cat random.py
from numpy.random import choice as weighted_choice
(test_dir)
test_dir $ python random.py
Traceback (most recent call last):
  File "bit_generator.pyx", line 40, in numpy.random.bit_generator
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/secrets.py", line 20, in <module>
    from random import SystemRandom
  File "/Users/chuck/test_dir/random.py", line 1, in <module>
    from numpy.random import choice as weighted_choice
ImportError: cannot import name 'choice' from 'numpy.random' (/Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/lib/python3.7/site-packages/numpy/random/__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "random.py", line 1, in <module>
    from numpy.random import choice as weighted_choice
  File "/Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/lib/python3.7/site-packages/numpy/__init__.py", line 150, in <module>
    from . import random
  File "/Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/lib/python3.7/site-packages/numpy/random/__init__.py", line 181, in <module>
    from . import _pickle
  File "/Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/lib/python3.7/site-packages/numpy/random/_pickle.py", line 1, in <module>
    from .mtrand import RandomState
  File "mtrand.pyx", line 9, in init numpy.random.mtrand
  File "mt19937.pyx", line 1, in init numpy.random.mt19937
  File "bit_generator.pyx", line 43, in init numpy.random.bit_generator
  File "/Users/chuck/test_dir/random.py", line 1, in <module>
    from numpy.random import choice as weighted_choice
ImportError: cannot import name 'choice' from 'numpy.random' (/Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/lib/python3.7/site-packages/numpy/random/__init__.py)
(test_dir)
test_dir $ python
Python 3.7.4 (default, Oct 12 2019, 19:06:48)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> from numpy.random import choice as weighted_choice
Traceback (most recent call last):
  File "bit_generator.pyx", line 40, in numpy.random.bit_generator
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/secrets.py", line 20, in <module>
    from random import SystemRandom
  File "/Users/chuck/test_dir/random.py", line 1, in <module>
    from numpy.random import choice as weighted_choice
ImportError: cannot import name 'choice' from 'numpy.random' (/Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/lib/python3.7/site-packages/numpy/random/__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/lib/python3.7/site-packages/numpy/__init__.py", line 150, in <module>
    from . import random
  File "/Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/lib/python3.7/site-packages/numpy/random/__init__.py", line 181, in <module>
    from . import _pickle
  File "/Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/lib/python3.7/site-packages/numpy/random/_pickle.py", line 1, in <module>
    from .mtrand import RandomState
  File "mtrand.pyx", line 9, in init numpy.random.mtrand
  File "mt19937.pyx", line 1, in init numpy.random.mt19937
  File "bit_generator.pyx", line 43, in init numpy.random.bit_generator
  File "/Users/chuck/test_dir/random.py", line 1, in <module>
    from numpy.random import choice as weighted_choice
ImportError: cannot import name 'choice' from 'numpy.random' (/Users/chuck/.local/share/virtualenvs/test_dir-jyRsrdMW/lib/python3.7/site-packages/numpy/random/__init__.py)

>>>```

Solution

  • The problem is the name of the file you're testing with, just rename with a non-conflicting name (eg: random2.py) and it must work.