当我在 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-packages
中。 sys.path.append('/usr/local/lib/python2.7/dist-packages')
解决了当前会话的问题。
那么如何永久修改路径或告诉 pip 在哪里安装地理编码器?
在 IPython (jupyter
) 7.3 及更高版本中,有一个神奇的 %pip
和 %conda
命令将安装到当前内核中(而不是安装到启动笔记本的 Python 实例中)。
%pip install geocoder
在早期版本中,您需要使用 sys 来解决问题,如 the answer by FlyingZebra1
import sys
!{sys.executable} -m pip install geocoder
! pip install --user <package>
!
告诉笔记本将单元格作为 shell 命令执行。
pip
实际上并不是 virtualenv 中 python 版本的安装程序。使用 %pip
魔法而不是 !pip
(shell 命令)是要走的路,正如@eponymous 的回答一样。
%pip install fedex #fedex = package name
在 2019 年。
在旧版本的 conda 中:
import sys
!{sys.executable} -m pip install fedex #fedex = package name
*注意 - 您确实需要导入 sys
在 python 3.6 下的 jupyter notebook 中,以下行有效:
!source activate py36;pip install <...>
这在 Jupyter nOtebook /Mac Platform /Python 3 中对我有用:
import sys
!{sys.executable} -m pip install -r requirements.txt
问题是 pip
将 pyarrow
保存到 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")
尝试使用一些 shell 魔法:%%sh %%sh pip install geocoder
让我知道它是否有效,谢谢
替代选项:您还可以使用 bash 内核然后 pip install geocoder
在 jupyter 中创建一个 bash 单元。那应该工作
我有同样的问题。
我发现这些说明对我有用。
# Example of installing handcalcs directly from a notebook
!pip install --upgrade-strategy only-if-needed handcalcs
参考:https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
一起使用 pip 和 conda 时可能会出现问题。结合 conda 和 pip 时,最好使用隔离的 conda 环境。只有在使用 conda 安装尽可能多的软件包之后,才应该使用 pip 安装任何剩余的软件。如果需要对环境进行修改,最好创建一个新环境,而不是在 pip 之后运行 conda。在适当的时候,conda 和 pip 要求应该存储在文本文件中。我们建议您: 仅在 conda 之后使用 pip 使用 conda 安装尽可能多的要求,然后使用 pip。只有在需要时(默认)才能使用 --upgrade-strategy 运行 Pip。不要将 pip 与 --user 参数一起使用,避免所有用户安装。
conda create -n py27 python=2.7 ipykernel
source activate py27
pip install geocoder
使用 pip2 对我有用:
!pip2 install geocoder
...
import geocoder
g = geocoder.google('Mountain View, CA')
g.latlng
[37.3860517, -122.0838511]
不定期副业成功案例分享