user9676571
user9676571

Reputation: 145

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.

Upvotes: 1

Views: 883

Answers (2)

CFLJ
CFLJ

Reputation: 1

Another option is to add the path at in the jupyter notebook startup script, so that you don't have to add these lines every time you start a notebook:

Open a Terminal, then do

cd ~/.ipython/profile_default/startup/
nano startup.py

In this file, add

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

Exit and save (ctrl+x) and voila!

Upvotes: 0

Dustin Ingram
Dustin Ingram

Reputation: 21580

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.

Upvotes: 1

Related Questions