How can I run command inside virtualenv in shell script - bash

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

Related

Run pipenv shell as part of a bash script

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

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

Run a remote script on a remote node

Easy question.
I have a Python3 script on my remote node in the path ~/setup/inventory.py, and I am trying to execute it remotly.
So I took a look at the script resource, which is searching on my local machine for the script.
How do I get Ansible to run my remote Python3 script. Executing it normally would look like os python3 /setup/inventory.py ansible vscode bash
Use command or shell modules, depending on your case.
Something like
- name: execute my script
command: python3 /setup/inventory.py ansible vscode bash

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

Linux shell command source execute only with . call

On my ubuntu 18 server i create an .sh file like this one:
#!/bin/bash
source "/home/ubuntu/.venv36/bin/activate"
nohup python manage.py runserver &
well, i save it as startup.sh in my /var/www/web/core directory.
At this point if in console I put my working directory like this:
cd /var/www/web/core
and execute
. startup.sh
all work fine, virtualenv get created and nohup start runserver.
the problem is if from root i call directly my script like this:
/var/www/web/core/startup.sh
command source does not run, but nohup yes, and finnaly my app does not work.
How can I execute directly my sh file? I need to execute it at server startup, now I use in crontab the command:
#reboot /var/www/web/core/startup.sh
but it does not work like I explain above.

Resources