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.
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"? – Kamil Maciorowski Apr 30 '20 at 08:50source .venv/bin/activatemanually it activates your virtual env as it can be seen in the terminal that.venvhas 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. – marshmello Apr 30 '20 at 09:11as 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? – Kamil Maciorowski Apr 30 '20 at 09:15source .venv/bin/activatethrough 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. – marshmello Apr 30 '20 at 09:29deactivate, so even if you make it affect your current shell,deactivatewill "win". – Kamil Maciorowski Apr 30 '20 at 09:33deactivateand we are only runningsource .venv/bin/activatethrough bash script. Now accordingly when I execute the bash script do the environment gets activated or not as mentioned in the script? – marshmello Apr 30 '20 at 09:55. after_install.shorsource after_install.sh) to allow commands in the script affect your current shell. For the same reason you need tosource .venv/bin/activatein the first place (and you do), not just.venv/bin/activate. – Kamil Maciorowski Apr 30 '20 at 09:57python -m numpy ...or similar. Put this between the activate and deactivate. This should be enough to tell you if the bash works or not – Manish Dash Apr 25 '22 at 23:02