bash script to run jupyter notebook in virtualenv

一世执手 提交于 2021-02-19 04:18:25

问题


To speed up launching projects I created a small bash script which does the following:

  • takes an argument (project name)
  • moves to the directory of that project
  • starts a virtual environment
  • starts a 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

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

I can manually type jupyter notebook --ip=0.0.0.0 in my newly created environment and that does work.

What may be the problem?


回答1:


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

See pipenv documentation:

  • shell will spawn a shell with the virtualenv activated. This shell can be deactivated by using exit.
  • run will run a given command from the virtualenv, with any arguments forwarded (e.g. $ pipenv run python or $ pipenv run pip freeze).



回答2:


Hi you need to add this

pipenv run jupyter notebook


来源:https://stackoverflow.com/questions/61071121/bash-script-to-run-jupyter-notebook-in-virtualenv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!