Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

how to add the path to PYTHONPATH in google colab

Writer Matthew Harrington

how to execute the following command in google colab.export PYTHONPATH=/project/pylib/src:$PYTHONPATH

!export PYTHONPATH=/project/pylib/src:$PYTHONPATHit is not affect.

2

3 Answers

Edit 2020-11-12

Using `%` instead of `!` retains any changes to all cells in the session. So, we can use that to set the environment variable `PYTHONPATH`. However, export doesn't work in colab. `%env` can be used to set the environment variable.
! echo $PYTHONPATH
%env PYTHONPATH="$/env/python:/content/gdrive/My Drive/Colab Notebooks/MNIST_Classifier/src"
! echo $PYTHONPATH

Output:

/env/python
/env/python:/content/gdrive/My Drive/Colab Notebooks/MNIST_Classifier/src

Initial Answer

The following worked for me
! echo $PYTHONPATH
import os
os.environ['PYTHONPATH'] += ":/content/gdrive/My Drive/Colab Notebooks/MNIST_Classifier/src"
! echo $PYTHONPATH

Output:

/env/python
/env/python:/content/gdrive/My Drive/Colab Notebooks/MNIST_Classifier/src

Sources:

The answer depends on why you want to do this.

For example, if you want to add the path to your current Python session so that Python's import mechanism finds modules located in that directory, you can do this:

import sys
sys.path.insert(1, "/project/pylib/src")

If you want to modify the environment variable itself (which won't affect the paths used in your current Python session) you can use the %set_env magic:

%set_env PYTHONPATH=/project/pylib/src:/env/python
2

In my colab session (tested on 8 June 2022)

!echo $PYTHONPATH

&

import os
os.environ['PYTHONPATH']

outputs /env/python


But

import sys
print(sys.path)

outputs

['', '/content', '/env/python', '/usr/lib/python37.zip', '/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages', '/usr/local/lib/python3.7/dist-packages/IPython/extensions', '/root/.ipython']

So, in scenarios where you perform !git clone

import sys
sys.path.insert(1, "/content/some_repo")
from some_package.utils import some_func #located in some_repo

will work rather than the first two options. I added /content/ because current working dir !pwd is /content/

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy