简体   繁体   English

为什么 numpy import 的行为不同?

[英]Why does numpy import behave differently?

I used the following commands to install numpy in my python3 virtual environment on Ubuntu 16.04 LTS machine.我使用以下命令在 Ubuntu 16.04 LTS 机器上的 python3 虚拟环境中安装 numpy。

My goal is to use python 3.5 by default in my venv and learn numpy.我的目标是在我的 venv 中默认使用 python 3.5 并学习 numpy。 I shouldn't have to explicitly use python3 .我不应该显式使用python3 I feel there's some overlap/error which could be a bigger issue if ignored now.我觉得有一些重叠/错误,如果现在忽略它可能是一个更大的问题。 Also, I don't have python 2.x installed in my virtual environment but I have it at system level.另外,我的虚拟环境中没有安装 python 2.x,但我在系统级别安装了它。

The commands python3 -V and python -V show the same version and both are located at same path.命令python3 -Vpython -V显示相同的版本,并且都位于相同的路径。 Why does the last command work but the second to last doesn't work?为什么最后一个命令有效,但倒数第二个命令无效?

~/proj1$ virtualenv --no-site-packages -p python3 venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/$USER/proj1/venv/bin/python3
Also creating executable in /home/$USER/proj1/venv/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
~/proj1$ source venv/bin/activate
(venv) ~/proj1$ which pip
/home/$USER/proj1/venv/bin/pip
(venv) ~/proj1$ pip -V
pip 20.0.2 from /home/$USER/proj1/venv/lib/python3.5/site-packages/pip (python 3.5)
(venv) ~/proj1$ pip install numpy
Collecting numpy
  Using cached numpy-1.18.1-cp35-cp35m-manylinux1_x86_64.whl (19.9 MB)
Installing collected packages: numpy
Successfully installed numpy-1.18.1
(venv) ~/proj1$ python -V
Python 3.5.2
(venv) ~/proj1$ python3 -V
Python 3.5.2
(venv) ~/proj1$ which python
/home/$USER/proj1/venv/bin/python
(venv) ~/proj1$ which python3
/home/$USER/proj1/venv/bin/python3
(venv) ~/proj1$ python -c "import numpy"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'numpy'
(venv) ~/proj1$ python3 -c "import numpy"
(venv) ~/proj1$ 

The problem has nothing to do with numpy per se.这个问题与numpy本身无关。 Rather, what's happening is that the alias python='/usr/bin/python3' prevents your shell from finding the python executable that's first in your execution path (ie, the executable with the path given by " which python "), which messes with your virtual environment setup.相反,发生的事情是别名python='/usr/bin/python3'阻止您的 shell 找到在您的执行路径中首先出现的python可执行文件(即,具有“ which python ”给出的路径的可执行文件),这会造成混乱使用您的虚拟环境设置。 Because of that alias,因为那个别名,

python -c "import numpy"

is interpreted as被解释为

/usr/bin/python3 -c "import numpy"

Since you installed numpy in a virtual environment, the system-wide Python 3 installation in /usr/bin has by design no knowledge of that numpy installation, so you get that ImportError .由于您在虚拟环境中安装了numpy ,因此/usr/bin的系统范围的 Python 3 安装在设计上不知道该numpy安装,因此您会得到ImportError

If, on the other hand, you were to run另一方面,如果你要跑

unalias python
python -c "import numpy"

then python would be taken to be /home/$USER/proj1/venv/bin/python , provided that you had already sourced /home/$USER/proj1/venv/bin/activate , of course, and things would work as you expect.那么python将被视为/home/$USER/proj1/venv/bin/python ,前提是你已经获得了/home/$USER/proj1/venv/bin/activate ,当然,事情会像你一样工作预计。

The moral here is “don't use which ”.这里的寓意是“不要使用which ”。 bash (which almost everyone uses now) has a builtin command type that shows how a command is interpreted; bash (现在几乎每个人都在使用)有一个内置的命令type ,用于显示如何解释命令; in particular, type -a python here would show you that it would be your virtual environment's python , but is in fact aliased to run the one from /usr/bin that is the same version but doesn't have the same packages installed (because of course the virtual environment's directories aren't on its sys.path ).特别是, type -a python在这里会告诉你,这是您的虚拟环境的python ,但实际上别名运行从一个/usr/bin是相同的版本,但没有安装相同的程序包(因为当然,虚拟环境的目录不在其sys.path )。

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

相关问题 为什么 numpy.where 在尝试在数组中查找 None 元素时“== None”和“is None”的行为不同? - Why does numpy.where behave differently for “== None” and “is None” when trying to find None elements in array? 为什么这个上下文管理器与dict理解有不同的表现? - Why does this contextmanager behave differently with dict comprehensions? 为什么 pyzmq 订阅者与 asyncio 的行为不同? - Why does pyzmq subscriber behave differently with asyncio? 为什么 groupby 操作的行为不同 - Why does groupby operations behave differently 为什么 Python 3 for loop output 和行为不同? - Why does Python 3 for loop output and behave differently? 为什么numpy.dot会以这种方式表现? - Why does numpy.dot behave in this way? 为什么numpy.convolve不能表现出关联性? - Why does numpy.convolve not behave associatively? 为什么 python/numpy += 与 arrays 的行为类似? - Why does python/ numpy += behave like this with arrays? 为什么这个argparse代码在Python 2和3之间表现不同? - Why does this argparse code behave differently between Python 2 and 3? 为什么在包装时sys.excepthook会有不同的行为? - Why does sys.excepthook behave differently when wrapped?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM