How do I install Python packages in Google's Colab?

209,357

Solution 1

You can use !setup.py install to do that.

Colab is just like a Jupyter notebook. Therefore, we can use the ! operator here to install any package in Colab. What ! actually does is, it tells the notebook cell that this line is not a Python code, its a command line script. So, to run any command line script in Colab, just add a ! preceding the line.

For example: !pip install tensorflow. This will treat that line (here pip install tensorflow) as a command prompt line and not some Python code. However, if you do this without adding the ! preceding the line, it'll throw up an error saying "invalid syntax".

But keep in mind that you'll have to upload the setup.py file to your drive before doing this (preferably into the same folder where your notebook is).

Hope this answers your question :)

Solution 2

A better, more modern, answer to this question is to use the %pip magic, like:

%pip install scipy

That will automatically use the correct Python version. Using !pip might be tied to a different version of Python, and then you might not find the package after installing it.

And in colab, the magic gives a nice message and button if it detects that you need to restart the runtime if pip updated a packaging you have already imported.

BTW, there is also a %conda magic for doing the same with conda.

Solution 3

Let's say you want to install scipy. Here is the code to install it:

!pip install scipy

Solution 4

Joining the party late, but just as a complement, I ran into some problems with Seaborn not so long ago, because CoLab installed a version with !pip that wasn't updated. In my specific case, I couldn't use Scatterplot, for example. The answer to this is below:

To install the module, all you need is:

!pip install seaborn

To upgrade it to the most updated version:

!pip install --upgrade seaborn

If you want to install a specific version

!pip install seaborn==0.9.0

I believe all the rules common to pip apply normally, so that pretty much should work.

Solution 5

To import a library that's not in Colaboratory by default, you can use !pip install or !apt-get install.

!pip install matplotlib-venn
Share:
209,357
Admin
Author by

Admin

Updated on May 11, 2021

Comments

  • Admin
    Admin almost 3 years

    In a project, I have e.g. two different packages, How can I use the setup.py to install these two packages in the Google's Colab, so that I can import the packages?

  • avigil
    avigil almost 6 years
    your answer would be easier to read if it weren't split into a list
  • Rohit Kumar
    Rohit Kumar about 5 years
    I just ran !setup.py install and it shows /bin/bash: setup.py: command not found. I have uploaded the whole package and the setup.py file exists in the same folder as the notebook
  • Ashutosh Pathak
    Ashutosh Pathak almost 5 years
    Sorry for the late response. Did you try !python setup.py install?
  • keramat
    keramat over 4 years
    It seems that it is not possible to directly provide url of setup.py from github.
  • David Parks
    David Parks over 4 years
    This is the clearest, easiest to read answer here, it needs more upvotes!
  • theProcrastinator
    theProcrastinator about 3 years
    I do not understand the difference between !pip and pip, both are giving same results i.e., installing the package without any error, mind if someone could clear that?
  • Benjamin B.
    Benjamin B. about 3 years
    @keramat: it is possible to install Python packages from GitHub, but not by referring to its setup.py. Rather, to install e.g. scipy use: !pip install git+https://github.com/scipy/scipy.git. The gist here is to use git+https in the URL. pip will try to find setup.py in the URL and install as usual.