How to choose your conda environment in Jupyter Notebook - python

I installed Anaconda 5.3 with Python 3.7 (root environment).
After that I created a new environment (py36) using Python 3.6
I activated the new environment with activate py36
conda env list shows that the environement is active.
But when I start Jupyter Notebook (from the Anaconda prompt with jupyter notebook), it seems to use the root environemt, not the activated environment.
How can I use Jupyter Notebook with the new create environment (py36)?

As #Ista mentioned, the documentation gives an easy solution using notebook extensions.
conda install nb_conda
After installing, you have the option in Jupyter Notebook to 'Change kernel' from the 'Kernel' menu in your Jupyter Notebook.

I managed to find a solution for this in a similar problem. The thing is that IPython is not virtualenv-aware, so a workaround (the one that I found to be most comfortable) is to specify custom IPython kernels to avoid having one Jupyter Notebook installation for each virtualenv (or anaconda environments, in your case).
Jupyter relies on some "kernels" (definitions of where to find the python binary) that are stored somewhere in your OS. These files are something like this:
{
"display_name": "NameOfTheKernel",
"language": "python",
"argv": [
"/usr/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
]
Where /usr/bin/python is the path to the python binary that will be executed. However, as these kernels are defined somewhere in your computer by Jupyter, they don't update when you install some other environments (which is the case for anaconda or virtualenv). I found that the easiest way is to define a custom kernel for each environment that you use. Also, doing this supresses the need to activate the environment every single time you want to use it, as it is loaded directly.
The idea is to define a custom kernel so that Jupyter can "see" the environment that you have created with anaconda. For doing so, execute the following line in bash:
ipython kernel install --user --name=NameOfTheKernel
The "NameOfTheKernel" doesn't actually matter that much. If you don't have the ipython package, install it with pip, anaconda, via sudo apt install or whatever.
What this line will do is to define a custom kernel that will be detected by jupyter. For illustration purposes, in Ubuntu, this will be stored in the folder /home/USERNAME/.local/share/jupyter with this data structure:
/home/USERNAME/.local/share/jupyter/kernels/
└── nameofthekernel
├── kernel.json
├── logo-32x32.png
└── logo-64x64.png
Once you have installed the kernel, you have to:
1) Know where your environment has been installed by anaconda. An easy way to do it is to activate your environment in anaconda and, then, write "which python" in the terminal. That will show the full path to the binary.
2) Write that path in the jupyter kernel just created. E.g. using pluma:
pluma /home/USERNAME/.local/share/jupyter/kernels/nameofthekernel/kernel.json
Then, you substitute the path of the python binary you installed with anaconda where /usr/bin/python is.
After this, if Jupyter was running, restart it. This way, the next time you open Jupyter, you can change the kernel (in the notebooks, one of the tabs in the upper part) and you will be using your environment and everything installed alongside that environment.
[TL;DR] I did it with pip, but the steps for anaconda will be more or less the same. The steps are:
#CREATE THE IPYTHON KERNELS
ipython kernel install --user --name=NameOfKernel
#IF PYTHON2 - MODIFY THE KERNELS TO USE THE ANACONDA BINARIES
sed -i -e "s|/usr/bin/python3|/home/${USER}/anaconda/bin/python27|g" /home/$USER/.local/share/jupyter/kernels/nameofkernel/kernel.json
#IF PYTHON3 - MODIFY THE KERNELS TO USE THE ANACONDA BINARIES
sed -i -e "s|/usr/bin/python3|/home/${USER}/anaconda/bin/python36|g" /home/$USER/.local/share/jupyter/kernels/nameofkernel/kernel.json
Or, if you are working with environments:
#CREATE THE IPYTHON KERNELS
ipython kernel install --user --name=NameOfKernel
#IF PYTHON2 - MODIFY THE KERNELS TO USE THE ANACONDA BINARIES
sed -i -e "s|/usr/bin/python3|/home/${USER}/anaconda/envs/nameofenvironment/python27|g" /home/$USER/.local/share/jupyter/kernels/nameofkernel/kernel.json
#IF PYTHON3 - MODIFY THE KERNELS TO USE THE ANACONDA BINARIES
sed -i -e "s|/usr/bin/python3|/home/${USER}/anaconda/envs/nameofenvironment/python36|g" /home/$USER/.local/share/jupyter/kernels/nameofkernel/kernel.json
BEWARE: I DID NOT INSTALL CONDA FOR TESTING THE SOLUTION, SO THE PATHS LEADING TO THE ACTUAL PYTHON BINARIES MIGHT CHANGE. The procedure is the same, however.

