ipython install new modules

Here's what I did that made it work; open up iypthon through the command line and type

import sys
sys.path 

This shows a list of folders where other python modules are located. For me this was:

['',
 '/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:

/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.

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.path list.


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:

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.

edit

Another alternative would be to use the %%bash magic.

%%bash
pip install requests

edit2

If you want the standard output, one could even use the exclamation bang.

! pip install requests

edit3

From within ipython this is the safest installation method.

%pip install requests

This ensures that everything is installed in the virtualenv that your ipython is installed in.