Celeb Glow
general | March 06, 2026

How to activate python virtualenv through shell script?

0

I have to activate the virtual environment (venv) so I running these commands manually in terminal:

source .venv/bin/activate # To activate the virtual env.

and

deactivate # To deactivate the virtual env

This works fine when running manually. Now I have to insert these commands in a bash script to make AWS CodeDeploy to deploy it on a Ubuntu 18.04 server.

My bash script named after_install.sh looks like this...

#!/usr/bin/env bash
set -e
source .venv/bin/activate
## DO SOME STUFF ##
deactivate

For local testing, I made the script executable and ran the script using bash after_install.sh. But nothing happened. It doesn't activate the virtual environment. It seems none of the above commands worked while running the bash script.

I am not getting why these commands work when I run them manually but not with a bash script. What is going on? I need to write these commands inside the bash script so that AWS CodeDeploy can deploy it on the server.

10

2 Answers

Try to use the full path to virtualenv directory.

#!/usr/bin/env bash
set -e
source /full-path/to/.venv/bin/activate
## DO SOME STUFF -> USE FULL PATH HERE TOO #
deactivate

Best regards.

1

Use source to run the shell script.

source file_name.sh

It worked for me.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy