Different numpy version in Anaconda and numpy.__version__ in IPython Shell

别等时光非礼了梦想. 提交于 2021-01-29 19:26:36

问题


I used How do I check which version of NumPy I'm using? to learn how to get the version of numpy. However, when I run conda list | grep numpy, I get:

numpy                     1.15.2           py36ha559c80_0
numpy-base                1.15.2           py36h8128ebf_0
numpydoc                  0.8.0                    py36_0

However, when I run version from IPython shell, I get:

import numpy as np
np.__version__
Out: '1.13.3'
np.version.version
Out: '1.13.3'
np.version.full_version
Out: '1.13.3'

Why are the two versions different? Which one should I trust? Thanks for any help.


Please note that I am not using venv (i.e. virtual environment). I am directly accessing Anaconda's packages. So, there is no issue about versioning.


Here's what PyCharm is showing me:


As per Conda's version information on package doesn't correspond to __version__, here's __file__ and sys.path. Please note that I have hidden my name for privacy issues.


回答1:


It seems that you have besides your python 3 environment in anaconda, another python with IPython and numpy installed.

It looks like that PyCharm and Anaconda see (correctly) the same numpy versions, while IPython which, I assume you didn't start from within your anaconda environment, sees another python installation with the older numpy. In fact, your output shows, that there is another python3.6 in C:\Users\... which doesn't belong to anaconda.

To make numpy 1.15 available in IPython you can either start IPython from within your anaconda environment by typing in the terminal (easier solution)

C:\>activate <your_anaconda_environment_name>
(<your_anaconda_environment_name>) C:\>ipython

or you make your local IPython load the modules from the anaconda environment by having a look at this answer. This will be not a recommended option in this case, given the resulting cross linkings of two python installations.




回答2:


The issue is that PyCharm reads older python version from location App-data\roaming... What I did is that in start-up script, I added the following code.

print("Correcting sys paths now...")
paths = [
'C:\\Anaconda3\\python36.zip',
 'C:\\Anaconda3\\DLLs',
 'C:\\Anaconda3\\lib',
 'C:\\Anaconda3',
 'C:\\Anaconda3\\lib\\site-packages',
 'C:\\Anaconda3\\lib\\site-packages\\win32',
 'C:\\Anaconda3\\lib\\site-packages\\win32\\lib',
 'C:\\Anaconda3\\lib\\site-packages\\Pythonwin',
 'C:\\Anaconda3\\lib\\site-packages\\IPython\\extensions',
]
import sys
for path in reversed(paths):
    sys.path.insert(0,path)
print("Completed correcting sys paths now...")
del path
del paths

Above code will force Python to read latest files from Anaconda. However, if you are using virtual environment, you would need to point to that environment.

If you want to know where is Python installed, you can run:

import os
import sys
os.path.dirname(sys.executable)

Above answer is inspired from conda python isn't using the numpy version I try install if I also specify that it should use python 2. It doesn't provide the solution. I have posted a solution above.



来源:https://stackoverflow.com/questions/52714232/different-numpy-version-in-anaconda-and-numpy-version-in-ipython-shell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!