How to activate python virtualenv through shell script?
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.
Related videos on Youtube
marshmello
Updated on September 18, 2022Comments
-
marshmello over 1 year
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.
-
Kamil Maciorowski almost 4 years
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"? -
marshmello almost 4 years@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. -
Kamil Maciorowski almost 4 yearsSigh…
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? -
marshmello almost 4 years@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. -
Kamil Maciorowski almost 4 yearsThere's a difference between expecting something to work in a script and after the script. Also note the script contains
deactivate
, so even if you make it affect your current shell,deactivate
will "win". -
marshmello almost 4 years@KamilMaciorowski Ok, let's remove
deactivate
and we are only runningsource .venv/bin/activate
through bash script. Now accordingly when I execute the bash script do the environment gets activated or not as mentioned in the script? -
Kamil Maciorowski almost 4 yearsHave you read my answer to the linked question? You need to source the script (
. after_install.sh
orsource after_install.sh
) to allow commands in the script affect your current shell. For the same reason you need tosource .venv/bin/activate
in the first place (and you do), not just.venv/bin/activate
. -
marshmello almost 4 years@KamilMaciorowski I have read your answer to the linked question and I know that I have to source the script, but I do not have to execute/source the script by myself. My role is just to write commands in a script and on my behalf, AWS CodeDeploy will execute it on the server and thats when I do not know how the script will behave.
-
Kamil Maciorowski almost 4 yearsOK, 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.
-
Admin about 2 years@marshmello I am guessing you need a specific package in this python env? Can you add a command where you try to load a package that is in this env but NOT in the base env
python -m numpy ...
or similar. Put this between the activate and deactivate. This should be enough to tell you if the bash works or not
-
-
Admin about 2 yearsYour 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.
-
Admin almost 2 yearsWord 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, theset -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 likepython
. gnu.org/software/bash/manual/html_node/…