I am writing a bash script which need to start a service using virtualenv
. I imagine my bash script to look 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 that the environment has been activated. Is there an environment variable / command in the shell that can tell me if I am inside a virtualenv or not, if so, which one?
I can hard code to print the python path, where it should point to a customized python interpreter if I am inside the virtualenv, but I am not sure if that is the proper way to do it.
Thanks!
The name of the current virtual environment will now appear on the left of the prompt (e.g. (venv)Your-Computer:project_folder UserName$ ) to let you know that it's active. From now on, any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.
Verify if Virtualenv is installed There is a chance that virtualenv is already installed on your system. If you see a version number (in my case 1.6. 1), it's already installed.
If you want to verify that you're using the right pip and the right virtual environment, type pip -V and check that the path it displays points to a subdirectory of your virtual environment. Note that when you want to upgrade pip in a virtual environment, it's best to use the command python -m pip install -U pip .
You can check for the environment variable VIRTUAL_ENV and see if it has the correct source path. Now if it doesn't exist then you know it's not activated. If it does, you need to check and see if it has the correct path.
The correct bash snippet that will work to check if the 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With