我习惯了安装包的R功能,我正在尝试用ipython
做同样的事情。有时候下面的方法有效,但有时它又不起作用,我想最终找出为什么它只能工作一半的时间。
通常安装模块(例如requests
模块)我打开一个新终端后会输入以下内容:
$ sudo pip install requests
Password: *******
然后会出现一条消息,指示安装成功或已安装。
Requirement already satisfied (use --upgrade to upgrade):
requests in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Cleaning up...
这表明可以访问代码。事实上,如果我现在从终端运行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
>>>
我现在通过Alfred打开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
我已经从StackOverflow(here)上的另一个问题中获得了一些帮助,这表明我是从ipython
shell安装模块的。这给出了更令人困惑的回应:
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
这对我来说似乎很奇怪。系统上是否安装了多个版本的python?我怎么检查这个?我是否需要将ipython
指向已安装代码的位置?
答案 0 :(得分:34)
实际上有一个更优雅的解决方案。当安装pip然后在python中你也可以做这样的事情:
import pip
def install(package):
pip.main(['install', package])
install('requests')
这更容易。一旦登录了virtualenv,您就可以确保在您所在的会话中拥有所需的内容。
另一种选择是使用%%bash
魔法。
%%bash
pip install requests
如果你想要标准输出,甚至可以使用感叹号。
! pip install requests
答案 1 :(得分:7)
这就是我所做的让它发挥作用的东西;通过命令行打开iypthon并输入
import sys
sys.path
这显示了其他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]
有了这些信息,我现在知道ipython在哪里寻找可以导入的模块。所以我手动下载了请求库,将其添加到同一根目录,以便存在以下目录:
/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/requests
此文件夹包含属于请求的python模块。我现在唯一要做的就是确保ipython知道这个文件夹存在。这是通过更新sys.path完成的。
req_link = '/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/requests'
sys.path.append(req_link)
在此之后我不再有错误。
import requests
正常工作。
在重启ipython之后,我发现ipython会自动将新路径更新到sys.path
列表中。
答案 2 :(得分:0)
我在尝试安装patool时遇到了同样的问题,但事实证明这是由于安装了多个解释器,当我从ipython运行它时,它工作正常。