Related

conda kernels and conda modules not found in jupyter notebooks when added, but otherwise existant?

There are many questions like this (e.g. Conda environments not showing up in Jupyter Notebook, Python: modul not found after Anaconda installation, https://askubuntu.com/questions/1271137/importing-anaconda-libraries-not-working). I have tried their solutions but to no avail.
In short, I have conda installed. I have installed jupyterlabs and nb_conda_kernels in my base environment. When I create a new conda env via a file, the environment is not listed in jupyter automatically. I can add it via
python -m ipykernel install --user --name <env-name>
but then after I restart jupyter and switch kernels, when I try to import a python module from it, it says module not found.
What do I need to do to fix this? I do not want to add jupyterlabs to every env.
NOTE:
when activating the env in the terminal the path is correct, e.g. /opt/anaconda/anaconda3/envs/<env-name>/bin
when using the env in the terminal, the pack is there.
when using the kernel in Jupyter, the module is not there.

Cannot Open Jupyter Notebook From Python 3.5 Environment [duplicate]

I installed Anaconda (with Python 2.7), and installed Tensorflow in an environment called tensorflow. I can import Tensorflow successfully in that environment.
The problem is that Jupyter Notebook does not recognize the new environment I just created. No matter I start Jupyter Notebook from the GUI Navigator or from the command line within the tensorflow env, there is only one kernel in the menu called Python [Root], and Tensorflow cannot be imported. Of course, I clicked on that option multiple times, saved file, re-opened, but these did not help.
Strangely, I can see the two environments when I open the Conda tab on the front page of Jupyter. But when I open the Files tab, and try to new a notebook, I still end up with only one kernel.
I looked at this question:
Link Conda environment with Jupyter Notebook
But there isn't such a directory as ~/Library/Jupyter/kernels on my computer! This Jupyter directory only has one sub-directory called runtime.
I am really confused. Are Conda environments supposed to become kernels automatically? (I followed https://ipython.readthedocs.io/en/stable/install/kernel_install.html to manually set up the kernels, but was told that ipykernel was not found.)
I don't think the other answers are working any more, as conda stopped automatically setting environments up as jupyter kernels. You need to manually add kernels for each environment in the following way:
source activate myenv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
As documented here:http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments
Also see this issue.
Addendum:
You should be able to install the nb_conda_kernels package with conda install nb_conda_kernels to add all environments automatically, see https://github.com/Anaconda-Platform/nb_conda_kernels
If your environments are not showing up, make sure you have installed
nb_conda_kernels in the environment with Jupyter
ipykernel and ipywidgets in the Python environment you want to access (note that ipywidgets is to enable some Juptyer functionality, not environment visibility, see related docs).
Anaconda's documentation states that
nb_conda_kernels should be installed in the environment from which
you run Jupyter Notebook or JupyterLab. This might be your base conda
environment, but it need not be. For instance, if the environment
notebook_env contains the notebook package, then you would run
conda install -n notebook_env nb_conda_kernels
Any other environments you wish to access in your notebooks must have
an appropriate kernel package installed. For instance, to access a
Python environment, it must have the ipykernel package; e.g.
conda install -n python_env ipykernel
To utilize an R environment, it must have the r-irkernel package; e.g.
conda install -n r_env r-irkernel
For other languages, their corresponding kernels must be installed.
In addition to Python, by installing the appropriatel *kernel package, Jupyter can access kernels from a ton of other languages including R, Julia, Scala/Spark, JavaScript, bash, Octave, and even MATLAB.
Note that at the time originally posting this, there was a possible cause from nb_conda not yet supporting Python 3.6 environments.
If other solutions fail to get Jupyter to recognize other conda environments, you can always install and run jupyter from within a specific environment. You may not be able to see or switch to other environments from within Jupyter though.
$ conda create -n py36_test -y python=3.6 jupyter
$ source activate py36_test
(py36_test) $ which jupyter
/home/schowell/anaconda3/envs/py36_test/bin/jupyter
(py36_test) $ jupyter notebook
Notice that I am running Python 3.6.1 in this notebook:
Note that if you do this with many environments, the added storage space from installing Jupyter into every environment may be undesirable (depending on your system).
The annoying thing is that in your tensorflow environment, you can run jupyter notebook without installing jupyter in that environment. Just run
(tensorflow) $ conda install jupyter
and the tensorflow environment should now be visible in Jupyter Notebooks started in any of your conda environments as something like Python [conda env:tensorflow].
I had to run all the commands mentioned in the top 3 answers to get this working:
conda install jupyter
conda install nb_conda
conda install ipykernel
python -m ipykernel install --user --name mykernel
Just run conda install ipykernel in your new environment, only then you will get a kernel with this env. This works even if you have different versions installed in each envs and it doesn't install jupyter notebook again. You can start youe notebook from any env you will be able to see newly added kernels.
Summary (tldr)
If you want the 'python3' kernel to always run the Python installation from the environment where it is launched, delete the User 'python3' kernel, which is taking precedence over whatever the current environment is with:
jupyter kernelspec remove python3
Full Solution
I am going to post an alternative and simpler solution for the following case:
You have created a conda environment
This environment has jupyter installed (which also installs ipykernel)
When you run the command jupyter notebook and create a new notebook by clicking 'python3' in the 'New' dropdown menu, that notebook executes python from the base environment and not from the current environment.
You would like it so that launching a new notebook with 'python3' within any environment executes the Python version from that environment and NOT the base
I am going to use the name 'test_env' for the environment for the rest of the solution. Also, note that 'python3' is the name of the kernel.
The currently top-voted answer does work, but there is an alternative. It says to do the following:
python -m ipykernel install --user --name test_env --display-name "Python (test_env)"
This will give you the option of using the test_env environment regardless of what environment you launch jupyter notebook from. But, launching a notebook with 'python3' will still use the Python installation from the base environment.
What likely is happening is that there is a user python3 kernel that exists. Run the command jupyter kernelspec list to list all of your environments. For instance, if you have a mac you will be returned the following (my user name is Ted).
python3 /Users/Ted/Library/Jupyter/kernels/python3
What Jupyter is doing here is searching through three different paths looking for kernels. It goes from User, to Env, to System. See this document for more details on the paths it searches for each operating system.
The two kernels above are both in the User path, meaning they will be available regardless of the environment that you launch a jupyter notebook from. This also means that if there is another 'python3' kernel at the environment level, then you will never be able to access it.
To me, it makes more sense that choosing the 'python3' kernel from the environment you launched the notebook from should execute Python from that environment.
You can check to see if you have another 'python3' environment by looking in the Env search path for your OS (see the link to the docs above). For me (on my mac), I issued the following command:
ls /Users/Ted/anaconda3/envs/test_env/share/jupyter/kernels
And I indeed had a 'python3' kernel listed there.
Thanks to this GitHub issue comment (look at the first response), you can remove the User 'python3' environment with the following command:
jupyter kernelspec remove python3
Now when you run jupyter kernelspec list, assuming the test_env is still active, you will get the following:
python3 /Users/Ted/anaconda3/envs/test_env/share/jupyter/kernels/python3
Notice that this path is located within the test_env directory. If you create a new environment, install jupyter, activate it, and list the kernels, you will get another 'python3' kernel located in its environment path.
The User 'python3' kernel was taking precedence over any of the Env 'python3' kernels. By removing it, the active environment 'python3' kernel was exposed and able to be chosen every time. This eliminates the need to manually create kernels. It also makes more sense in terms of software development where one would want to isolate themselves into a single environment. Running a kernel that is different from the host environment doesn't seem natural.
It also seems that this User 'python3' is not installed for everyone by default, so not everyone is confronted by this issue.
To add a conda environment to Jupyter:
In Anaconda Prompt :
run conda activate <env name>
run conda install -c anaconda ipykernel
run python -m ipykernel install --user --name=<env name>
** tested on conda 4.8.3 4.11.0
$ conda install nb_conda_kernels
(in the conda environment where you run jupyter notebook) will make all conda envs available automatically. For access to other environments, the respective kernels must be installed. Here's the ref.
This worked for me in windows 10 and latest solution :
1) Go inside that conda environment ( activate your_env_name )
2) conda install -n your_env_name ipykernel
3) python -m ipykernel install --user --name build_central --display-name "your_env_name"
(NOTE : Include the quotes around "your_env_name", in step 3)
The nb_conda_kernels package is the best way to use jupyter with conda. With minimal dependencies and configuration, it allows you to use other conda environments from a jupyter notebook running in a different environment. Quoting its documentation:
Installation
This package is designed to be managed solely using conda. It should be installed in the environment from which you run Jupyter Notebook or JupyterLab. This might be your base conda environment, but it need not be. For instance, if the environment notebook_env contains the notebook package, then you would run
conda install -n notebook_env nb_conda_kernels
Any other environments you wish to access in your notebooks must have an appropriate kernel package installed. For instance, to access a Python environment, it must have the ipykernel package; e.g.
conda install -n python_env ipykernel
To utilize an R environment, it
must have the r-irkernel package; e.g.
conda install -n r_env r-irkernel
For other languages, their corresponding kernels must be installed.
Then all you need to do is start the jupyter notebook server:
conda activate notebook_env # only needed if you are not using the base environment for the server
# conda install jupyter # in case you have not installed it already
jupyter
Despite the plethora of answers and #merv's efforts to improve them, it still hard to find a good one. I made this one CW, so please vote it to the top or improve it!
This is an old thread, but running this in Anaconda prompt, in my environment of interest, worked for me:
ipython kernel install --name "myenvname" --user
We have struggle a lot with this issue, and here's what works for us. If you use the conda-forge channel, it's important to make sure you are using updated packages from conda-forge, even in your Miniconda root environment.
So install Miniconda, and then do:
conda config --add channels conda-forge --force
conda update --all -y
conda install nb_conda_kernels -y
conda env create -f custom_env.yml -q --force
jupyter notebook
and your custom environment will show up in Jupyter as an available kernel, as long as ipykernel was listed for installation in your custom_env.yml file, like this example:
name: bqplot
channels:
- conda-forge
- defaults
dependencies:
- python>=3.6
- bqplot
- ipykernel
Just to prove it working with a bunch of custom environments, here's a screen grab from Windows:
I ran into this same problem where my new conda environment, myenv, couldn't be selected as a kernel or a new notebook. And running jupter notebook from within the env gave the same result.
My solution, and what I learned about how Jupyter notebooks recognizes conda-envs and kernels:
Installing jupyter and ipython to myenv with conda:
conda install -n myenv ipython jupyter
After that, running jupter notebook outside any env listed myenv as a kernel along with my previous environments.
Python [conda env:old]
Python [conda env:myenv]
Running the notebook once I activated the environment:
source activate myenv
jupyter notebook
hides all my other environment-kernels and only shows my language kernels:
python 2
python 3
R
This has been so frustrating, My problem was that within a newly constructed conda python36 environment, jupyter refused to load “seaborn” - even though seaborn was installed within that environment. It seemed to be able to import plenty of other files from the same environment — for example numpy and pandas but just not seaborn. I tried many of the fixes suggested here and on other threads without success. Until I realised that Jupyter was not running kernel python from within that environment but running the system python as kernel. Even though a decent looking kernel and kernel.json were already present in the environment. It was only after reading this part of the ipython documentation:
https://ipython.readthedocs.io/en/latest/install/kernel_install.html#kernels-for-different-environments
and using these commands:
source activate other-env
python -m ipykernel install --user --name other-env --display-name "Python (other-env)"
I was able to get everything going nicely. (I didn’t actually use the —user variable).
One thing I have not yet figured is how to set the default python to be the "Python (other-env)" one. At present an existing .ipynb file opened from the Home screen will use the system python. I have to use the Kernel menu “Change kernel” to select the environment python.
I had similar issue and I found a solution that is working for Mac, Windows and Linux. It takes few key ingredients that are in the answer above:
To be able to see conda env in Jupyter notebook, you need:
the following package in you base env:
conda install nb_conda
the following package in each env you create:
conda install ipykernel
check the configurationn of jupyter_notebook_config.py
first check if you have a jupyter_notebook_config.py in one of the location given by jupyter --paths
if it doesn't exist, create it by running jupyter notebook --generate-config
add or be sure you have the following: c.NotebookApp.kernel_spec_manager_class='nb_conda_kernels.manager.CondaKernelSpecManager'
The env you can see in your terminal:
On Jupyter Lab you can see the same env as above both the Notebook and Console:
And you can choose your env when have a notebook open:
The safe way is to create a specific env from which you will run your example of envjupyter lab command. Activate your env. Then add jupyter lab extension example jupyter lab extension. Then you can run jupyter lab
While #coolscitist's answer worked for me, there is also a way that does not clutter your kernel environment with the complete jupyter package+deps.
It is described in the ipython docs and is (I suspect) only necessary if you run the notebook server in a non-base environment.
conda activate name_of_your_kernel_env
conda install ipykernel
python -m ipykernel install --prefix=/home/your_username/.conda/envs/name_of_your_jupyter_server_env --name 'name_of_your_kernel_env'
You can check if it works using
conda activate name_of_your_jupyter_server_env
jupyter kernelspec list
First you need to activate your environment .
pip install ipykernel
Next you can add your virtual environment to Jupyter by typing:
python -m ipykernel install --name = my_env
Follow the instructions in the iPython documentation for adding different conda environments to the list of kernels to choose from in Jupyter Notebook. In summary, after installing ipykernel, you must activate each conda environment one by one in a terminal and run the command python -m ipykernel install --user --name myenv --display-name "Python (myenv)", where myenv is the environment (kernel) you want to add.
Possible Channel-Specific Issue
I had this issue (again) and it turned out I installed from the conda-forge channel; removing it and reinstalling from anaconda channel instead fixed it for me.
Update: I again had the same problem with a new env, this time I did install nb_conda_kernels from anaconda channel, but my jupyter_client was from the conda-forge channel. Uninstalling nb_conda_kernels and reinstalling updated that to a higher-priority channel.
So make sure you've installed from the correct channels :)
I encountered this problem when using vscode server.
In the conda environment named "base", I installed the 1.2.0 version of opennmt-py, but I want to run jupyter notebook in the conda environment "opennmt2", which contains code using opennmt-py 2.0.
I solved the problem by reinstalling jupyter in conda(opennmt2).
conda install jupyter
After reinstalling, executing jupyter notebook in the opennmt2 environment will execute the newly installed jupyter
where jupyter
/root/miniconda3/envs/opennmt2/bin/jupyter
/root/miniconda3/bin/jupyter
For conda 4.5.12, what works for me is (my virtual env is called nwt)
conda create --name nwt python=3
after that I need to activate the virtual environment and install the ipykernel
activate nwt
pip install ipykernel
then what works for me is:
python -m ipykernel install --user --name env_name --display-name "name of your choosing."
As an example, I am using 'nwt' as the display name for the virtual env. And after running the commands above. Run 'jupyter notebook" in Anaconda Prompt again. What I get is:
Using only environment variables:
python -m ipykernel install --user --name $(basename $VIRTUAL_ENV)
I just wanted to add to the previous answers: in case installing nb_conda_kernels, ipywidgets and ipekernel dosen't work, make sure your version of Jupyter is up to date. My envs suddenly stopped showing up after a period of everything working fine, and it resumed working after I simply updated jupyter through the anaconda navigator.
In my case, using Windows 10 and conda 4.6.11, by running the commands
conda install nb_conda
conda install -c conda-forge nb_conda_kernels
from the terminal while having the environment active didn't do the job after I opened Jupyter from the same command line using conda jupyter notebook.
The solution was apparently to opened Jupyter from the Anaconda Navigator by going to my environment in Environments: Open Anaconda Navigator, select the environment in Environments, press on the "play" button on the chosen environment, and select 'open with Jupyter Notebook'.
Environments in Anaconda Navigator to run Jupyter from the selected environment

