12

I'm a complete noob to writing bash scripts. I'm trying to do the following:

#!/bin/bash

mkdir New_Project
cd New_Project
pipenv install ipykernel
pipenv shell
python -m ipykernel install --user --name==new-virtual-env
jupyter notebook

The problem I'm having is that after it executes pipenv shell, it starts the new shell and then doesn't execute the last two commands. When I exit the new shell it then tries to execute the remaining lines. Is there any way to get a script to run all of these commands from start to finish?

Austin
  • 6,921
  • 12
  • 73
  • 138
  • You could enter the venv with `source $(pipenv --venv)/bin/activate`. That would activate the virtual environment that you created with `pipenv install` without opening a new shell (which is what `pipenv shell` does). Then you need to use `deactivate` (not `exit`) to stop the venv. I hope that helps. – msoutopico Mar 22 '21 at 18:24

1 Answers1

13

As per the manual :

shell will spawn a shell with the virtualenv activated.

which is not what you need. Instead use run :

run will run a given command from the virtualenv, with any arguments forwarded (e.g. $ pipenv run python).

In your case, something like

pipenv run python -m ipykernel install --user --name==new-virtual-env
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
sjsam
  • 21,411
  • 5
  • 55
  • 102
  • thanks for the answer! just to clarify `pipenv run` will run any shell command i.e. `pipenv run scrapy` etc. not just python – mand Jul 20 '20 at 03:17