gpt4 book ai didi

python - 为什么导入 numpy 函数首先在 Python shell 中起作用,然后在 Python 文件中不起作用?

转载 作者:行者123 更新时间:2023-12-01 07:05:45 24 4
gpt4 key购买 nike

我使用pipenvnumpy安装到虚拟环境中,进入虚拟环境,进入Python shell。我在那里导入一个带有别名的函数并且它可以工作。我将完全相同的行写入文件并使用 python 命令来执行该文件。它失败了,但是当我返回到 Python shell 并执行与之前完全相同的操作时,我也得到了错误。

这是终端 session 。这一切都在 macOS Catalina 上。

我确实看到第一个回溯显示了对自制安装的 Python 的引用,我认为这是问题的一部分。但是为什么它首先在 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)

>>>```

最佳答案

问题在于您正在测试的文件的名称,只需使用不冲突的名称重命名(例如:random2.py),它就一定可以工作。

关于python - 为什么导入 numpy 函数首先在 Python shell 中起作用,然后在 Python 文件中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58458935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com