Running multiple Python versions (2.x. 3.y, 3.z) on Jupyter Notebook via multiple kernels

Apologies first-hand if I've done some silly mistake in the below raised issue. I have been stuck on this since quite some time, trying to successfully install multiple Python versions (via separate virtual environments) and run Jupyter notebook with all three versions in Change kernel switch.
AIM:
Setup Anaconda with Python 3.5.6 as default and create two virtual environments with Python 2.7.13 and Python 3.7.3 version and to be able to switch between these three Python versions on a Jupyter Notebook on Windows.
Process Followed: What I did (and ended in mess):
I first successfully installed Anaconda3 with Python 3.5.6 as default (installed in C:\ProgramData\Anaconda3) and set the PATH variables. Jupyter Notebook was up and running with an ipython kernel running from 'base' (or root) from
(base) jupyter kernelspec list
Available kernels:
python_3.5.6 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.5.6
and kernel.json file was also mapped to the correct python version.
.
Then I created my first virtual environment (Python_2.7.13_ENV):
(base) conda create --p C:\ProgramData\Anaconda3\envs\Python_2.7.13_ENV python=2.7.13
and installed jupyter on it
(base) activate Python_2.7.13_ENV
(Python_2.7.13_ENV) conda install notebook ipykernel
(Python_2.7.13_ENV) python -m ipykernel install --p C:\Users\username\AppData\Roaming\jupyter\kernels\ --name Python_2.7.13_ENV --display-name "python_2.7.13"
I used the prefix notation as the default installation syntax was installing it for the root user and I wanted it to install it only for a specific user.
And this worked like a charm. The updated jupyter kernelspec read:
(base) jupyter kernelspec list
Available kernels:
python_3.5.6 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.5.6
python_2.7.13 C:\Users\username\AppData\Roaming\jupyter\kernels\python_2.7.13
and kernel.json file was also mapped to the correct python version ("C:\\ProgramData\\Anaconda3\\envs\\Python_2.7.13\\python.exe")
This was also working fine. I could open a file in jupyter and succesfully switch between the two kernels.
.
Than I followed the same steps for creating my second virtual environment (Python_3.7.3_ENV):
Now, the updated kernelspec read:
(base) jupyter kernelspec list
Available kernels:
python_3.5.6 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.5.6
python_2.7.13 C:\Users\username\AppData\Roaming\jupyter\kernels\python_2.7.13
python_3.7.3 C:\Users\username\AppData\Roaming\jupyter\kernels\python_3.7.3
and the kernel.json was also mapped to the correct python version.
Problem:
Both the virtual envs were created successfully.
Now when I run a jupyter notebook and try to switch to Python 2.7.13 kernel, it works fine, but shows a ImportError: DLL load failed (due to some import issue in zmq) on switching to Python_3.7.3 kernel.
However, when I first activate the Python_3.7.3_ENV virtual env and then load the jupyter notebook, I am able to switch between all three Python versions.
Can anybody provide a solution on how to toggle between all three versions without activating the virtual env beforehand if it's possible as I am able to do it with Py 2.7 & Py 3.5 versions.
PS. I have set the 'open with' default on right-click on a ipynb file to jupyter-notebook.exe.
I have similar setting: default python 3.7, env: python 3.6 and python 2.7.
set multiple versions of python/ipykernels running on jupyter notebook
Check you Anaconda version, if it is '>= 4.1.0', it is easier since after 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. If the Anaconda version is lower than 4.1.0 or just want to manually do that, you can reference here.
For python2.7 and adding the ipykernel to jupyter notebook, shown as 'Python (py27)'
conda create -n py27 python=2.7 ipykernel
conda activate py27
python -m ipykernel install --user --name=py27 --display-name "Python (py27)"
For python3.6 and adding the ipykernel to jupyter notebook, shown as 'Python (py36)'
conda create -n py36 python=3.6 ipykernel
conda activate py36
python -m ipykernel install --user --name=py36 --display-name "Python (py36)"
Then we can check if they works well not. You can deactivate from specific env and go back to the base env, and type jupyter notebook (or use graphic shortcut for jupyter notebook anaconda provides)and then you can create 'new' notebook, you can find apart from default Python 3, there also shows Python (py27) and Python (py36).
I was making mistake at the beginning to check which python was running:
Do not just use !python --version in jupyter notebook no matter which version kernel you are using.Because this is just like you running command in base env, it will always show the default env python version, which in my case is python 3.7.x.
What you can do to:
from platform import python_version
print(python_version())
## or this one, which ever you like.
import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)
You will get right python version accordingly.
Show list of jupyter notebook kernels
jupyter kernelspec list (in base env, it will show all kernels name)
If you want to remove specific kernel, you can use:
jupyter kernelspec uninstall <kernel name>
Another way to confirm the python version is right, you can check the ipykernel json file, if the path to start the python is right or not.
To do this: use jupyter kernelspec list, so you can get to know the path of json file. (for example, in my case I can get the path for py27: C:\Users\username\AppData\Roaming\jupyter\kernels\py27). Then, you can go to the dir/path, and you can check what is shown in kernel.json
for the argv, it should show the path to access related python version. For example,
for py27, it will show like "C:\\Users\\username\\Anaconda3\\envs\\py27\\python.exe",
for py36, it will show like "C:\\Users\\username\\Anaconda3\\envs\\py36\\python.exe"
Last thing about setting PATH env variables: do not add Anaconda to the Windows PATH because this can interfere with other software. Instead, open Anaconda with the Start Menu and select Anaconda Prompt, or use Anaconda Navigator. More information can check the Anaconda official docs.
Hope this may be helpful if you meet similar issues.
PS: I was always using Unix like system, not so used to Windows env setting. That is what I met for setting different kernels for different anaconda envs and summarized it. Hope it helps.
After here and there, the only to make this work is having that virtual environment activated.
Following the above steps, I have installed Python 2.7.13, 3.5.6 and 3.7.3 version. My default python is Python 3.5.6 while the other two versions are installed in two virtual environments - Python_2.7.13_ENV and Python_3.7.3_ENV respectively.
How to use them ?
For Python 2.7 and 3.5, just use the Jupyter notebooks normally as you would. Since the default python is set to Python 3.5.6 there is no problem in switching between two versions using Change Kernel option in Jupyter Notebook Toolbar.
For Python 3.7 we first need to activate Python_3.7.3_ENV virtual environemnt and then we can switch between all three versions succesfully using Change Kernel option in Jupyter Notebook.

