I installed Python through the latest Anaconda distribution. The default Python version is 3.6 which Anaconda installs in the root environment. However, some of the libraries I need require version 3.5. I created a new environment py3_5 and installed Python 3.5 along with those libraries. I want to choose which version (environment) I want to launch Spyder or Jupyter Notebook, but when I try to launch 3.5 from the Anaconda prompt:
C:\Users\me> activate py3_5
(py3_5) C:\Users\me> spyder
Spyder launches with Python 3.6. The same thing happens with Jupyter Notebooks. I would prefer the flexibility of choosing the version at the time I launch the application.
Look at the issue here, everything is explained.
This is my habit - I frequently make envs and install IPython in them,
and register a kernelspec for the env. What I don't do is install the
notebook server multiple times.
You can install Ipython (just the kernel, not the notebook) in every environment you have OR change the python path in the kernel.json file.
Related
I am running some codes in the Jupyter Notebook and found this issue in my machine.
When I execute "print(sys.version)" I got my python version that my jupyter kernel using is 3.8.2 which is as below.
And when I execute "!python -V" I got another python version which is 3.9.1.
When I type "!jupyter kernelspec list" I only have 1 python 3 for the jupyter kernel.
(Ignored the TFOD which was my virtual env that I created for last project)
Or can refer the image below for the output.
enter image description here
However, in my machine, I only installed two python version which is 3.8 and 3.9 as below. And I am using 3.9 as my default.
enter image description here
I actually found the code "!python -m ipykernel install --user --name=Python_39" can solve my problem and let me created another kernel that using python 3.9.
But I still keen why this could happen and how to change(or add) the Jupyter Kernel that respective to the python that installed in my machine through appropriate way?
Because py3.9 is my active environment as I installed many libraries and wish to run on it rather than other (py3.8) version in the jupyter notebook.
It is not mentioned how you create your Jupyter notebook server, but I would suggest you do that from python's virtual environment
This should activate an environment of python which when activated, gives you a consistent view of a specific python version and the dependencies (python libraries) you installed on this environment.
when the virtualenv is activated, you can just use python instead of python3.x as the activated virtualenv tells your shell to refer to its own version of python when python is called.
if your jupyter is instantiated from a virtualenv, it will work with this specific python version - as it is the only one it knows.
if your project already uses a venv, you can instantiate jupyter from it each time.
This is a snippet from what I would do on a linux shell:
// create a new venv:
shell# python3.9 -m venv ./myenv/
// activate the venv to operate on the shell:
shell# source ./myenv/bin/activate
// install jupyter to the venvs libraries
(myvenv) shell# python3.9 -m pip install jupyter
// run the jupyter server
(myvenv) shell# python -m jupyter notebook --port=8888 --no-browser --ip=0.0.0.0 --allow-root"
The workspace\directories you see from your notebook will be the one you instantiated it in.
you can access the jupyter notebook's user interface from your browser: http://127.0.0.1:8888
You can also try this article or similar: https://www.codingforentrepreneurs.com/blog/install-jupyter-notebooks-virtualenv/
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.
I have got two anaconda environments. The base one and my testing environment called testenv. This environment works without an issue for example with the PyCharm IDE.
When starting a jupyter server straight from the pictured directory by issuing jupyter notebook my scripts/notebooks complains about missing packages.
This tells me it is using base environment instead of my testenv environment. But as the screenshot show, its not the one thats activated.
So the question is: How can I set the virtual environment used by jupyter?
Used versions:
Windows 10 Pro x64
Jupyter Notebook 5.7.4
Python 3.7.1
Conda 4.5.12
[Edit] It turned out that jupyter was not even installed in my environment. I was assuming to be able to use the installation coming from base. After installing jupyter within my environment everything was working as expected.
This is usually foolproof for me. However, it does not allow switching virutal env on the fly. Conda docs.
Windows
$ activate testenv
$ jupyter notebook
Linux
$ source activate testenv
$ jupyter notebook
If you'd like to be able to switch between your envs even on the fly from within Jupyter, this should also do the trick.
How do I add python3 kernel to jupyter (IPython)
I have created a new environment, specifying the Python version as 2.7. This has worked correctly as when I activate the environment and I run on the command line:
python --version
It returns:
Python 2.7.15
However, when I then open Spyder from the command line in the same environment, Spyder is still using Python 3. I was expecting the Python version in Spyder to match the Python version in the environment. So what could be stopping it working as I expected? I am on Linux Ubuntu on a virtual machine.
Maybe you are launching it from the wrong environment.
Assuming you're using Anaconda, you should activate the py2 environment and launch spyder from it, but being sure to have installed it in there before.
Here a simple sequence as example:
1. Create a new env py2 based
>conda create -n <NAME_OF_YOUR_ENV> python=2.7
2. activate the environment
>activate <NAME_OF_YOUR_ENV>
3. install Spyder
<NAME_OF_YOUR_ENV> >conda install spyder
4. launch spyder
<NAME_OF_YOUR_ENV> >spyder
It should be enough
Do you have several python installed on your system? I guess you have 2.7.15 installed by default with your system and you installed python 3 (via Anaconda?) without adding it in your path.
Are you in spyder's directory when you launch it (and launch spyder with ./spyder)?
It seems I missed a step to install the Spyder package within the environment.
conda install spyder
It works as expected now.
I uninstalled Anaconda 2.3 with Python 3.4.3 and then installed the same Anaconda version with Python 2.7.10.
When I open a notebook via $ ipython notebook "Example Notebook.ipynb" it tries to use the python3 kernel as opposed to opening with the installed python2. Of course I get the error python3 kernel not found.
How can I get ipython notebooks to open with the python2 kernel? I've tried to uninstall ipython and ipython notebook, then delete .ipython and .jupyter from my user directory in case there were any defaults set in these folders, then reinstalled both. Still get the same problem.
Any help would be appreciated
You can install several python versions alongside each other. Just create another environment (replace "all my packages" with the names of the packages).
conda create --name mypy_27 python = 2.7
or
conda create --name mypy_34 python = 3.4
afterwards you can activate the environments by typing
source activate mypy_34
if you then do
conda install "all your packages"
you install the desired packages in the active environment.
You can do much more.