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
Because activate runs in the current shell process, the PATH changes persist for the rest of the script.
Why running activate directly does nothing
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:
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_SOURCE[0]} is the path to the script itself, regardless of where it was called from.
CI/CD pipeline example
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:
Or skip activation entirely and call the venv's Python directly:
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:
. is POSIX-compliant and works in all Bourne-compatible shells.
Related reading
- How to specify a byte-array serializer/deserializer in Kafka-Python
- How to specify a part of code to run in a particular thread in a multithreaded environment in python?
- How to specify credentials when connecting to boto3 S3?
- How to specify multiple types using type-hints
- How to specify nullable return type with type hints
- How to specify number of GPUs in Python interface?
- How to specify python type hints for complex package as opencv or tensorflow?
- How to split a dataframe string column into two columns?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.