iPython with different env (using anaconda)

I have just created a new env with python 3.5 using anaconda (called it python35). My root env points to python 2.7.11. I cant seem to launch ipython with this new env, here is what I did
1. in conda prompt, activate required env: activate python35
2. confirm the version: python --version
3. launch ipython: ipython notebook
4. open a notebook and do: import sys; print (sys.version)
Step #2, returns 3.5 but step #4 always gives me 2.7.11, its like ipython is picking up python version from root env. How do I fix this. Thanks for any help!
This question is related but I have already done what it suggests.
AFAIK, different environments in anaconda (and in venv as well) are activated by prepending env path to $PATH environment variable. It means, that if some file (eg, ipython) is not found in env path (the first entry of $PATH), the system searches for it in consequent entries of $PATH and finds it in root environment (that stays in $PATH). To fix the behavior, you need to install its own copy of ipython to anaconda env:
In command prompt, activate the environment: source activate python35 (or simply activate python35, depending on the OS)
While in environment, issue the command conda install ipython-notebook
An addition to Andrey Sobolev solution,you should switch to conda install ipython for higher python3.x version and log out the environment by Ctrl+D or conda deactivate then activate again
I could confirm the solution above (basically install notebook in active environment) in my case. Two updates from my side:
Since Anaconda 4.4 (?) ipython notebook is now jupyter notebook. So I had to install jupyter instead. I guess, deactivate and activate was required afterwards to get the path variables in notebook updated -> checked by python -c "import sys; print(sys.path)" or corresponding command in notebook.
If you aren't using a isolated anaconda environment, you may eventual calling the notebook package from your native OS-Python installation, instead the desired from Anaconda. Similarly, I had trouble with cmake or cxx, when I try to compile in Anaconda Environment - the diffent root folder might found in linux bash with e.g "which jupyter"

