Search code examples
pythongoogle-cloud-platformpathpipgcp-ai-platform-notebook

GCP AI Notebook %pip install outside of PATH + can't change path


I'm trying to use the GCP's AI Notebooks. However, when you %pip install a package, it gets installed outside of the system's path you can't call it from the shell. Tried to change the default path but I'm doing something wrong. Would appreciate suggestions.

Here's an example:

[1] %pip install kaggle --user
 ... Installing collected packages: kaggle
  WARNING: The script kaggle is installed in '/home/jupyter/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed kaggle-1.5.6
Note: you may need to restart the kernel to use updated packages.
#restarted the kernel
[2] import kaggle
 #works
[3] !pip show kaggle
Name: kaggle
Version: 1.5.6
...
Location: /home/jupyter/.local/lib/python3.5/site-packages
...
[4] !kaggle -v #doesn't work
/bin/sh: 1: kaggle: not found

[5] !echo $PATH
 /usr/local/cuda/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
[6] !export PATH=$PATH:/home/jupyter/.local/lib/python3.5/site-packages 
#no output
[7] !echo $PATH
/usr/local/cuda/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
# path is unchanged

When I try the same !export line in the shell, the path changes and the shell finds my executable. When I try it in the notebook, it doesn't.


Solution

  • Pip is telling you exactly what to do here:

    WARNING: The script kaggle is installed in '/home/jupyter/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

    You can use the following to append to environment variables from within Jupyter:

    os.environ['PATH'] += os.pathsep + '/home/jupyter/.local/bin'
    

    After which !kaggle -v should work as expected.