Run pipenv shell as part of a bash script - bash

I created a bash script to scaffold boilerplates for several apps (React, Next.js, django, etc).
In part of my django_install() function, I run the following (reduced here):
mkdir "$app_name"
cd ./"$app_name" || exit 0
gh repo clone <my-repo-boilerplate> .
rm -rf .git
pipenv install
pipenv install --dev
exit 0
I would also like to execute pipenv shell and some commands that need to run inside that virtual environment, as my boilerplate has some custom scripts that I'd like to run to automatise the script completely.
I understand I cannot just run pipenv shell or python manage.py [etc...] in my bash script.
How could I achieve it?

I think you can use pipenv run for that. E.g.:
pipenv run python manage.py [etc...]
Which will run python manage.py within the virtual environment created my pipenv.
https://pipenv.pypa.io/en/latest/cli/#pipenv-run

Related

Launch bash subshell and run commands in a script

I want to write a shell script that does the following:
Activate pipenv virtual environment
Runs mkdocs serve which starts a local dev server for my mkdocs documentation
If I do the naïve thing and put this in my script:
cd <my-docs-directory>
pipenv shell
mkdocs serve
it fails because pipenv shell "launches a subshell in the virtual environment". I need to pass the mkdocs serve command into the virtual shell (and preferably land in that same shell after running the script ).
Thanks in advance!
Answer
Philippe's answer works. Here's why.
pipenv run bash -c 'mkdocs serve ; exec bash --norc'
Pipenv allows you to run a command in the virtual environment without launching a shell:
$ pipenv run <insert command here>
bash -c <insert command here> allows you to pass a command to bash to execute
$ bash -c "echo hello"
hello
exec serves to replace current shell process with a command, so that parent goes a way and child owns pid. Here's a related question on AskUbuntu.
You can use this command :
pipenv run bash -c 'mkdocs serve ; exec bash --norc'

bash script to run jupyter notebook in virtualenv

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?
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

How to run command after source env shell inside bash script

I am trying to run a command after setting up an environment. This command runs a python script which depends on the environment.
I have the following code:
#!/bin/bash
source ~/some/linux/env/shell
python test.py
However, the "python test.py" only runs after I exit the env shell.
I want to be able to run the "python test.py" inside this new shell env.
Firstly adding python running and interpreting directory.
#!/usr/bin/env python
And you have to give a code execution authority. You can give a permission below
chmod a+x test.py
Now you can run it from the command line.
./test.py

How can I run command inside virtualenv in shell script

I develop my own django-based project with pipenv.
Couple days ago I created simple bash script to speed up boring stuff.
All time I run terminal, go to the same directory and execute pipenv shell, after that I open up project in VScode code . and started python manage.py runserver for looking up my progress.
I try create script which do the same stuff but simplied, just run webber and here go (it comes from /usr/local/bin).
But I have one problem of these, I can't keep my pipenv shell running and execute python manage.py runserver at the same time. I mean when I stop Ctrl+C python server I don't receive my virtual enviroment (this happed in bash script ~ normally work fine).
However, server is start up, so it's virtual env.
I tried with pipenv run command but it doesn't get inside virtual env at all.
Script:
#!/bin/bash
cd ~/Documents/myprojects/Webber
code .
source $(pipenv --venv)/bin/activate
python manage.py runserver
My question is: How can I run command inside virtualenv in shell script and receive this subshell?
you can use the full path to the virtualenv folder instead of pipenv command. For example, if you created the virtual environment in you home directory called venv-webber:
source $HOME/venv-webber/bin/activate

bash script starting new shell and continuing to run commands [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 5 years ago.
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?
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

Resources