bash脚本在virtualenv中运行jupyter笔记本

时间:2020-04-07 00:45:37

标签: bash automation jupyter-notebook jupyter

为了加快启动项目的速度,我创建了一个小的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命令。退出环境时,我看到错误:

line 16: jupyter: command not found

我可以在新创建的环境中手动输入jupyter notebook --ip=0.0.0.0,这确实有效。

可能是什么问题?

2 个答案:

答案 0 :(得分:4)

pipenv shell启动一个新的shell,必须使用exit将其禁用。在脚本中,调用pipenv shell之后的任何命令都不会在该新Shell中执行。相反,它们在关闭虚拟环境外壳程序后在同一bash shell中执行。您应该使用pipenv run jupyter notebook --ip=0.0.0.0

请参见pipenv documentation

  • shell将在激活virtualenv的情况下生成一个shell。可以使用exit停用此shell。
  • run将通过virtualenv运行给定命令,并转发任何参数(例如$ pipenv run python$ pipenv run pip freeze)。

答案 1 :(得分:1)

您好,您需要添加

pipenv run jupyter notebook
相关问题