Reputation: 532
Quick run down of what I'm doing, I'm making python development environments on my computer using virtualenv. I'm not one to remember all these commands, so I like to build simple(noob-ish) script that will help me.
As you see on step 2 in the Basic Usage
section of the documentation. I need to run a command:
$ source venv/bin/activate
This activates my python environment. So I've tried a mixture of functions
and eval
. As mentioned, I am a bit of a noob when it comes to bash scripts.
File: fpyenv
#!/bin/bash
# $ cd ~/scripts/
# $ chmod u+x <filename>.sh
activateSrc(){
eval '$(source "${1}"/bin/activate)'
}
if [[ $1 == '' ]];
then
printf "ERROR: Missing Filename\n"
else
printf "Creating Virtual Environment $1\n"
# This creates the environment
virtualenv $1
printf "Do you want to activate $1 as your virtual environment?(y/n)\n"
# Get answer from user
read answer
if [[ $answer != 'y' ]];
# Said No
then
printf "Did not set $1 as your virtual environment\n"
else
# Said Yes
activateSrc $1
printf "Set $1 as your virtual environment\n"
fi
fi
myComputer $ fpyenv venv
Creating Virtual Environment venv
Do you want to activate venv as your virtual environment?(y/n)
y
Set venv as your virtual environment
(venv)myComputer $
But what I'm getting is:
Set venv as your virtual environment
myComputer $
So it doesn't run the source venv/bin/activate
. Does anyone know what I'm doing wrong? I've looked through many answers, but given that source
is commonly used in a different way in bash scripts, the answers I'm getting are no help. Thank you in advance!
Change activateSrc
to:
activateSrc(){
source $1/bin/activate
}
And execute script like this:
myComputer $ . fpyenv venv
Upvotes: 0
Views: 56
Reputation: 799490
It runs source
just as written. The thing is that you need to source this new script as well, otherwise it just runs in a subshell and any changes made by the sourced script are lost when the subshell exits.
Upvotes: 2