virtualenv
Bash scripting
Python environment
shell script
command line

How to source virtualenv activate in a Bash script

Codemia Knowledge Hub ·

Use source /path/to/venv/bin/activate, or its shorthand . /path/to/venv/bin/activate. source runs the activate script in the current shell, changing PATH and PS1 so later commands in the script use the virtual environment's Python and packages. Running activate without source activates the environment in a child shell that exits immediately and leaves your script unaffected.

Basic usage

bash
1#!/bin/bash
2
3# Create and activate a virtual environment
4python3 -m venv myenv
5source myenv/bin/activate
6
7# Now python and pip point to the venv
8which python    # /path/to/myenv/bin/python
9pip install requests
10python my_script.py
11
12# Deactivate when done
13deactivate

Because activate runs in the current shell process, the PATH changes persist for the rest of the script.

Why running activate directly does nothing

bash
1#!/bin/bash
2
3# WRONG: runs activate in a subshell — changes are lost
4./myenv/bin/activate        # Permissions error or no effect
5bash myenv/bin/activate     # Activates in child shell, then child exits
6
7# CORRECT: runs in current shell
8source myenv/bin/activate   # Changes PATH in current script
9. myenv/bin/activate        # Shorthand for source (POSIX compatible)

Executing ./venv/bin/activate or bash venv/bin/activate runs it in a subprocess. When that subprocess exits, every environment change (PATH, VIRTUAL_ENV, and so on) is discarded.

Use absolute paths

For scripts that may run from any directory, use an absolute path, and check that the activate script exists before sourcing:

bash
1#!/bin/bash
2VENV_DIR="/home/deploy/app/venv"
3
4if [ -f "$VENV_DIR/bin/activate" ]; then
5    source "$VENV_DIR/bin/activate"
6    echo "Virtual environment activated: $VIRTUAL_ENV"
7else
8    echo "Error: Virtual environment not found at $VENV_DIR"
9    exit 1
10fi
11
12python manage.py migrate
13python manage.py runserver

A missing or corrupted venv otherwise fails silently or with a confusing error, so exit with a clear message instead.

Use paths relative to the script

Hardcoded paths such as /home/alice/venv/bin/activate break for other users and machines. Activate relative to the script's own location instead:

bash
1#!/bin/bash
2SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3VENV_DIR="$SCRIPT_DIR/venv"
4
5source "$VENV_DIR/bin/activate"
6python "$SCRIPT_DIR/app.py"
7deactivate

${BASH_SOURCE[0]} is the path to the script itself, regardless of where it was called from.

CI/CD pipeline example

bash
1#!/bin/bash
2set -euo pipefail
3
4# Create venv if it doesn't exist
5if [ ! -d "venv" ]; then
6    python3 -m venv venv
7fi
8
9source venv/bin/activate
10
11pip install --upgrade pip
12pip install -r requirements.txt
13pytest tests/
14pylint src/
15
16deactivate

In CI pipelines, use set -e so the script fails fast when any command errors.

Cron jobs

Cron does not load .bashrc or .profile, and its minimal PATH may not find python3 or pip. Activate the virtual environment explicitly in the script cron runs:

bash
# crontab entry
0 */6 * * * /bin/bash /home/deploy/scripts/run_task.sh >> /var/log/task.log 2>&1
bash
1#!/bin/bash
2# run_task.sh
3source /home/deploy/app/venv/bin/activate
4cd /home/deploy/app
5python process_data.py
6deactivate

Or skip activation entirely and call the venv's Python directly:

bash
# No activation needed — call the interpreter directly
/home/deploy/app/venv/bin/python /home/deploy/app/process_data.py

This is simpler for cron jobs and scripts that run a single Python command.

POSIX sh compatibility

source is a Bash builtin and is not available in POSIX sh. In /bin/sh scripts, use . instead:

bash
1#!/bin/sh
2. /home/deploy/app/venv/bin/activate
3python my_script.py
4deactivate

. is POSIX-compliant and works in all Bourne-compatible shells.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms