How to activate a virtualenv within bash script resulting in activated prompt

32,291

Solution 1

A shell script, when executed, runs in a subshell and all parameters and environment goes out of scope once the execution completes.

To make the environment available in the current shell session , you need to source it:

source /path/to/script.sh

As a side note, if you want some blocking process to keep working in the background while you regain access to the terminal prompt, send the process to background:

some_command_to_run_in_background &

Solution 2

As I am facing the same case I have found out that putting source acitvate into the .bashrc of a subshell might be a good direction.

#!/bin/bash
#
# This is an example for https://askubuntu.com/a/1052868/847382
#
# Copyright 2018 (c) Sebastian Sawicki (0x52fb0d10)
#  OPENPGP4FPR:5691BED8E6CA579830842DD85CB361E552FB0D10
#
# Licence: https://creativecommons.org/licenses/by/4.0/
#

# Create temporary directory
TMPDIR="$(mktemp --directory)"
trap "echo 'INFO: Exited temporary shell.' >&2; rm --force --recursive '${TMPDIR}'" EXIT

# Set-up virtualenv in the temporary directory
virtualenv "${TMPDIR}"
. "${TMPDIR}/bin/activate"

# Install any required pip packages
[ -r "$(pwd)/pyReqs.txt" ] && pip install --requirement "$(pwd)/pyReqs.txt"

# Run a subshell with virtualenv already activated
bash --rcfile "${TMPDIR}/bin/activate" -i

Solution 3

This is not what you asked for but I think it might help you.

The easiest way to activate virtual environment from anywhere:

Developer Note:- you should create all your virtualenv in one folder, such as virt.

Assuming your virtualenv folder name is virt (if not change it)

cd
mkdir custom

To install nano use below command or use your favorite editor

sudo apt-get install nano

To exit nano press ctrl+x and press y

nano custom/vhelper

Add these lines...

#!/usr/bin/env bash
ENV_PATH="$HOME/virt/$1/bin/activate"
bash --rcfile $ENV_PATH -i

Grant execue permission to your file

sudo chmod +x custom/vhelper

now export that custom folder path so that you can find it on command-line by clicking tab...

PATH=$PATH:"$HOME/custom"

Now you can use it from anywhere by just typing below command...

vhelper YOUR_VIRTUAL_ENV_FOLDER_NAME

suppose it is abc then...

vhelper abc

Congrats you have activated you virtualenv...

Share:
32,291

Related videos on Youtube

Rick Page
Author by

Rick Page

Updated on September 18, 2022

Comments

  • Rick Page
    Rick Page over 1 year

    I want a script that will set up my tools, but also keep the virtual environment intact for my prompt. But, as heemayl points out below, a shell script, when executed, runs in a subshell and all parameters and environment goes out of scope once the execution completes. Now I am thinking, the best way would be to open a new terminal window from the script, and have the script "send" a source activate command to the new terminal. More details follow, but is there a way to set up my tools and leave me in a virtual env prompt?

    When first beginning work on many of my projects, namely those using Django, I would start the same way. This turned into a script, below.

    #!/bin/bash
    #0 cd into project
    cd $WSGI
    
    #1. Load the virtual env
    . /path/to/virtualenvs/django1-8-py-3/bin/activate
    
    #2. Open spyder or other IDEs
    spyder3 &
    
    #3. Run an interactive shell that runs the development server i.e.
    
    ipython -m pdb manage.py runserver
    

    This works fine, until I need to drop out of the ipython and into the activated bash. Because the script above has completed after the ipython is quit, I am dropped into the plain ol shell. But what I want is to see the prompt i.e. (virtualenv) me@mine:~$ as if I had run the source activate command and not the script above.

    This smells like an XY problem, I know. But without context I was afraid my question would be unclear. I'm asking how to make a script that, at the end of it, provides me a terminal with my virtualenv. Yes, I could just, do this:

    source acitvate
    run my script
    when done in ipython I'm still in virtual env
    

    Possible solution

    Can I maybe use a shell script to open a new terminal window I'm thinking, is there maybe a way to:

    do the steps in my script first
    then open a new terminal window
    run activate in this window
    

    ...resulting in two tabs/windows, one that runs ipython and the other is in the virtualenv bash (so I can, for example, run manage.py commands without stopping ipython).

  • Rick Page
    Rick Page over 7 years
    "A shell script ... runs in a subshell and all parameters and environment [variables go] out of scope once the execution completes." Yes, I was struggling to find the way to say this. So is there a way to use a shell script to open a new terminal window and subsequently run "source activate" in that new terminal?
  • heemayl
    heemayl over 7 years
    @RPCodes Possible but isn't source-ing the solution you are looking for?
  • heemayl
    heemayl over 7 years
    @RPCodes If i understand you correctly, then source activate setup_dev_tools.sh should work for you..
  • Rick Page
    Rick Page over 7 years
    That gives me the prompt that I want, but doesn't seem to run the script. I need to revisit virtualenv docs. And yeah, I'm finding this hard to explain, sorry!
  • Rick Page
    Rick Page almost 6 years
    Interesting, thanks. Gives me a good idea (will post here if I get something working) One thing: bash --rcfile="${TMPDIR}/bin/activate" -i gives "invalid option". Leaving out the = seemed to work for me. bash --rcfile "${TMPDIR}/bin/activate" -i
  • Sebo.PL
    Sebo.PL almost 6 years
    You are right RP Codes: there was a spelling mistake at last line. bash does not accept '=' sign after option.