Я использую pipenv
, чтобы установить numpy
в виртуальную среду, войти в виртуальную среду, войти в оболочку Python. Там я импортирую функцию с псевдонимом, и она работает. Я записываю точно такую же строку в файл и использую команду python
для выполнения файла. Это не удается, но затем, когда я возвращаюсь в оболочку Python и делаю то же самое, что работало раньше, я также получаю ошибку там.
Вот сеанс терминала. Это все на macOS Catalina.
Я вижу, что первый трассировщик показывает ссылки на Python, установленный в homebrew, и я предполагаю, что это часть проблемы. Но почему он сначала работает в 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)
>>>```