解决已安装模块无法import的问题

荒凉一梦 提交于 2020-02-04 04:43:05

问题解决

系统:windows 10

工具:python3.7版本,pycharm,anaconda(电脑之前装过python官方自带的编辑器)

问题:numpy无法import

首先,检查自己pycharm的解释器对应的环境在哪儿,以及里面有没有安装numpy包。这里我对应在base环境下。

在anaconda prompt窗口输入conda list(这是检查base环境下的包)【conda -n xxx(环境名) list检查指定环境下的包】,检查后发现是有numpy包的。

然后,在anaconda prompt窗口中,输入conda uninstall numpy,卸载base环境下的numpy【同理,conda uninstall -n xxx(环境名) numpy】,然后conda install numpy重装,发现还是无法import。

-------------好吧,我再尝试一下-------------

在anaconda prompt窗口中,改用pip命令卸载和安装numpy。

先查一下numpy的信息

(base) C:\Users\Christine Yang>pip show numpy
Name: numpy
Version: 1.16.5
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: None
License: BSD
Location: c:\anaconda\lib\site-packages
Requires:
Required-by: torchvision, tables, statsmodels, seaborn, scikit-learn, PyWavelets, pytest-doctestplus, pytest-arraydiff, patsy, pandas, numexpr, numba, mkl-random, mkl-fft, matplotlib, imageio, h5py, Bottleneck, bokeh, bkcharts, astropy

然后卸载重装

(base) C:\Users\Christine Yang>pip uninstall numpy
Uninstalling numpy-1.16.5:
  Would remove:
    c:\anaconda\lib\site-packages\numpy
    c:\anaconda\lib\site-packages\numpy-1.16.5-py3.7.egg-info
    c:\anaconda\scripts\f2py-script.py
    c:\anaconda\scripts\f2py.exe
Proceed (y/n)? y
  Successfully uninstalled numpy-1.16.5

(base) C:\Users\Christine Yang>pip install numpy
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/a9/38/f6d6d8635d496d6b4ed5d8ca4b9f193d0edc59999c3a63779cbc38aa650f/numpy-1.18.1-cp37-cp37m-win_amd64.whl (12.8MB)
     |████████████████████████████████| 12.8MB 7.0kB/s
Installing collected packages: numpy
Successfully installed numpy-1.18.1

为确保安装位置正确,再show一下:

(base) C:\Users\Christine Yang>pip show numpy
Name: numpy
Version: 1.18.1
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: None
License: BSD
Location: c:\anaconda\lib\site-packages
Requires:
Required-by: torchvision, tables, statsmodels, seaborn, scikit-learn, PyWavelets, pytest-doctestplus, pytest-arraydiff, patsy, pandas, numexpr, numba, mkl-random, mkl-fft, matplotlib, imageio, h5py, Bottleneck, bokeh, bkcharts, astropy

最后到pycharm里面刷新一下环境,运行相关代码就成功了!!(激动)

----------------------------------------------------------------------------------------------

这也让我开始思考

pip和conda命令的区别

网上查看了相关博客,主要的解释是:pip允许在任何环境中安装python包,conda允许在conda环境中安装任何语言包。

但这无法解释我这次遇到的情况。在anaconda prompt里面运行pip的时候,因为已经默认是在anaconda的基础环境下,所以pip的安装卸载针对的也是anaconda目录下的包。

为此,我重新尝试了conda install 的命令,想看看是否是两种命令搜索包的途径和来源不一样。

(base) C:\Users\Christine Yang>conda install numpy
Collecting package metadata (current_repodata.json): done
Solving environment: /
The environment is inconsistent, please check the package plan carefully
The following packages are causing the inconsistency:

  - defaults/win-64::anaconda==2019.10=py37_0
  - defaults/win-64::astropy==3.2.1=py37he774522_0
  - defaults/win-64::bkcharts==0.2=py37_0
  - defaults/win-64::bokeh==1.3.4=py37_0
  - defaults/win-64::bottleneck==1.2.1=py37h452e1ab_1
  - defaults/noarch::dask==2.5.2=py_0
  - defaults/win-64::h5py==2.9.0=py37h5e291fa_0
  - defaults/win-64::imageio==2.6.0=py37_0
  - defaults/win-64::matplotlib==3.1.1=py37hc8f65d3_0
  - defaults/win-64::mkl_fft==1.0.14=py37h14836fe_0
  - defaults/win-64::mkl_random==1.1.0=py37h675688f_0
  - defaults/win-64::numba==0.45.1=py37hf9181ef_0
  - defaults/win-64::numexpr==2.7.0=py37hdce8814_0
  - defaults/win-64::numpy==1.16.5=py37h19fb1c0_0
  - defaults/win-64::pandas==0.25.1=py37ha925a31_0
  - defaults/win-64::patsy==0.5.1=py37_0
  - defaults/win-64::pytables==3.5.2=py37h1da0976_1
  - defaults/win-64::pytest-arraydiff==0.3=py37h39e3cac_0
  - defaults/win-64::pytest-astropy==0.5.0=py37_0
  - defaults/noarch::pytest-doctestplus==0.4.0=py_0
  - pytorch/win-64::pytorch==1.4.0=py3.7_cuda101_cudnn7_0
  - defaults/win-64::pywavelets==1.0.3=py37h8c2d366_1
  - defaults/win-64::scikit-image==0.15.0=py37ha925a31_0
  - defaults/win-64::scikit-learn==0.21.3=py37h6288b17_0
  - defaults/win-64::scipy==1.3.1=py37h29ff71c_0
  - defaults/win-64::seaborn==0.9.0=py37_0
  - defaults/win-64::statsmodels==0.10.1=py37h8c2d366_0
  - pytorch/win-64::torchvision==0.5.0=py37_cu101
done

## Package Plan ##

  environment location: C:\Anaconda

  added / updated specs:
    - numpy


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    _anaconda_depends-2019.03  |           py37_0           6 KB
    anaconda-custom            |           py37_1           3 KB
    numpy-base-1.16.5          |   py37hc3f5095_0         3.3 MB
    ------------------------------------------------------------
                                           Total:         3.3 MB

The following NEW packages will be INSTALLED:

  _anaconda_depends  pkgs/main/win-64::_anaconda_depends-2019.03-py37_0
  numpy-base         pkgs/main/win-64::numpy-base-1.16.5-py37hc3f5095_0

The following packages will be UPDATED:

  ca-certificates                               2019.8.28-0 --> 2020.1.1-0
  certifi                                  2019.9.11-py37_0 --> 2019.11.28-py37_0
  openssl                                 1.1.1d-he774522_2 --> 1.1.1d-he774522_3

The following packages will be DOWNGRADED:

  anaconda                                   2019.10-py37_0 --> custom-py37_1

从上面的信息中,发现两个问题:一是报告我的环境是不一致inconsistent的,二是其准备安装的包的版本是numpy-base-1.16.5          |   py37hc3f5095_0,除此之外还有anaconda的相关包。

分析了半天,还是不知道为啥pip能成功conda就不行……(菜鸡的我),之后可以在现有的环境上找一下原因。

关于环境不一致

stackoverflow上有人解释其原因主要是由于包的版本不同,以及依赖包的冲突。

提出的解决方法有:

  • conda update --all
  • 把对应的包卸载重装,conda uninstall xxx(系统提示包的名称)和conda install xxx
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!