VirtualEnv initialized from bash script

I am trying to write what should be a super simple bash script. Basically activate virtual env, and then replace the working directory. The task that I do a lot and compress one team makes sense.

Mostly...

#!/bin/bash source /usr/local/turbogears/pps_beta/bin/activate cd /usr/local/turbogears/pps_beta/src 

However, when it starts, it is simply thrown back to the shell, and I'm still in the directory in which I ran the script, and the environment is not activated.

+7
source share
4 answers

All you have to do is run the script using the source command. This is because the cd command is local to the shell that runs it. When the script is run directly, a new shell is executed, which terminates when it reaches the end of the script file. Using the source command, you tell the shell to directly execute script instructions.

+20
source

The cd value is local to the current script, which ends when you fall off the end of the file.

What you are trying to do is not "super simple" because you want to override this behavior.

See exec to replace the current process with the process of your choice.

To issue commands to the interactive Bash, see the --rcfile .

+1
source

I assume that you want your script to be dynamic, however, as a quick solution when working on the new system, I create an alias.

begin ie

env is called 'py1' located at ~ / envs / py1 / with a repository location at ~ / proj / py1 /

alias py1 = 'source ~ / envs / py1 / bin / activate; cd ~ / proj / py1 /;

end ie

Now you can access your project and virtualenv by typing py1 from anywhere in the CLI.

I know that this is not that close to ideal, violates DRY and many other programming concepts. This is just a quick and dirty way to quickly access your env and project without the need for setting variables.

+1
source

I know I'm late for the game here, but can I suggest using virtualenvwrapper? It provides a nice bash hook which seems to do exactly what you want.

Check out this tutorial: http://blog.fruiapps.com/2012/06/An-introductory-tutorial-to-python-virtualenv-and-virtualenvwrapper

+1
source

All Articles