Checking Python Virtualenv Environment
I am writing a bash script that needs to start a service using virtualenv
. I am assuming my bash script looks like this:
#!/bin/bash
source /root/user/python-ev/bin/activate
IPYTHON_OPTS="notebook --port 8889 --profile pyspark" pyspark --master spark://701.datafireball.com:7077
However, I want to add a line between activate
and IPYTHON...
to make sure the environment has been activated. Is there an environment variable / command in the shell that can tell me if I am inside virtual or not, if so which one?
I can hard code to print the python path where it should point to a custom python interpreter if I am inside a virtualenv, but I'm not sure if this is the correct way to do it.
Thank!
source to share
You can check the VIRTUAL_ENV environment variable and see if it has the correct source path. Now if it doesn't exist, you know that it is not activated. If so, you need to check and see if it has the correct path.
The correct bash snippet that will work to check if a variable is set or not is the following
if [[ -z "$VIRTUAL_ENV" ]]; then
echo "No VIRTUAL_ENV set"
else
echo "VIRTUAL_ENV is set"
fi
source to share