341

I have jupyter/anaconda/python3.5.

  1. How can I know which conda environment is my jupyter notebook running on?

  2. How can I launch jupyter from a new conda environment?

jkdev
  • 11,360
  • 15
  • 54
  • 77
RockScience
  • 17,932
  • 26
  • 89
  • 125
  • 3
    install a separate ipython kernel in every environment you want to use in jupyter notebook. This way it does not matter which jupyter instance you are starting. – cel May 07 '16 at 07:17
  • 5
    [Docs on installing kernels for environments](http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments). To see which conda env a notebook is running in, you can check `sys.executable`. – Thomas K May 07 '16 at 11:13
  • 4
    It's easier if you use the package `nb_conda_kernels`. Related question/answer: [Using both Python 2.x and Python 3.x in IPython Notebook](http://stackoverflow.com/a/30492913/3345375) – jkdev Aug 22 '16 at 02:01
  • 6
    Please also see [this great article](https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/). Briefly, it talks very clearly about the difference between `jupyter`'s "kernel" python environment and the shell's python environment you launch `jupyter notebook [notebook_name].ipynb` from. – Nathan majicvr.com Aug 22 '19 at 08:16
  • 1
    Question 1 simplest answer is to type " !conda info " or "import sys; print(sys.executable)" into a current notebook cell or at the command prompt. For Question 2 follow the simplest answer below. – Rich Lysakowski PhD Mar 25 '21 at 00:06
  • @Nathan since you must have written your comment right after finishing reading the article you linked I'd have appreciated if you also summarized what those differences are – Redoman Feb 16 '22 at 10:55
  • check this answer https://stackoverflow.com/a/42660674/6814154 – Khalil Al Hooti Jul 27 '22 at 10:47
  • @Redoman yeah if I had been more diligent at that time, I should have written a proper, thorough answer. Even just copying and pasting the most important points from that would have been helpful in case that link died – Nathan majicvr.com Feb 27 '23 at 18:43

17 Answers17

407

As mentioned in the comments, conda support for jupyter notebooks is needed to switch kernels. Seems like this support is now available through conda itself (rather than relying on pip). http://docs.continuum.io/anaconda/user-guide/tasks/use-jupyter-notebook-extensions/

conda install nb_conda

which brings three other handy extensions in addition to Notebook Conda Kernels.

alth
  • 3
  • 2
ink
  • 4,413
  • 3
  • 14
  • 11
  • 20
    Note: to reflect the changes, please restart the conda environment. – Rishabh Agrahari Aug 14 '17 at 19:38
  • 1
    this is the right way to bring the notebook to the environment – Damon Yuan Sep 03 '17 at 14:30
  • 12
    Note: In addition, you would need `Jupyter` (or perhaps `ipykernel` according to the answer by rakesh) installed in *that environment*. Otherwise Jupyter can recognise that environment, but it cannot create new notebook in that environment. – taper Dec 29 '17 at 16:47
  • 4
    @RockScience, consider switching this to be the accepted answer. – TayTay Aug 08 '18 at 15:22
  • 1
    Very helpful. Solved the issue. – Huanfa Chen Sep 08 '18 at 12:28
  • 2
    Should nb_conda be installed in base env, target env, or both? Please extend the answer, thanks. – Martin Thøgersen Jul 11 '19 at 19:13
  • 3
    Sometimes `nb_conda` doesn't play nice with some `conda`/`pip` installed libraries. When setting up a new environment I encourage you to install `nb_conda` first *then* install your other libraries – MattR Aug 16 '19 at 19:50
  • Very helpful. I was about to remove anaconda directory and start all over again. you saved me weeks of work that could have lost in another few minutes. thanks a ton. – emeralddove Aug 26 '19 at 07:36
  • 1
    If you have any other packages installed nb_conda will insist on downgrading them to three year old versions. Make sure to run conda update --all if it clobbers your workspace. – jtiscione Sep 15 '19 at 06:03
  • @MartinThøgersen Anaconda Prompt was in the target env when I ran `conda install nb_conda` but I feel it's irrelevant. – YoussefDir Sep 24 '20 at 19:29
174

