1

我正在尝试使用GCP 的 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 会找到我的可执行文件。当我在笔记本中尝试时,它没有。

4

2 回答 2

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应该按预期工作。

于 2020-01-14T17:37:17.903 回答
0

另一种选择是在 jupyter notebook 启动脚本中添加路径,这样就不必每次启动 notebook 时都添加这些行:

打开终端,然后执行

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

在这个文件中,添加

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

退出并保存(ctrl+x),瞧!

于 2021-04-22T19:17:51.803 回答