ipython install new modules
Question: 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.

Normally to install a module (like the `requests` module for example) I would
type the following after opening a fresh terminal:

    
    
    $ sudo pip install requests
    Password: ******* 
    

This would then be followed by a message indicating that the install was
succesfull 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
    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 PyLab through Alfred and it gives me an error.

    
    
    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
<http://bit.ly/1148FRL>) which suggests that I install the module from ipython
shell. This gives an even more baffeling response:

    
    
    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 ipython to the
location of the installed code?


Answer: 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
    

