bash脚本在virtualenv中运行jupyter笔记本 [英] bash script to run jupyter notebook in virtualenv
问题描述
为了加快启动项目的速度,我创建了一个小的bash脚本,该脚本执行以下操作:
To speed up launching projects I created a small bash script which does the following:
- 接受参数(项目名称)
- 移动到该项目的目录
- 启动虚拟环境
- 启动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
命令.当我退出环境时,我看到错误:
It activates the environment, but does not run the jupyter
command. When I exit the environment I see the error:
line 16: jupyter: command not found
我可以在新创建的环境中手动键入jupyter notebook --ip=0.0.0.0
,并且可以正常工作.
I can manually type jupyter notebook --ip=0.0.0.0
in my newly created environment and that does work.
可能是什么问题?
推荐答案
pipenv shell
启动一个新的shell,必须使用exit
将其禁用.在脚本中,调用pipenv shell
之后的任何命令都不会在该新Shell中执行.相反,它们在关闭虚拟环境外壳程序后在同一bash外壳程序中执行.您应该使用pipenv run jupyter notebook --ip=0.0.0.0
pipenv shell
starts a new shell which must be deactivated by using exit
. In your script any commands following the call to pipenv shell
are not executed in that new shell. Instead they are executed in the same bash shell after the virtual environment shell is closed. You should use pipenv run jupyter notebook --ip=0.0.0.0
请参见 pipenv文档:
shell
将在激活virtualenv的情况下生成一个shell.可以使用exit
停用此外壳.run
将从virtualenv运行给定命令,并转发任何参数(例如$ pipenv run python
或$ pipenv run pip freeze
).
shell
will spawn a shell with the virtualenv activated. This shell can be deactivated by usingexit
.run
will run a given command from the virtualenv, with any arguments forwarded (e.g.$ pipenv run python
or$ pipenv run pip freeze
).
这篇关于bash脚本在virtualenv中运行jupyter笔记本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!