简体   繁体   中英

How to create and activate virtualenv at the same time in my bash shell script

I'm trying to create an automation program that setups my django project.

I was able to create the project folder and virtual environment however I'm not sure if virtual env was activated or not in that folder. I need the virtual env to activate so that I can install django. The way I run the command for now is ./auto.sh setup <poject_folder_name> , where setup is the name of my function. Here is the inside of the function.

python3 setup.py $2
cd $FILEPATH/$2
echo $FILEPATH/$2
virtualenv env
source env/bin/activate
  
cd ~/scripts
python3 checker.py

The last two lines in the above code is my attempt in checking if the virtual env was activated in that folder and I always get the FAILED output. This is what checker.py contains.

import sys

if hasattr(sys, 'real_prefix'):
    print('\nOK\n')
else:
    print('\nFAILED\n')

I'm really new to bash scripting and any help is greatly appreciated.

[EDIT] FILEPATH was set in my .bash_profile to the directory where I want the new folder to be created.

Solution 1, using a prefix

VENV="env/bin"
PYTHON="$VENV/python"
# etc...

Solution 2, using a function

activate () {
    . `pwd`/env/bin/activate
}

activate
# etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM