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

PythonPipJupyter NotebookGoogle

Python Problem Overview

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

Python Solutions

Solution 1 - Python

You can use ! 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 file to your drive before doing this (preferably into the same folder where your notebook is).

Hope this answers your question :)

Solution 2 - Python

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 - Python

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

!pip install scipy

Solution 4 - Python

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 - Python

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

Solution 6 - Python

  1. Upload to drive.
  2. Mount the drive.
  3. Get the path of
  4. !python PATH install.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLin JianjieView Question on Stackoverflow
Solution 1 - PythonAshutosh PathakView Answer on Stackoverflow
Solution 2 - PythonDoug BlankView Answer on Stackoverflow
Solution 3 - PythonRaviView Answer on Stackoverflow
Solution 4 - PythonmarcogemaqueView Answer on Stackoverflow
Solution 5 - PythoniacobView Answer on Stackoverflow
Solution 6 - PythonkeramatView Answer on Stackoverflow