简体   繁体   English

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

[英]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.我正在使用pipenv安装numpy进入虚拟环境,进入虚拟环境,进入Python shell。 There I import a function with an alias and it works.在那里我导入了一个带有别名的 function 并且它可以工作。 I write the exact same line to a file and use the python command to execute the file.我将完全相同的行写入文件并使用python命令执行文件。 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.它失败了,但是当我 go 回到 Python shell 并执行与之前完全相同的操作时,我也得到了错误。

Here's the terminal session.这是端子 session。 This is all on macOS Catalina.这一切都在 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.我确实看到第一个回溯显示对自制软件安装的 Python 的引用,我认为这是问题的一部分。 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?但是为什么它首先在 REPL 中起作用,在执行文件时不起作用,然后在 REPL 中不起作用,当然,我该如何解决这个问题?

~ $ 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)

>>>```

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.问题是您正在测试的文件的名称,只需使用不冲突的名称重命名(例如:random2.py),它必须工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM