Issue
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 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
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) which suggests that I install the module from ipython
shell. This gives an even more baffling 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?
Solution
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.
Answered By - cantdutchthis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.