Question 1: Find the current notebook's conda environment

Open the notebook in Jupyter Notebooks and look in the upper right corner of the screen.

It should say, for example, "Python [env_name]" if the language is Python and it's using an environment called env_name.

jupyter notebook with name of environment


Question 2: Start Jupyter Notebook from within a different conda environment

Activate a conda environment in your terminal using source activate <environment name> before you run jupyter notebook. This sets the default environment for Jupyter Notebooks. Otherwise, the [Root] environment is the default.

jupyter notebooks home screen, conda tab, create new environment

You can also create new environments from within Jupyter Notebook (home screen, Conda tab, and then click the plus sign).

And you can create a notebook in any environment you want. Select the "Files" tab on the home screen and click the "New" dropdown menu, and in that menu select a Python environment from the list.

jupyter notebooks home screen, files tab, create new notebook

Community
  • 1
  • 1
jkdev
  • 11,360
  • 15
  • 54
  • 77
  • 162
    What should I do if I don't have the `Conda` tab? – Dror Sep 29 '16 at 13:54
  • 3
    @Dror Make sure jupyter notebook is running from within a conda environment. Check where your jupyter is located by running `which jupyter` on the command line. – jkdev Sep 29 '16 at 14:20
  • 30
    `which jupyter` returns `/Users/name/anaconda/envs/myEnv/bin/jupyter` and I don't get the `Conda` tab nor environment name in `[]`. My `jupyter` version is 4.2.0 – Dror Sep 29 '16 at 14:27
  • 11
    It seems like the missing piece is http://stuartmumford.uk/blog/jupyter-notebook-and-conda.html Thanks @chinnychinchin (http://stackoverflow.com/a/39900046/671013) – Dror Oct 06 '16 at 15:36
  • 2
    hmm this doesn't seem to work for me... I cannot import modules even though I see my condo environment as default in the environment list and am running my default conda env – BigBoy1337 Jan 19 '17 at 04:20
  • 8
    In my case, after `conda install jupyter`, I deactivated env, then ran `jupyter notebook`, then I got my env listed in the dropdown of 'new'. – Rishabh Agrahari Feb 28 '17 at 18:49
  • 18
    @BigBoy1337 ink's answer `conda install nb_conda` seems to be the most straightforward way to get to the state described in this answer. – Harsh Dec 08 '17 at 14:28
  • 2
    "source is not recognized as an internal or external command, operable program or batch file" – Jack M May 25 '18 at 08:11
  • 2
    @JackM, `source` is needed under Linux / macOS. On Windows just use `activate`. – Royi May 25 '18 at 14:12
  • Can one have different configuration file of `Jupyter` per each `conda` envioronment? – Royi May 25 '18 at 14:59
  • `import sys; print(sys.executable)` was the only thing that worked for me – Lukas Jun 26 '19 at 11:23
  • 1
    why everything in this solution/notebook looks different from my Jupyter notebook? on home page, I do not have Conda, on top right mine does not say Python[env_name], when I click on New, there is no Python[env_name] I have installed Earth Engine module which works in terminal, but not on Jupyter! – OverFlow Police Nov 07 '19 at 23:39
  • Here is a link to a screenshot of my Python: https://ibb.co/g6JNh5Q – OverFlow Police Nov 09 '19 at 22:13
  • 4
    When I go to my Jupyter homescreen and click "new", my only options for notebook are Python 3, even though I have created several different environments in Anaconda navigator. How did you get the other options? – seeker_after_truth Jul 01 '20 at 09:41
  • do we need to install jupyter notebook in the new environment? – user75252 Aug 07 '20 at 10:50
  • Referring to question 1, what is the path for this at Jupyter Lab? – Ramon Nov 26 '20 at 20:46
  • What if I use `python3 -m venv env` and I don't see `conda` menu ? – Dmitriy Ogureckiy Aug 07 '22 at 16:29
122

which environment is jupyter executing:

import sys
print(sys.executable)

create kernel for jupyter notebook

source activate myenv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
source activate other-env
python -m ipykernel install --user --name other-env --display-name "Python (other-env)"

http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernel-install

HongboZhu
  • 4,442
  • 3
  • 27
  • 33
