In which conda environment is Jupyter executing?

391,556

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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

Share:
391,556

Related videos on Youtube

RockScience
Author by

RockScience

Updated on January 15, 2022

Comments

  • RockScience
    RockScience over 2 years

    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?

    • cel
      cel almost 8 years
      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.
    • Thomas K
      Thomas K almost 8 years
      Docs on installing kernels for environments. To see which conda env a notebook is running in, you can check sys.executable.
    • jkdev
      jkdev over 7 years
      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
    • Nathan
      Nathan over 4 years
      Please also see this great article. 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.
    • Rich Lysakowski PhD
      Rich Lysakowski PhD about 3 years
      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.
    • Redoman
      Redoman about 2 years
      @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
  • Dror
    Dror over 7 years
    What should I do if I don't have the Conda tab?
  • jkdev
    jkdev over 7 years
    @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.
  • Dror
    Dror over 7 years
    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
    Dror over 7 years
    It seems like the missing piece is stuartmumford.uk/blog/jupyter-notebook-and-conda.html Thanks @chinnychinchin (stackoverflow.com/a/39900046/671013)
  • BigBoy1337
    BigBoy1337 over 7 years
    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
  • Rishabh Agrahari
    Rishabh Agrahari about 7 years
    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'.
  • Ken Myers
    Ken Myers almost 7 years
    Thank you! I tried everything above this and this was the only thing that worked short of deleting and remaking the environment
  • Rishabh Agrahari
    Rishabh Agrahari over 6 years
    Note: to reflect the changes, please restart the conda environment.
  • Damon Yuan
    Damon Yuan over 6 years
    this is the right way to bring the notebook to the environment
  • Harsh
    Harsh over 6 years
    @BigBoy1337 ink's answer conda install nb_conda seems to be the most straightforward way to get to the state described in this answer.
  • taper
    taper over 6 years
    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.
  • vizyourdata
    vizyourdata about 6 years
    This worked to finally get my theano environment working from jupyter. Thanks!
  • Jack M
    Jack M almost 6 years
    "source is not recognized as an internal or external command, operable program or batch file"
  • Royi
    Royi almost 6 years
    @JackM, source is needed under Linux / macOS. On Windows just use activate.
  • Royi
    Royi almost 6 years
    Can one have different configuration file of Jupyter per each conda envioronment?
  • TayTay
    TayTay over 5 years
    @RockScience, consider switching this to be the accepted answer.
  • Huanfa Chen
    Huanfa Chen over 5 years
    Very helpful. Solved the issue.
  • Bersan
    Bersan over 5 years
    Yes, this was the right answer. nb_conda alone didn't work.
  • Sincole Brans
    Sincole Brans over 5 years
    if it throws "kernel Error" we need to install users for the kernels,add "python -m ipykernel install --user"
  • Neveroldmilk
    Neveroldmilk over 5 years
    Great solution! But it is difficult to delete these manually added ipykernel environment.
  • Lukas
    Lukas almost 5 years
    import sys; print(sys.executable) was the only thing that worked for me
  • Martin Thøgersen
    Martin Thøgersen almost 5 years
    Should nb_conda be installed in base env, target env, or both? Please extend the answer, thanks.
  • MattR
    MattR over 4 years
    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
  • emeralddove
    emeralddove over 4 years
    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.
  • jtiscione
    jtiscione over 4 years
    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.
  • OverFlow Police
    OverFlow Police over 4 years
    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
    OverFlow Police over 4 years
    Here is a link to a screenshot of my Python: ibb.co/g6JNh5Q
  • Bonson
    Bonson almost 4 years
    Thank you. This was required for me to finally see the specific environment in the Kernels tab when Jupyter Notebooks is opened.
  • seeker_after_truth
    seeker_after_truth almost 4 years
    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?
  • user75252
    user75252 over 3 years
    do we need to install jupyter notebook in the new environment?
  • YoussefDir
    YoussefDir over 3 years
    @MartinThøgersen Anaconda Prompt was in the target env when I ran conda install nb_conda but I feel it's irrelevant.
  • Ramon
    Ramon over 3 years
    Referring to question 1, what is the path for this at Jupyter Lab?
  • Alex W
    Alex W over 3 years
    To delete start a terminal in jupyter and run jupyter kernelspec uninstall [env_name]
  • Rich Lysakowski PhD
    Rich Lysakowski PhD about 3 years
    This is the simplest and easiest answer for part 1 of the OP's questions.
  • Rich Lysakowski PhD
    Rich Lysakowski PhD about 3 years
    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 anaconda.com/blog/using-pip-in-a-conda-environment for more information.
  • Rich Lysakowski PhD
    Rich Lysakowski PhD about 3 years
    This is the best answer. nb_conda is fraught with compatibility problems and brings a lot of detritus into the base environment.
  • apoorv020
    apoorv020 almost 3 years
    I had to change ~/.local/share/jupyter/kernels/<myenv>/kernel.json as well.
  • Jakinduria
    Jakinduria over 2 years
    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\\mye‌​nv, 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.
  • hbstha123
    hbstha123 over 2 years
    Worked for me. Thanks!