【问题标题】:bash script to run jupyter notebook in virtualenv在 virtualenv 中运行 jupyter notebook 的 bash 脚本
【发布时间】:2020-07-19 02:52:05
【问题描述】:

为了加快启动项目,我创建了一个小型 bash 脚本,它执行以下操作:

  • 接受一个参数(项目名称)
  • 移动到该项目的目录
  • 启动虚拟环境
  • 启动一个 jupyter notebook
#!/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,这确实有效。

可能是什么问题?

【问题讨论】:

  • 可以在新创建的环境中手动检查which jupyter 吗?并在新创建的环境中手动运行env?之后在你的 bash 脚本中也一样 - pipenv shell pipenv shell env jupyter notebook --ip=0.0.0.0 我在某个地方认为这是一个路径问题..

标签: bash automation jupyter-notebook jupyter


【解决方案1】:

pipenv shell 启动一个新的 shell,必须使用 exit 将其停用。在您的脚本中,调用 pipenv shell 之后的任何命令都不会在该新 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)。

【讨论】:

    【解决方案2】:

    你好,你需要添加这个

    pipenv run jupyter notebook
    

    【讨论】:

      猜你喜欢
      • 2018-08-13
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 2017-07-15
      • 2023-03-11
      相关资源
      最近更新 更多