【问题标题】:Unable to import even after installing the packages即使安装了软件包也无法导入
【发布时间】:2016-06-17 18:43:00
【问题描述】:

我已经安装了 numpy、scipy、scikit learn 和其他需要的包。仍然当我尝试在 python 中导入它们时,它显示没有找到模块。

pip install -U numpy scipy scikit-learn
Requirement already up-to-date: numpy in /usr/lib/python2.7/dist-packages
Collecting scipy
  Downloading scipy-0.17.1-cp27-cp27mu-manylinux1_x86_64.whl (39.5MB)
    100% |████████████████████████████████| 39.5MB 32kB/s 
Requirement already up-to-date: scikit-learn in /home/tecsadmin/.local/lib/python2.7/site-packages
Installing collected packages: scipy
  Found existing installation: scipy 0.17.0
    DEPRECATION: Uninstalling a distutils installed project (scipy) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling scipy-0.17.0:
Exception:
Traceback (most recent call last):
  File "/home/tecsadmin/.local/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/home/tecsadmin/.local/lib/python2.7/site-packages/pip/commands/install.py", line 317, in run
    prefix=options.prefix_path,
  File "/home/tecsadmin/.local/lib/python2.7/site-packages/pip/req/req_set.py", line 736, in install
    requirement.uninstall(auto_confirm=True)
  File "/home/tecsadmin/.local/lib/python2.7/site-packages/pip/req/req_install.py", line 742, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/home/tecsadmin/.local/lib/python2.7/site-packages/pip/req/req_uninstall.py", line 115, in remove
    renames(path, new_path)
  File "/home/tecsadmin/.local/lib/python2.7/site-packages/pip/utils/__init__.py", line 267, in renames
    shutil.move(old, new)
  File "/usr/lib/python2.7/shutil.py", line 303, in move
    os.unlink(src)
OSError: [Errno 13] Permission denied: '/usr/lib/python2.7/dist-packages/scipy-0.17.0.egg-info'
tecsadmin@BLITZ:~/.local/lib/python2.7/site-packages/sklearn/__check_build$ python
Python 2.7.5 (default, Jun 17 2016, 04:41:21) 
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>> 

更新

admin@BLITZ:~$ sudo pip install -U numpy scipy scikit-learn
[sudo] password for tecsadmin: 
The directory '/home/tecsadmin/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/tecsadmin/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already up-to-date: numpy in /usr/lib/python2.7/dist-packages
Collecting scipy
  Downloading scipy-0.17.1-cp27-cp27mu-manylinux1_x86_64.whl (39.5MB)
    100% |████████████████████████████████| 39.5MB 32kB/s 
Requirement already up-to-date: scikit-learn in ./.local/lib/python2.7/site-packages
Installing collected packages: scipy
  Found existing installation: scipy 0.17.0
    DEPRECATION: Uninstalling a distutils installed project (scipy) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling scipy-0.17.0:
      Successfully uninstalled scipy-0.17.0
Successfully installed scipy-0.17.1
tecsadmin@BLITZ:~$ python
Python 2.7.5 (default, Jun 17 2016, 04:41:21) 
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>> import scipy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named scipy

更新 2

tecsadmin@BLITZ:~$ pip freeze | grep numpy 
numpy==1.11.0
tecsadmin@BLITZ:~$ pip freeze | grep scipy
scipy==0.17.1
tecsadmin@BLITZ:~$ pip freeze | grep scikit-learn
scikit-learn==0.17.1
tecsadmin@BLITZ:~$ python
Python 2.7.5 (default, Jun 17 2016, 04:41:21) 
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named scipy
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named numpy

【问题讨论】:

  • 您没有安装这些库,您遇到了错误。
  • @user123 使用 sudo pip install -U numpy scipy scikit-learn 重新安装它们
  • @wind85 请检查更新
  • 你能运行 pip freeze 吗?
  • @shivsn 我没听懂你。它说要求已经满足。 To :lapinkoira 什么是 taht?

标签: python ubuntu numpy scipy scikit-learn


【解决方案1】:

不确定是否相关,但您遇到permission denied 错误,请尝试使用 sudo pip install -U numpy scipy scikit-learn

要仔细检查您的安装运行pip freeze

$ pip freeze | grep numpy 
$ numpy==1.9.2

这样你才能真正检查它是否已成功安装

另外我建议你使用 python 虚拟环境https://virtualenv.pypa.io/en/stable/ 这样你就不必使用 sudo 权限了

【讨论】:

  • 运行 pip freeze,它应该显示 numpy 和 scipy 已安装,让我们检查一下
  • 好的,你能运行一个'which python'
【解决方案2】:

你没有安装你得到错误 13 权限被拒绝的库。使用命令重新安装它们:

sudo pip install numpy scipy scikit-learn

【讨论】:

  • 我是用 Sudo 安装的,还是报同样的错误
  • @user123 你的机器上有多个站点包。您可以设置一个虚拟环境(强烈建议)并在那里安装您需要的所有软件包,或者使用 pip install --force-reinstall 软件包名称重新安装当前软件包。
猜你喜欢
  • 2017-10-21
  • 2020-06-21
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-27
  • 2021-07-28
相关资源
最近更新 更多