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

bashcommand linepythonscripts

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).

Best Answer

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 &
Related Question