bash script to run jupyter notebook in virtualenv - bash

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

Related

`command not found` when using Bash Script to Install & Run a Software

I am trying to create a Bash script that downloads a .sh file, runs it to install a program (Anaconda for Python), then run the command conda init to activate the default Anaconda environment base
However, running my bash script as shown
# !#/bin/bash
wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
bash Anaconda3-*-Linux-x86_64.sh -b
echo 'export PATH=~/anaconda3/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
conda init
conda env create -f environment.yml
gives the error
./foo.sh: line 6: conda: command not found
./foo.sh: line 7: conda: command not found
Why is conda not found when trying to use it from within the Bash script, but is found if I relogin to the terminal?
How can we fix the Bash script so that using conda works?

'Conda activate base' fails to load when executed from Matlab on Ubuntu (18.04). Error: conda: command not found

My Conda working perfectly from the ubuntu terminal. I can easily activate the environment I need using the following command
$ conda activate base
and it loads the base environment on the terminal.
But here, in MATLAB, when I try to activate conda environment using the following command it returns an error
>> system('conda activate base')
/bin/bash: conda: command not found
Based on other posts with similar discussions, I have tried the following but to no luck
>> system('/home/anirudha/anaconda3/bin/conda activate base')
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
>> system('conda init bash');
/bin/bash: conda: command not found
I have also tried solutions from this but to no success again.
My purpose is to execute a few gdal functions from the command line through a MATLAB script. I have tried doing this on my windows PC, and everything works perfectly. But not on my Linux station. Am I missing something?

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

Run Jupyter Notebook in the Background on Docker

I am trying to run a jupyter notebook in the background without printing anything to the console. I found this solution in a question for bash:
jupyter notebook &> /dev/null &
But I am running jupyter in a docker container and want it to start in the background via CMD. How can I do the same in sh?
I got it to work using the setup from: https://github.com/jupyter/docker-stacks/tree/master/minimal-notebook
the trick was to install tini and put the following code into a start-notebook.sh script:
#!/bin/bash
exec jupyter notebook &> /dev/null &
this is than added to the path with:
COPY start-notebook.sh /usr/local/bin/ and
RUN chmod +x /usr/local/bin/start-notebook.sh
Then I could set CMD ["start-notebook.sh"] to start up the container with jupyter running in the background on start.
You can do that, executing the below command
jupyter notebook --allow-root &> /dev/null &
You might see the warning that jupyter command needs --allow-root option if you execute jupyter notebook command as a root in a docker container.

Resources