10

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
  • It doesn't activate the virtual environment – How exactly do you test this inside the script? My point is: ## DO SOME STUFF ## obviously does nothing. I don't know .venv/bin/activate. Does it output anything when sourced interactively? and doesn't output if sourced in a script? Is this your "test"? Commented Apr 30, 2020 at 8:50
  • @KamilMaciorowski I mean to say when you run source .venv/bin/activate manually it activates your virtual env as it can be seen in the terminal that .venv has been activated. But when you use the same command inside the bash script it does not activate .venv, seems that the command does not have any effect when running bash script. Commented Apr 30, 2020 at 9:11
  • Sigh… as it can be seen in the terminal – How exactly? Do you need the environment in the script? Or outside of the script after you "run" it? Commented Apr 30, 2020 at 9:15
  • @KamilMaciorowski Why do you create a bash script? - To run all things automatically just by executing it so that you do not have run each and every command manually. The same thing I am trying to achieve I have to run this command source .venv/bin/activate through a bash script which is not running, as I need the env should get activated after this particular script is executed which is not happening. Commented Apr 30, 2020 at 9:29
  • 1
    OK, there is probably an XY problem here. I admit I don't fully understand the issue now (I don't know AWS). I'm reopening the question. My advice for you is to edit it and state clearly that (1) you want the script to affect something (what?) after the script finishes; (2) and sourcing the script (per this answer) is not an option. Commented Apr 30, 2020 at 10:10

5 Answers 5

5

source file_name.sh

worked for me as well (as per Dipanwita Mallick's answer).

The full explanation for why this works is provided here by Lesmana. In summary:

  • Sourcing a script will run the commands in the current shell process. Changes to the environment take effect in the current shell.
  • Executing a script will run the commands in a new shell process.
1
  • There's no actual need to make readers of answers confused by not giving all the details. The file in question should then be something like #!/bin/bash (line 1) and . /my_path_to_my_ve/bin/activate (line 2) (note space after "."). And it must of course be made executable. Commented Nov 9 at 10:43
3

You can do the following using && to chain the commands and continue the lines using \:

#!/usr/bin/env bash set -e source .venv/bin/activate && \ echo "DO SOME STUFF" && \ deactivate 
2
  • 1
    this method worked Commented Mar 19 at 12:22
  • Chaining did the job for me! Thanks !! Commented Apr 22 at 10:59
2

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
  • Word of caution: we probably don't want to use set -e here since the script is likely to be used with an interactive shell. From the Bash manual, the set -e option will "Exit immediately if a pipeline, which may consist of a single simple command, a list, or a compound command returns a non-zero status." The phrase "non-zero status" is UNIX-speak for "error". When "set -e" is enabled and a command fails, any command, the shell will exit. This includes tab completion on commands like python. gnu.org/software/bash/manual/html_node/… Commented Jun 2, 2022 at 2:50
0

Use source to run the shell script.

source file_name.sh 

It worked for me.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. Commented Apr 25, 2022 at 21:53
0

in addition to "activate" file I see python file in .my_env/bin/ so just writing .sh file with one line

./my_env/bin/python start.py 

works fine. this python is from the env and it can see all required dependencies.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.