0

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.

sudocat
  • 46
  • 2
  • 8
  • You activated the virtual environment globally; your current working directory doesn't have any affect on whether it is activated or not. There isn't really anything *to* check. – chepner Sep 02 '20 at 12:49
  • Sorry as I wasnt clear the first time. I have edited my post which would explain `FILEPATH` – sudocat Sep 02 '20 at 12:51
  • Are you sure it's a bash script? [Set bash shebang explicitly](https://stackoverflow.com/a/670216/7976758). May be anything here help: https://stackoverflow.com/search?q=%5Bbash%5D+%5Bvirtualenv%5D+activate+from+script ? – phd Sep 02 '20 at 12:56

1 Answers1

0

Solution 1, using a prefix

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

Solution 2, using a function

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

activate
# etc...
mlisthenewcool
  • 380
  • 1
  • 10