Python 从 Jupyter Notebook 中安装 pip 包不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38368318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing a pip package from within a Jupyter Notebook not working
提问by Mikhail Janowski
When I run !pip install geocoder
in Jupyter Notebook I get the same output as running pip install geocoder
in the terminal but the geocoder package is not available when I try to import it.
当我!pip install geocoder
在 Jupyter Notebook 中运行时,我得到的输出与pip install geocoder
在终端中运行时的输出相同,但是当我尝试导入地理编码器包时,它不可用。
I'm using Ubuntu 14.04, Anaconda 4.0.0 and pip 8.1.2
我使用的是 Ubuntu 14.04、Anaconda 4.0.0 和 pip 8.1.2
Installing geocoder:
安装地理编码器:
!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
Then try to import it:
然后尝试导入它:
import geocoder
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-603a981d39f2> in <module>()
----> 1 import geocoder
ImportError: No module named geocoder
I also tried shutting down the notebook and restarting it without any luck.
我还尝试关闭笔记本并重新启动它,但没有任何运气。
Edit:I found that using the terminal installs the geocoder package in /home/ubuntu/.local/lib/python2.7/site-packages
and using a notebook installs it in /usr/local/lib/python2.7/dist-packages
which is not in the path. sys.path.append('/usr/local/lib/python2.7/dist-packages')
solves the problem for the current session.
编辑:我发现使用终端安装地理编码器包/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')
解决了当前会话的问题。
So how can I permanently modify the path or tell pip where to install geocoder?
那么如何永久修改路径或告诉 pip 在哪里安装地理编码器?
回答by Ajinkya
! pip install --user <package>
The !
tells the notebook to execute the cell as a shell command.
该!
通知笔记本执行单元作为shell命令。
回答by Eponymous
In IPython (jupyter
) 7.3 and later, there is a magic %pip
and %conda
command that will install into the current kernel (rather than into the instance of Python that launched the notebook).
在 IPython ( jupyter
) 7.3 及更高版本中,有一个魔法%pip
和%conda
命令将安装到当前内核中(而不是安装到启动笔记本的 Python 实例中)。
%pip install geocoder
In earlier versions, you need to use sys to fix the problem like in the answer by FlyingZebra1
在早期版本中,您需要使用 sys 来解决问题,就像FlyingZebra1 的答案一样
import sys
!{sys.executable} -m pip install geocoder
回答by FlyingZebra1
%pip install fedex #fedex = package name
in 2019.
在 2019 年。
In older versions of conda:
在旧版本的 conda 中:
import sys
!{sys.executable} -m pip install fedex #fedex = package name
*note - you do need to import sys
*注意 - 您确实需要导入 sys
回答by Vinayak Gupta
This worked for me in Jupyter nOtebook /Mac Platform /Python 3 :
这在 Jupyter nOtebook /Mac Platform /Python 3 中对我有用:
import sys
!{sys.executable} -m pip install -r requirements.txt
回答by robinary
In jupyter notebook under python 3.6, the following line works:
在 python 3.6 下的 jupyter notebook 中,以下行有效:
!source activate py36;pip install <...>
回答by Dawid Laszuk
The problem is that pyarrow
is saved by pip
into dist-packages
(in your case /usr/local/lib/python2.7/dist-packages
). This path is skipped by Jupyter so pip
won't help.
问题是它pyarrow
被保存pip
到dist-packages
(在你的情况下/usr/local/lib/python2.7/dist-packages
)。Jupyter 跳过了此路径,因此pip
无济于事。
As a solution I suggest adding in the first block
作为解决方案,我建议在第一个块中添加
import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')
or whatever is path or python version. In case of Python 3.5 this is
或任何路径或python版本。在 Python 3.5 的情况下,这是
import sys
sys.path.append("/usr/local/lib/python3.5/dist-packages")
回答by Mattony
Alternative option :
you can also create a bash cell in jupyter using bash kernel and then pip install geocoder
. That should work
替代选项:您还可以使用 bash 内核在 jupyter 中创建一个 bash 单元,然后pip install geocoder
. 那应该工作
回答by casper
Try using some shell magic: %%sh
%%sh pip install geocoder
let me know if it works, thanks
尝试使用一些 shell 魔法:%%sh
%%sh pip install geocoder
让我知道它是否有效,谢谢
回答by Steve Ji
conda create -n py27 python=2.7 ipykernel
source activate py27
pip install geocoder
回答by elwarren
Using pip2 worked for me:
使用 pip2 对我有用:
!pip2 install geocoder
...
import geocoder
g = geocoder.google('Mountain View, CA')
g.latlng
[37.3860517, -122.0838511]