Reputation: 49
I can activate my python virtual environment from it's folder by entering . bin/activate
. I'd like to instead type a single word alias, such as shazam
, from the home folder (or anywhere else) that activates the environment, changes to my master project folder, and lists my projects.
I tried creating an alias in .bashrc that pointed to an .sh file containing:
cd ~/path-to-virtual-environment
. bin/activate
cd ~/path-to-master-project-folder
ls -a
I was getting a permission denied error, so I ran chmod u+x <script file>
. The script now runs, but the VE does not activate and while the project folders are listed, the shell is not in the master project folder. I would appreciate some guidance. Thanks.
Upvotes: 4
Views: 3166
Reputation: 94736
Recreate all your environments in ~/.virtualenvs
and install virtualenvwrapper
. The command to activate an env is workon shazam
. Command line completion is supported.
Now about your problem: you've tried to activate an environment in a shell script. That doesn't work because shell scripts run with another shell and env activation change their environments, not the current. I.e., the environment briefly activated but then at the end of the script the new shell exits and the environment deactivated.
There are two ways to overcome this.
Use aliases or shell functions instead of scripts — they are the only way to change the current shell's environment.
Run interactive shell at the end of your script (exec $SHELL
). It inherits activated environment and gives you a command prompt. To deactivate simply exit the shell (exit
or [Ctrl]+[D].).
Upvotes: 1