There are several methods to determine if a Python venv (virtual environment) is active:
1. Shell Prompt Indication:
When a virtual environment is activated, its name typically appears in parentheses at the beginning of your shell prompt. For example, if your virtual environment is named my_env, your prompt might look like (my_env) user@hostname:~$.
2. Checking the VIRTUAL_ENV Environment Variable:
A common method is to check for the presence and value of the VIRTUAL_ENV environment variable, which is set when a virtual environment is activated. In Bash/Zsh (Linux/macOS).
    echo $VIRTUAL_ENV
If a path to your virtual environment is displayed, it is active. If nothing is returned, it is not active. In CMD (Windows).
    echo %VIRTUAL_ENV%
Similar to Bash, the path will be displayed if active. In PowerShell (Windows).
    Get-ChildItem Env:VIRTUAL_ENV
This will show the environment variable’s details if it exists.
3. Using Python’s sys Module:
Within a Python script or interactive session, you can compare sys.prefix and sys.base_prefix.
Python
import sys<br><br>def in_venv():<br>    return sys.prefix != sys.base_prefix<br><br>if in_venv():<br>    print("Inside a virtual environment.")<br>else:<br>    print("Not in a virtual environment.")
If sys.prefix and sys.base_prefix are different, it indicates that the current interpreter is running from a virtual environment.
4. Checking pip --version:
When a virtual environment is active, pip --version will show that pip is located within the virtual environment’s directory.
pip --version
Look for the path to the pip executable; if it points to a location within your virtual environment (e.g., path/to/my_env/bin/pip), then the venv is active.
5. Checking which python (Linux/macOS) or where python (Windows):
These commands reveal the path to the python executable currently being used. If the path points to the python executable within your virtual environment, it is active. Linux/macOS.
    which python
Windows:
    where python
