I read similar questions and have tried their suggestions but I still cannot activate my virtual environment. The hierarchy of directories is:
myproject
-- virtualenv
-- startvenv.sh
startvenv.sh is:
#!/bin/bash
source virtualenv/bin/activate
And, I am running startvenv.sh by:
./startvenv.sh
No error, but nothing happens. Why? Ideally, I want to open a new terminal and activate my virtual environment there.
The virtualenv activates by sourcing (not normally running) the
virtualenv/bin/activate
script. If you want to do this in your own script, then you must source that script too and not just run it. Meaning:The difference between running and sourcing is that running executes the script in its own separate subshell, which is isolated from the parent shell (the one from which you called it) so that e.g. environment variables and other changes inside the script do not get propagated to the parent.
Sourcing explicitly does exactly that, executing the script in your current shell, which leaves all changes to environment variables etc intact after it finishes.
Here's a shorted extract from
man bash
(section about Shell Builtins):