I am trying to setup Jupyter notebook using Conda, but the python version being used by notebook is not the same as the Conda environment.
I used the following command to create the 'python_jupyter' environment.
conda create -n python_jupyter python=3.6.5 ipykernel
But in the notebook, when I check the python version I get different results depending on how I am checking it. If I check using bash command I get the expected version number 3.6.5, but from python code, I get 3.6.3(which I am guessing the default one came with default Conda installation).
When I go back to the 'base'(default) environment, the version conflict is resolved,
Why there is a python version conflict in the first image, and how do I resolve it?
Edit
When I run 'conda info' on the command line with 'python_jupyter' environment active I see python version is being reported as 3.6.3. But if I run 'python -V' I see python version 3.6.5. Why this discrepancy? Also when I try to update python using 'conda update python' it doesn't prompt me for an update which means conda thinks I am on the latest python version 3.6.5.
The problem was solved by running conda install ipython jupyter on my python_jupyter environment. Since I were able to run jupyter notebook command with the default installation, I was under the assumption that jupyter notebook installation was complete.
conda info reports the version of Python in the base environment, and conda list or conda list -f python or python -V reports the version of Python in the currently active environment, in this case python_jupyter. Source
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/
I created a virtual environment in Conda for 2.7 named PY27 using the method of this thread:
How to run Spyder in virtual environment?
I have currently Python 3.7 installed and I wanted to emulate 2.7. However, although the environment looks as if it's working in the Anaconda Prompt switching correctly to the new PY27, in Spyder I still see 3.7.
In addition, I keep receiving this message:
"Note: you may need to restart the kernel to use updated packages."
I have tried to use the
conda init powershell command but I still receive the same message.
Thank you very much in advance for your help!
The python version in spyder's title bar is the python version of the environment in which spyder is installed. As of spyder 3.3, you can change the python kernel used by spyder without changing the python version of its environment.
First make sure you have spyder-kernels in your PY27 environment.
conda install -c conda-forge spyder-kernels
I'll recommend you install one of the spyder-kernels = 0* versions
Then execute python -c "import sys; print(sys.executable)" in your PY27 env prompt and copy the output. Open spyder and navigate to Tools -> Preferences -> Python interpreter and paste the command output there. You just told spyder to use yourPY27 env's python interpreter.
Reload spyder and you should see you're PY27 env's python kernel show up on the console.
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 installed Anaconda3 so I can create environments and install different packages in each environment. But I fail to understand the difference between the Python in
/usr/bin/python
and
/opt/anaconda3/bin/python
I can seem to access Python 3.6.5 Anaconda from both, why is that? And, what is the difference between both?
Furthermore, I would like to install packages to a single Python environment only.
When you are running python in the terminal, it is looking up your default path to your the python command. In this case, anaconda probably put a line in your shell profile specify the path to the anaconda version, which is why you are seeing it in the interpreter when you run python from either directory.
Secondly, you can set up a conda environment to download app specific dependencies without interfering with your default set up by
conda create --name myenv
source activate myenv
conda install packagename
This will install it in the myenv environment only. To deactivate the environment just run
source deactivate
Here is the documentation on that https://conda.io/docs/user-guide/tasks/manage-environments.html
Judging by your path, you are using Linux which comes with python installed. So /usr/bin/python is default and you have installed the other one later.
For the environments use https://conda.io/docs/user-guide/tasks/manage-environments.html to activate the desired environment, then you can pip install or conda install the packages and it will be places safely only in that environment. Note that spyder icon runs the root environment by default and you have to run it from terminal after activating one of the environments.
Edit:
I'm not sure why you want to use cd to change the python version. I suggest use aliases. I guess you are just changing the path but running the same version of the python anyway. Take a look at this question:
Two versions of python on linux. how to make 2.7 the default
I wanted to create a new virtual environment to install new packages. Following worked for me:
Commands are executed in Jupyter Notebook (OS: Ubuntu 16.04 LTS)
Upgrade pip:
!pip install --upgrade pip
Install virtual environment:
!pip install virtualenv
Select version of Python you want to use in new environment:
I wanted to create an environment with Python version 3. Naming it as Python3_xyz:
!virtualenv -p python3 Python3_xyz
After execution, this will create a folder with the same name in the current working directory (i.e. the location where Jupyter notebook is present)
Create a new option with the name of the created environment
And finally, run the following command:
!python -m ipykernel install --user --name=Python3_xyz
This will create a new option with the name Python3_xyz in the menu from where we create a new notebook.
NOTE: One can run above commands from the terminal as well just don't use '!' before the commands.
This question is bit dated, but since I faced a similar issue, what worked for me might help someone!
I did pip install requests from within my conda environment, but failed to import requests even after trying out everything.
What worked for me: run python -m pip install requests or python3 -m pip install requests within you environment. This installed requests successfully for me.
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.