我正在尝试使用GCP's AI Notebooks。但是,当%pip install
软件包安装在系统路径之外时,您将无法从shell调用它。试图更改默认路径,但我做错了。不胜感激建议。
这是一个例子:
[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
当我在shell中尝试相同的!export
行时,路径会更改,并且shell会找到我的可执行文件。当我在笔记本电脑上尝试时,不会。
答案 0 :(得分:1)
Pip会告诉您确切的操作方法:
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.
您可以使用以下内容从Jupyter内附加到环境变量:
os.environ['PATH'] += os.pathsep + '/home/jupyter/.local/bin'
之后!kaggle -v
应该可以正常工作。
答案 1 :(得分:0)
另一种选择是在 jupyter notebook 启动脚本中添加路径 at,这样就不用每次启动 notebook 时都添加这些行:
打开一个终端,然后执行
cd ~/.ipython/profile_default/startup/
nano startup.py
在这个文件中添加
import os
os.environ['PATH'] += os.pathsep + '/home/jupyter/.local/bin'
退出并保存(ctrl+x),瞧!