Execute Python script within Jupyter notebook using a specific virtualenv

I would like to execute a long running Python script from within a Jupyter notebook so that I can hack on the data structures generated mid-run.
The script has many dependencies and command line arguments and is executed with a specific virtualenv. Is it possible to interactively run a Python script inside a notebook from a specified virtualenv (different to that of the Jupyter installation)?
Here's what worked for me (non conda python):
(MacOS, brew version of python. if you are working with system python, you may (will) need prepend each command with sudo)
First activate virtualenv. If starting afresh then, e.g., you could use virtualenvwrapper:
$ pip install virtualenvwrapper
$ mkvirtualenv -p python2 py2env
$ workon py2env
# This will activate virtualenv
(py2env)$
# Then install jupyter within the active virtualenv
(py2env)$ pip install jupyter
# jupyter comes with ipykernel, but somehow you manage to get an error due to ipykernel, then for reference ipykernel package can be installed using:
(py2env)$ pip install ipykernel
Next, set up the kernel
(py2env)$ python -m ipykernel install --user --name py2env --display-name "Python2 (py2env)"
then start jupyter notebook (the venv need not be activated for this step)
(py2env)$ jupyter notebook
# or
#$ jupyter notebook
In the jupyter notebook dropdown menu: Kernel >> Change Kernel >> <list of kernels> you should see Python2 (py2env) kernel.
This also makes it easy to identify python version of kernel, and maintain either side by side.
Here is the link to detailed docs:
http://ipython.readthedocs.io/en/stable/install/kernel_install.html
A bit more simple solution to get notebook kernels available in other notebooks.
I'm using Linux + virtualenv + virtualenvwrapper. If you are using different setup, change some commands to the appropriate ones, but you should get the idea.
mkvirtualenv jupyter2
workon jupyter2
(jupyter2) pip install jupyter
(jupyter2) ipython kernel install --name "jupyter2_Python_2" --user
last command creates ~/.local/share/jupyter/kernels/jupyter2\ python\ 2/ directory
same stuff for 3
mkvirtualenv -p /usr/bin/python3 jupyter3
// this uses python3 as default python in virtualenv
workon jupyter3
(jupyter3) pip install jupyter
(jupyter3) ipython kernel install --name "jupyter3_Python_3" --user
When done you should see both kernels, no matter what env are you using to start jupyter.
You can delete links to kernels directly in ~/.local/share/jupyter/kernels/.
To specify location provide options to ipython kernel install (--help) or just copy directories from ~/.local/share/jupyter/kernels/ to ~/envs/jupyter3/share/jupyter if you want to run multiple kerenels from one notebook only.
I found this link to be very useful:
https://ocefpaf.github.io/python4oceanographers/blog/2014/09/01/ipython_kernel/
Make sure that you pip install jupyter into your virtualenv. In case the link goes away later, here's the gist:
You need to create a new kernel. You specify your kernel with a JSON file. Your kernels are usually located at ~/.ipython/kernels. Create a directory with the name of your virtualenv and create your kernel.json file in it. For instance, one of my paths looks like ~./ipython/kernels/datamanip/kernel.json
Here's what my kernel.json file looks like:
{
"display_name": "Data Manipulation (Python2)",
"language": "python",
"codemirror_mode": {
"version": 3,
"name":"ipython"
},
"argv": [
"/Users/ed/.virtualenvs/datamanip/bin/python",
"-c",
"from IPython.kernel.zmq.kernelapp import main; main()",
"-f",
"{connection_file}"
]
}
I am not certain exactly what the codemirror_mode object is doing, but it doesn't seem to do any harm.
It is really simple, based on the documentation
You can use a virtualenv for your IPython notebook. Follow the following steps, actually no need for step one, just make sure you activated your virtualenv via source ~/path-to-your-virtualenv/
Install the ipython kernel module into your virtualenv
workon my-virtualenv-name # activate your virtualenv, if you haven't already
pip install ipykernel
(The most important step) Now run the kernel "self-install" script:
python -m ipykernel install --user --name=my-virtualenv-name
Replacing the --name parameter as appropriate.
You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able to switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.
#singer's solution didn't work for me. Here's what worked:
. /path/to/virtualenv/.venv/bin/activate
python -m ipykernel install --user --name .venv --display-name .venv
Reference: Kernels for different environments (official docs)
the nb_canda is useful:
conda install nb_conda
so,you can create and select your own python kernel with conda virtual environment,and manage the packages in venv
Screenshots
List item
conda environment manager Conda tab in jupyter notebook allows you to manage your environments right from within your notebook.
Change Kernel
You can also select which kernel to run a notebook in by using the Change kernel option in Kernel menu

Categories