liuzhijun
  • 4,329
  • 3
  • 23
  • 27
58

If the above ans doesn't work then try running conda install ipykernel in new env and then run jupyter notebook from any env, you will be able to see or switch between those kernels.

rakesh
  • 4,368
  • 1
  • 19
  • 13
36

to show which conda env a notebook is using just type in a cell:

!conda info

if you have grep, a more direct way:

!conda info | grep 'active env'
Savrige
  • 3,352
  • 3
  • 32
  • 38
28

You can also switch environments in Anaconda Navigator, install Jupiter and run it. Anaconda Navigator

Rimma Shafikova
  • 416
  • 5
  • 7
16

Because none of the answers above worked for me, I write here the solution that finally solved my problem on Ubuntu. My problem was:
I did the following steps:

  1. Activate my environment: conda activate MyEnv
  2. Start jupyter notebook:jupyter notebook

Although MyEnv was active in the terminal and had an asterix when writing conda env list, but jupyter notebook was started with the base environment. Installing nb_conda and ipykernel didn't solve the problem for me either. Additionally, the conda tab wasn't appearing in jupyter notebook and also clicking on the kernels or going to the menu Kernel->Change Kernel didn't show the kernel MyEnv.
Solution was: install the jupyter_environment_kernel in MyEnv environment:

pip install environment_kernels

After that when starting jupyter notebook, it is started with the right environment. You can also switch between environments without stopping the kernel, by going to the menu Kernel->Change Kernel and selecting the desired kernel.

Code Pope
  • 5,075
  • 8
  • 26
  • 68
  • You should create the environment directly from the Anaconda Desktop GUI Environment. This answer gives unintended and unwanted side effects from using pip to modify a conda environment. You should not mix pip and conda commands within the same environment. See https://www.anaconda.com/blog/using-pip-in-a-conda-environment for more information. – Rich Lysakowski PhD Mar 25 '21 at 00:12
11

Question 1: How can I know which conda environment is my jupyter notebook running on?

  • Launch your Anaconda Prompt and run the command conda env list to list all the available conda environments.

    Anaconda Prompt Screenshot

    You can clearly see that I've two different conda environments installed on my PC, with my currently active environment being root(Python 2.7), indicated by the asterisk(*) symbol ahead of the path.

Question 2: How can I launch jupyter from a new conda environment?

  • Now, to launch the desired conda environment, simply run activate <environment name>. In this case, activate py36

    enter image description here

For more info, check out this link and this previous Stack Overflow question..

Sushant Rajbanshi
  • 1,985
  • 1
  • 19
  • 20
7

The following commands will add the env in the jupyter notebook directly.

conda create --name test_env
conda activate test_env
conda install -c anaconda ipykernel
python -m ipykernel install --user --name=test_env

Now It should say, "Python [test_env]" if the language is Python and it's using an environment called test_env.

5

To check on which environment your notebook is running type the following commands in the notebook shell

import sys
print(sys.executable)

To launch the notebook in a new environment deactivate that environment first. Create a conda environment and then install the ipykernel. Activate that environment. Install jupyter on that environment.

conda create --name {envname}
conda install ipykernel --name {envname}
python -m ipykernel install --prefix=C:/anaconda/envs/{envname} --name {envname}
activate envname
pip install jupyter

In your case path "C:/anaconda/envs/{envname}" could be different, check accordingly. After following all steps, launch notebook and do step 1 run the following in shell.

sys.executable

This should show: Anaconda/envs/envname

MBaadror
  • 51
  • 1
  • 3
2

On Ubuntu 20.04, none of the suggestions above worked.

I.e. I activated an existing environment. I discovered (using sys.executable and sys.path) that my jupyter notebook kernel was running the DEFAULT Anaconda python, and NOT the python I had installed in my activated environment. The consequence of this was that my notebook was unable to import packages that I had installed into this particular Anaconda environment.

Following instructions above (and a slew of other URLs), I installed ipykernel, nb_conda, and nb_conda_kernels, and ran: python -m ipykernel --user --name myenv.

Using the Kernels|Change Kernel... menu in my Jupyter notebook, I selected myenv, the one I had specified in my python -m ipykernel command.

