I\'m looking for a good way to figure out the name of the conda environment I\'m in from within running code or an interactive python instance.
The use-case is that
Since similar searches related to 'how do I determine my python environment' leads to this answer I thought I will also mention a way I find out which environment I am currently running my code from. I check the location of my pip binary which points to a location within the current environment. By looking at the output of the following command you can easily determine which environment you are in. (Please note that this solution is not applicable if you have inherited pip packages from your global environment/other environment)
In Windows command prompt:
where pip
If you are inside a Jupyter Notebook add an exclamation mark(!) before the command to execute the command in your host command prompt:
in[10]: !where pip
The output will look something like this:
C:\Users\YourUsername\.conda\envs\YourEnvironmentName\Scripts\pip.exe
C:\ProgramData\Anaconda3\Scripts\pip.exe
YourEnvironmentName gives out the name of your current environment.
In Linux/Mac, you can use the which command instead of where: (Not tested).
For python3 environment
which pip3
From Jupyter notebook:
in[10]: !which pip3
This should directly point to the location within your current environment.