简体   繁体   中英

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.

My goal is to use python 3.5 by default in my venv and learn numpy. I shouldn't have to explicitly use 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.

The commands python3 -V and python -V show the same version and both are located at same path. 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. 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. 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 .

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.

The moral here is “don't use which ”. bash (which almost everyone uses now) has a builtin command type that shows how a command is interpreted; 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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