简体   繁体   English

bash 脚本在 virtualenv 中运行 jupyter notebook

[英]bash script to run jupyter notebook in virtualenv

To speed up launching projects I created a small bash script which does the following:为了加快启动项目,我创建了一个小的 bash 脚本,它执行以下操作:

  • takes an argument (project name)接受一个参数(项目名称)
  • moves to the directory of that project移动到该项目的目录
  • starts a virtual environment启动虚拟环境
  • starts a jupyter notebook启动一个 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

It activates the environment, but does not run the jupyter command.它激活环境,但不运行jupyter命令。 When I exit the environment I see the error:当我退出环境时,我看到了错误:

line 16: jupyter: command not found

I can manually type jupyter notebook --ip=0.0.0.0 in my newly created environment and that does work.我可以在我新创建的环境中手动输入jupyter notebook --ip=0.0.0.0并且确实有效。

What may be the problem?可能是什么问题?

pipenv shell starts a new shell which must be deactivated by using exit . pipenv shell启动一个新的 shell ,必须使用exit将其停用。 In your script any commands following the call to pipenv shell are not executed in that new shell.在您的脚本中,调用pipenv shell之后的任何命令都不会在新的 shell 中执行。 Instead they are executed in the same bash shell after the virtual environment shell is closed.而是在虚拟环境 shell 关闭后在同一个 bash shell 中执行。 You should use pipenv run jupyter notebook --ip=0.0.0.0您应该使用pipenv run jupyter notebook --ip=0.0.0.0

See pipenv documentation :请参阅pipenv 文档

  • shell will spawn a shell with the virtualenv activated. shell将在激活 virtualenv 的情况下生成 shell。 This shell can be deactivated by using exit .这个 shell 可以使用exit停用。
  • run will run a given command from the virtualenv, with any arguments forwarded (eg $ pipenv run python or $ pipenv run pip freeze ). run将从 virtualenv 运行给定命令,并转发任何 arguments(例如$ pipenv run python$ pipenv run pip freeze )。

Hi you need to add this嗨,你需要添加这个

pipenv run jupyter notebook

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM