bash script to run jupyter notebook in virtualenv

Viewed 1755

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?

2 Answers

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).

Hi you need to add this

pipenv run jupyter notebook
Related