从Jupyter笔记本中安装pip包不起作用

Mik*_*ski 34 python ipython anaconda jupyter jupyter-notebook

当我!pip install geocoder在Jupyter Notebook中运行时,我获得与pip install geocoder在终端中运行相同的输出,但是当我尝试导入时,地理编码器包不可用.

我正在使用Ubuntu 14.04,Anaconda 4.0.0和pip 8.1.2

安装地理编码器:

!pip install geocoder

The directory '/home/ubuntu/.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/ubuntu/.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.
Collecting geocoder
  Downloading geocoder-1.15.1-py2.py3-none-any.whl (195kB)
    100% |????????????????????????????????| 204kB 3.2MB/s 
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): ratelim in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): click in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): decorator in /usr/local/lib/python2.7/dist-packages/decorator-4.0.10-py2.7.egg (from ratelim->geocoder)
Installing collected packages: geocoder
Successfully installed geocoder-1.15.1
Run Code Online (Sandbox Code Playgroud)

然后尝试导入它:

import geocoder

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-603a981d39f2> in <module>()
----> 1 import geocoder

ImportError: No module named geocoder
Run Code Online (Sandbox Code Playgroud)

我也尝试关闭笔记本电脑并重新启动它,没有任何运气.

编辑:我发现使用终端在/home/ubuntu/.local/lib/python2.7/site-packages中安装地理编码器包并使用笔记本将其安装在/usr/local/lib/python2.7/dist-中不在路径中的包.sys.path.append('/usr/local/lib/python2.7/dist-packages')解决了当前会话的问题.

那么如何永久修改路径或告诉pip安装地理编码器的位置?

小智 34

! pip install --user <package>
Run Code Online (Sandbox Code Playgroud)

!通知笔记本执行单元作为shell命令.

  • 这并不总是有效,因为有时使用 shell 命令调用的“pip”实际上并不是 virtualenv 中 python 版本的安装程序。使用 `%pip` 魔法而不是 `!pip` (shell 命令)是正确的方法,正如 @eponymous 的回答所示。 (4认同)
  • 问题本身提到了“!”的使用,根据我最初的理解,这个问题是为了找出即使在成功安装后地理编码器模块也无法工作的原因。 (2认同)

Epo*_*ous 21

在IPython(jupyter)7.3和更高版本中,有一个魔术%pip%conda命令可以安装到当前内核中(而不是安装到启动笔记本的Python实例中)。

%pip install geocoder
Run Code Online (Sandbox Code Playgroud)

在早期版本中,您需要使用sys来解决问题,例如FlyingZebra1的回答

import sys
!{sys.executable} -m pip install geocoder
Run Code Online (Sandbox Code Playgroud)

  • 确实是一个很好的解决方案。“安装在当前内核中”是否意味着通过此方法安装的软件包将在内核运行时可供使用。当停止并下次运行时,安装命令必须再次执行吗?正确的? (2认同)
  • @Veki 包管理器将在当前内核中*运行*,但在当前**环境**中*安装*,即包将保存到磁盘。我刚刚编辑以澄清。也许 Eponymous 误解了 [docs](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-pip): *“在当前内核中运行 [ ] 包管理器。”* (2认同)

Fly*_*ra1 9

%pip install fedex    #fedex = package name
Run Code Online (Sandbox Code Playgroud)

在2019年。

在较旧版本的conda中:

import sys
!{sys.executable} -m pip install fedex     #fedex = package name
Run Code Online (Sandbox Code Playgroud)

*注意-您确实需要导入sys


小智 0

尝试使用一些 shell 魔法:%%sh %%sh pip install geocoder 让我知道它是否有效,谢谢