在虚拟环境中运行Jupyter Notebook的Bash脚本

3
为了加速启动项目,我创建了一个小的bash脚本,它执行以下操作:
  • 接收参数(项目名称)
  • 进入该项目的目录
  • 启动一个虚拟环境
  • 启动Jupyter笔记本
#!/bin/bash

if [ "$1" == "k3" ]; then
    project_path="tau-code/k3-analysis/"
fi

codepath="/media/peter/somedrive/code"
full_path="$codepath/$project_path"

# Go to directory of project
cd $full_path

# Start environment & notebook if available
pipenv shell
jupyter notebook --ip=0.0.0.0

它激活了环境,但没有运行jupyter命令。当我退出环境时,会看到错误:

第16行:jupyter: command not found

我可以在新创建的环境中手动输入jupyter notebook --ip=0.0.0.0,这样可以正常工作。

可能是什么问题?


1
你可以手动检查在新创建的环境中 which jupyter 是否存在,并手动运行 env 吗? 同样,在你的 bash 脚本中,执行 pipenv shell 后也需要进行相同操作。pipenv shell env jupyter notebook --ip=0.0.0.0我认为这可能是一个路径问题。 - mandar
2个回答

4

pipenv shell 会启动一个新的shell环境,必须使用exit命令来停止。在你的脚本中,调用pipenv shell后面的所有命令不会在新的shell环境中执行,而是在关闭虚拟环境shell之后在同一个bash shell中执行。你应该使用pipenv run jupyter notebook --ip=0.0.0.0

请参阅pipenv文档:

  • shell将启动一个带有虚拟环境的Shell。可以使用exit命令停止此Shell。
  • run将从虚拟环境中运行给定的命令,并转发任何参数(例如:$ pipenv run python$ pipenv run pip freeze)。

1

嗨,您需要添加这个

pipenv run jupyter notebook

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接