首页 文章

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

提问于
浏览
16

当我在Jupyter Notebook中运行 !pip install geocoder 时,我获得与在终端中运行 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

然后尝试导入它:

import geocoder

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

ImportError: No module named geocoder

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

编辑:我发现使用终端在/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安装地理编码器的位置?

9 回答

  • 2
    ! pip install --user <package>
    

    ! 告诉笔记本将shell作为shell命令执行 .

  • 0

    在python 3.6下的jupyter笔记本中,以下行有效:

    !source activate py36;pip install <...>
    
  • 2

    这段代码对我有用(Windows 10 / Conda在python 2.x上的jupyter笔记本中安装/执行)

    import sys
    !{sys.executable} -m pip install fedex
    

    *注意 - 您需要导入sys

  • -3

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

  • 16
    conda create -n py27 python=2.7 ipykernel
    
    source activate py27
    
    pip install geocoder
    
  • 0

    替代选项:您还可以使用bash内核在jupyter中创建一个bash单元,然后使用 pip install geocoder . 这应该工作

  • 0

    问题是 pyarrowpyarrow 保存到 dist-packages (在您的情况下为 /usr/local/lib/python2.7/dist-packages ) . Jupyter跳过了这条路径,所以 pip 无济于事 .

    作为解决方案,我建议在第一个块中添加

    import sys
    sys.path.append('/usr/local/lib/python2.7/dist-packages')
    

    或者是路径或python版本 . 对于Python 3.5,这是

    import sys
    sys.path.append("/usr/local/lib/python3.5/dist-packages")
    
  • 0

    这在Jupyter nOtebook / Mac Platform / Python 3中对我有用:

    import sys
    !{sys.executable} -m pip install -r requirements.txt
    
  • 0

    使用pip2为我工作:

    !pip2 install geocoder
    ...
    import geocoder
    g = geocoder.google('Mountain View, CA')
    g.latlng
    [37.3860517, -122.0838511]
    

相关问题