ipython 安装新模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15974100/
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
ipython install new modules
提问by cantdutchthis
I am used to the R functionality of installing packages and I am trying to do the same thing with ipython. Sometimes the following method works but then again sometimes it doesn't and I would like to finally find out why it only works half the time.
我已经习惯了安装软件包的 R 功能,并且我正在尝试使用ipython. 有时以下方法有效,但有时又无效,我想最终找出为什么它只能在一半的时间内有效。
Normally to install a module (like the requestsmodule for example) I would type the following after opening a fresh terminal:
通常要安装一个模块(例如requests模块),我会在打开一个新终端后输入以下内容:
$ sudo pip install requests
Password: *******
This would then be followed by a message indicating that the install was successful or that it has already been installed.
然后会出现一条消息,指示安装成功或已经安装。
Requirement already satisfied (use --upgrade to upgrade):
requests in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Cleaning up...
Which suggests that the code can be accessed. And indeed if I run python now from the terminal it shows a good response without any errors whatsoever.
这表明可以访问代码。事实上,如果我现在从终端运行 python,它会显示出良好的响应,没有任何错误。
$ python
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:20:15)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>
I now open PyLabthrough Alfred and it gives me an error.
我现在PyLab通过阿尔弗雷德打开,它给了我一个错误。
Welcome to pylab, a matplotlib-based Python environment [backend: WXAgg].
For more information, type 'help(pylab)'.
In [1]: import requests
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/Users/vincentwarmerdam/<ipython-input-1-686486c241c8> in <module>()
----> 1 import requests
ImportError: No module named requests
I've read some help from another question on StackOverflow (here) which suggests that I install the module from ipythonshell. This gives an even more baffling response:
我已经阅读了有关 StackOverflow 的另一个问题的一些帮助(这里),这表明我从ipythonshell安装模块。这给出了更令人困惑的回应:
In [2]: !pip install requests
Requirement already satisfied (use --upgrade to upgrade): requests in
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Cleaning up...
In [3]: import requests
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/Users/vincentwarmerdam/<ipython-input-3-686486c241c8> in <module>()
----> 1 import requests
ImportError: No module named requests
This seems very strange to me. Are there multiple versions of python installed on the system? How could I check this? Do I need to point ipythonto the location of the installed code?
这对我来说似乎很奇怪。系统上是否安装了多个版本的python?我怎么能检查这个?我需要指向ipython已安装代码的位置吗?
采纳答案by cantdutchthis
actually there is a much much much more elegant solution. when pip is installed then within python you can do things like this as well:
实际上有一个更优雅的解决方案。当安装了 pip 然后在 python 中你也可以做这样的事情:
import pip
def install(package):
pip.main(['install', package])
install('requests')
which is easier. once logged into a virtualenv you can just make sure that you have what you need in the session you are in. easy.
这更容易。一旦登录到virtualenv,您就可以确保在您所在的会话中拥有您需要的东西。很容易。
edit
编辑
Another alternative would be to use the %%bashmagic.
另一种选择是使用%%bash魔法。
%%bash
pip install requests
edit2
编辑2
If you want the standard output, one could even use the exclamation bang.
如果您想要标准输出,甚至可以使用感叹号。
! pip install requests
回答by cantdutchthis
Here's what I did that made it work; open up iypthon through the command line and type
这就是我所做的使它起作用的方法;通过命令行打开 iypthon 并输入
import sys
sys.path
This shows a list of folders where other python modules are located. For me this was:
这显示了其他 python 模块所在的文件夹列表。对我来说这是:
['',
'/Library/Frameworks/Python.framework/Versions/7.3/bin',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/pandas-0.10.0-py2.7-macosx-10.5-i386.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/googlemaps-1.0.2-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/oauth-1.0.1-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/oauth2-1.5.211-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/httplib2-0.7.7-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/selenium-2.28.0-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/jellyfish-0.2.0-py2.7-macosx-10.5-i386.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/python_yelp-0.1.1-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/pymongo-2.4.2_-py2.7-macosx-10.5-i386.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/lucene_querybuilder-0.1.6-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/html2text-3.200.3-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python27.zip',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/plat-mac',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/plat-mac/lib-scriptpackages',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-old',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-dynload',
'/Users/vincentwarmerdam/Library/Python/2.7/lib/python/site-packages',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/PIL',
'/Library/Python/2.7/site-packages',
'/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/IPython/extensions]
With this information, I now knew where ipython looks for the modules that one can import. So I downloaded the requests library manually, added it to the same root directory such that the following directory exists:
有了这些信息,我现在知道 ipython 在哪里查找可以导入的模块。所以我手动下载了请求库,将其添加到相同的根目录中,以便存在以下目录:
/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/requests
This folder contains the python modules that belong to requests. The only thing I now had to do was to make sure that ipython knows that this folder exists. Which was done by updating the sys.path.
此文件夹包含属于请求的 python 模块。我现在唯一要做的就是确保 ipython 知道这个文件夹存在。这是通过更新 sys.path 完成的。
req_link = '/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/requests'
sys.path.append(req_link)
After this I no longer got the error.
在此之后,我不再收到错误。
import requests
Just works.
只是工作。
Also after restarting ipython, I found that ipython automatically updates the new path into the sys.pathlist.
同样在重新启动 ipython 后,我发现 ipython 自动将新路径更新到sys.path列表中。
回答by Dhananjay Mehta
If the new packages installed are imported on Terminal but not imported to ipython notebook then it is very likely that you have two versions of python installed on your library. Due to this there are separate site-packages directory from where packages are being imported on terminal and ipython notebook. To check if this is the case. On terminal use:
如果安装的新软件包在终端上导入但未导入到 ipython notebook,那么您的库中很可能安装了两个版本的 python。因此,在终端和 ipython 笔记本上导入包的位置有单独的 site-packages 目录。检查是否是这种情况。在终端使用:
import sys
sys.path
This will show where the python modules are located for python you using on terminal. Now, in ipython notebook use:
这将显示您在终端上使用的 python 的 python 模块所在的位置。现在,在 ipython 笔记本中使用:
import sys
sys.path
This will show where the python modules are located for python you using on terminal.
这将显示您在终端上使用的 python 的 python 模块所在的位置。
Now, if the two path are different you know you are using two different installations of python. To solve this problem, copy installed packages from site-packages directory of terminal python to site-packages directory of ipython.
现在,如果这两个路径不同,您就知道您正在使用两个不同的 python 安装。为了解决这个问题,将安装的包从终端python的site-packages目录复制到ipython的site-packages目录。
回答by Sinux1
I had this same problem when trying to install patool, but it turned out it was due to more than one interpreter installed, and when I ran it from ipython it worked.
我在尝试安装 patool 时遇到了同样的问题,但事实证明这是由于安装了多个解释器,当我从 ipython 运行它时,它起作用了。