However, sys.executable showed that this did not "stick".

I tried shutting down and restarting, but nothing resulted in my getting the environment I had selected.

Finally, I simply edited file kernel.json in folder:

~/.local/share/jupyter/kernels/myenv

Sure enough, despite my having performed all the steps suggested above, the first argument in this JSON file was still showing the default python location:

$Anaconda/bin/python (where $Anaconda is the location where I installed anaconda)

I edited file kernel.json with a text editor so that this was changed to:

$Anaconda/envs/myenv/bin/python

Hopefully, my use of myenv is understood to mean that you should replace this with the name of YOUR environment.

Having edited this file, my Jupyter notebooks started working properly - namely, they used the python specified for my activated environment, and I was able to import packages that were installed in this environment, but not the base Anaconda environment.

Clearly, something is messed up in how the set of packages ipykernel, nb_conda, and nb_conda_kernels are configuring Anaconda environments for jupyter.

ph99
  • 21
  • 2
  • I had to change `~/.local/share/jupyter/kernels//kernel.json` as well. – apoorv020 Jul 21 '21 at 22:12
  • I can confirm that this solution also worked for me on Windows. The file that needs to be edited should be found in `C:\\Users\\username\\AppData\\Roaming\\jupyter\\kernels\\myenv`, in which you need to change the path of the `python.exe` file to that found in your environment, which should probably be as such: `C:\\Users\\username\\anaconda3\\envs\\myenv\\python.exe`. – Jakinduria Aug 17 '21 at 12:10
1

I have tried every method mentioned above and nothing worked, except installing jupyter in the new environment.

to activate the new environment conda activate new_env replace 'new_env' with your environment name.

next install jupyter 'pip install jupyter'

you can also install jupyter by going to anaconda navigator and selecting the right environment, and installing jupyter notebook from Home tab

1

For windows 10,

  1. Go into Anaconda Launcher
  2. In the 'Applications on' dropdown menu, select the required conda environment.
  3. Install Jupyter notebook in the Anaconda Launcher
  4. Launch Jupyter notebook from the Anaconda Launcher

Image of Anaconda Navigator

The Conda tab is visible in the Jupyter notebook where you can see your active conda env.

Maria
  • 171
  • 1
  • 4
1

Run these lines in any cell to know the name of environment

import os
print(os.environ['CONDA_DEFAULT_ENV'])
Regi Mathew
  • 2,603
  • 3
  • 24
  • 38
0

Adding to the above answers, you can also use

!which python

Type this in a cell and this will show the path of the environment. I'm not sure of the reason, but in my installation, there is no segregation of environments in the notebook, but on activating the environment and launching jupyter notebook, the path used is the python installed in the environment.

  • In many cases, `which` is the wrong command to run. The POSIX standard command is `type`, though on Windows and in Zsh you want `whence` – tripleee Mar 22 '23 at 13:38
0
  1. For checking on Which Python your Jupyter Notebook is running try executig this code.

from platform import python_version print(python_version())

  1. In order to run jupyter notebook from your environment activate MYenv and install jupyter notebook using command pip install jupyter notebook

then just jupyter notebook

0

What solved the issue for me was that I had to run the following command:

python -m ipykernel install --user --name myenv --display-name "Python (myenv)"

The issue was that I opened a jupyter notebook made with/for a kernel for another conda python environment. That was visible from the output on the terminal; it is handy op run jupyter notebook from a terminal on the conda environment so that you can easily see what error messages are shown. Then it became clear that the notebook tried to run python from another environment.

Inspecting the folder/files:

C:\Users\<username>\AppData\Roaming\jupyter\kernels\<env name>\kernel.json

 "argv": [
  "D:\\Users\\<username..path>\\envs\\<env name>\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python (env name)",
 "language": "python",
 "metadata": {
  "debugger": true
 }
}

So you can check and correct if necessary.

Second, when using anaconda/conda, make sure you have a healthy channel policy and execute the following commands when creating a new environment:

conda config --add channels conda-forge
conda config --set channel_priority strict

It solved this problem for me, I hope it helps you too.

Luc Krols
  • 11
  • 3