How to know which Python is running in Jupyter notebook? - python

I use Jupyter notebook in a browser for Python programming, I have installed Anaconda (Python 3.5). But I'm quite sure that Jupyter is running my python commands with the native python interpreter and not with anaconda. How can I change it and use Anaconda as interpreter?

from platform import python_version
print(python_version())
This will give you the exact version of python running your script. eg output:
3.6.5

import sys
sys.executable
will give you the interpreter. You can select the interpreter you want when you create a new notebook. Make sure the path to your anaconda interpreter is added to your path (somewhere in your bashrc/bash_profile most likely).
For example I used to have the following line in my .bash_profile, that I added manually :
export PATH="$HOME/anaconda3/bin:$PATH"
EDIT: As mentioned in a comment, this is not the proper way to add anaconda to the path. Quoting Anaconda's doc, this should be done instead after install, using conda init:
Should I add Anaconda to the macOS or Linux PATH?
We do not recommend adding Anaconda to the PATH manually. During
installation, you will be asked “Do you wish the installer to
initialize Anaconda3 by running conda init?” We recommend “yes”. If
you enter “no”, then conda will not modify your shell scripts at all.
In order to initialize after the installation process is done, first
run source <path to conda>/bin/activate and then run conda init

import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)
Seen below :- output when i run JupyterNotebook outside a CONDA venv
/home/dhankar/anaconda2/bin/python
2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:42:40)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
Seen below when i run same JupyterNoteBook within a CONDA Venv created with command --
conda create -n py35 python=3.5 ## Here - py35 , is name of my VENV
in my Jupyter Notebook it prints :-
/home/dhankar/anaconda2/envs/py35/bin/python
3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
also if you already have various VENV's created with different versions of Python you switch to the desired Kernel by choosing KERNEL >> CHANGE KERNEL from within the JupyterNotebook menu...
JupyterNotebookScreencapture
Also to install ipykernel within an existing CONDA Virtual Environment -
http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments
Source --- https://github.com/jupyter/notebook/issues/1524
$ /path/to/python -m ipykernel install --help
usage: ipython-kernel-install [-h] [--user] [--name NAME]
[--display-name DISPLAY_NAME]
[--profile PROFILE] [--prefix PREFIX]
[--sys-prefix]
Install the IPython kernel spec.
optional arguments:
-h, --help show this help message and exit
--user Install for the current user instead of system-wide
--name NAME Specify a name for the kernelspec. This is needed to
have multiple IPython kernels at the same time.
--display-name DISPLAY_NAME
Specify the display name for the kernelspec. This is
helpful when you have multiple IPython kernels.
--profile PROFILE Specify an IPython profile to load. This can be used
to create custom versions of the kernel.
--prefix PREFIX Specify an install prefix for the kernelspec. This is
needed to install into a non-default location, such as
a conda/virtual-env.
--sys-prefix Install to Python's sys.prefix. Shorthand for
--prefix='/Users/bussonniermatthias/anaconda'. For use
in conda/virtual-envs.

You can check python version using
!python -V
or
!python --version
Python 3.6.5 :: Anaconda, Inc.
You can add Conda environment to your jupyter notebook
Step 1: Create a Conda environment.
conda create --name firstEnv
Step 2: Activate the environment using the command as shown in the console.
conda activate firstEnv
conda install -c conda-forge <package-name>
E.g.
conda install -c conda-forge tensorflow
Step 3: set this conda environment on your jupyter notebook
conda install -c anaconda ipykernel
python -m ipykernel install --user --name=firstEnv
Step 4: Just check your Jupyter Notebook, to see firstEnv
You can refer this article
https://medium.com/#nrk25693/how-to-add-your-conda-environment-to-your-jupyter-notebook-in-just-4-steps-abeab8b8d084

Looking the Python version
Jupyter menu help/about will show the Python version

Assuming you have the wrong backend system you can change the backend kernel by creating a new or editing the existing kernel.json in the kernels folder of your jupyter data path jupyter --paths. You can have multiple kernels (R, Python2, Python3 (+virtualenvs), Haskell), e.g. you can create an Anaconda specific kernel:
$ <anaconda-path>/bin/python3 -m ipykernel install --user --name anaconda --display-name "Anaconda"
Should create a new kernel:
<jupyter-data-dir>/kernels/anaconda/kernel.json
{
"argv": [ "<anaconda-path>/bin/python3", "-m", "ipykernel", "-f", "{connection_file}" ],
"display_name": "Anaconda",
"language": "python"
}
You need to ensure ipykernel package is installed in the anaconda distribution.
This way you can just switch between kernels and have different notebooks using different kernels.

Creating a virtual environment for Jupyter Notebooks
A minimal Python install is
sudo apt install python3.7 python3.7-venv python3.7-minimal python3.7-distutils python3.7-dev python3.7-gdbm python3-gdbm-dbg python3-pip
Then you can create and use the environment
/usr/bin/python3.7 -m venv test
cd test
source test/bin/activate
pip install jupyter matplotlib seaborn numpy pandas scipy
# install other packages you need with pip/apt
jupyter notebook
deactivate
You can make a kernel for Jupyter with
ipython3 kernel install --user --name=test

Check the Python Version
import sys
print(sys.version)

Related

Anaconda environment with a python specific [duplicate]

I have miniconda3 installed and since I would like to have an environment with python version 3.3.0, I create it via
conda create -n "myenv" python=3.3.0
However when I activate the environment via
conda activate myenv
python has version 2.7.15 and path
/usr/bin/python
and ipython has python version 3.6.8 and path
/home/myname/.local/bin/ipython
I can access the correct python with python3 which is at
/home/myname/miniconda3/envs/myenv/bin/python3
however, ipython3 has python version 3.6.8 again.
conda install python=3.3.0
left the situation unchanged.
A solution would be to open IPython via
python3 -m IPython
however, while this works fine for python here I get the error message
/home/myname/miniconda3/envs/myenv/bin/python3: No module named IPython
Is it possible to access with the commands python and ipython both python version 3.3.0 in that specific environment, i.e. not by setting an alias in the .bashrc?
EDIT:
Turns out that this problem does not occur if you select version 3.3 instead of 3.3.0 together with #ilmarinen's answer
conda create -n "myenv" python=3.3 ipython
everything works fine and python as well as ipython result to version python 3.3.5.
You need to install ipython as well into your given environment
conda create -n "myenv" python=3.3.0 ipython
The conda environments are prepended to your PATH variable, so when you are trying to run the executable "ipython", Linux will not find "ipython" in your activated environment (since it doesn't exist there), but it will continue searching for it, and eventually find it wherever you have it installed.
To create an environment named py33 with python 3.3.0, using the channel conda-forge and a list of packages:
conda create -y --name py33 python==3.3.0
conda install -f -y -q --name py33 -c conda-forge --file requirements.txt
conda activate py33
...
conda deactivate
Alternatively you can use
conda env create -f environment.yml
for using an environment.yml file instead of requirements.txt:
name: py33
channels:
- conda-forge
dependencies:
- python==3.3.0
- ipython
Use this command to remove the environment:
conda env remove -n py33
I had similar issue. And I could't find many useful discussions.
The problem for me was I have alias pointing python to miniconda python hardcoded in my shell config file when I execute conda init zsh. Somehow the init process copies the alias and always reload that, thus overwrites the "correct" version.
After conda create -n py27 python=2.7 (my system default is 3.6), the version was correctly installed at miniconda3/envs/py27/bin/python. But the activated evironment python was not pointing to it, as indicated by which python,even if I deleted updated my shell config.
In the end it was solved by 'reverse' conda init (remove the generated conda function in .zshrc), remove alias, and re-init.
I guess other shell is using the same mechanism.

Jupyter Notebook kernel not upgrading

I currently use python 3.7.4. I am trying to upgrade to python 3.8 by using a new environment. The following steps were executed in Anaconda prompt:
conda install ipykernel
conda create -n py38 python=3.8
Activate py38
pip install ipykernel
python -m ipykernel install --name py38
Then, I restarted Anaconda, and ran "jupyter notebook". In the top right corner within Jupyter notebook, the kernel shows "py38".
However, when running the following code :
import sys
print(sys.version)
It returns:
3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]
Run conda install nb_conda from the anaconda prompt in your py38 environment.
Launch a notebook with jupyter notebook in your activate conda environment in this case py38.
Then create a new notebook and you can select the environment you want in this case py38.
After you have done this try running:
import sys
print(sys.version)
and see the output.

Access anaconda modules from global to virtual env

I have successfully installed the Anaconda distribution to the default path (which includes pandas) for Python 3.7 following instructions on anaconda documentation.
Pandas import runs successfully after loading the base env that was created automatically during the Anaconda installation:
~$ conda activate
(base): ~$ python
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
>>> import pandas as pd
>>>
I have an existing virtual environment py37-venv within which I would like to use Pandas:
(base) ~/myproject$ source py37-venv/bin/activate
(py37-venv) (base) ~/myproject$ python
Python 3.7.3 (default, Jul 4 2019, 11:23:49)
[GCC 5.4.0 20160609] on linux
>>> import pandas as pd
ModuleNotFoundError: No module named 'pandas'
How can I use Pandas (and other Anaconda packages) within my virtual environment. Do I need to install Anaconda again within my virtual env?
Following advise from similar posts did not help. e.g:
Create an Anaconda env: but my env was already existing before installing Anaconda.
Set
export PATH="/myrootpath/anaconda3/etc/profile.d/conda.sh:$PATH" but getting the same result.
Copying: /myrootpath/anaconda3/pkgs/pandas-0.24.2-py37he6710b0_0 to myproject/py37-venv/lib/python3.7/site-packages/pandas-0.24.2-py37he6710b0_0
If your project doens't have any dependencies apart from what's already included in Anaconda, I'd imagine that you can just run your code without activating your virtualenv environment.
Other than that the easisest thing to do would be to create a new conda environment and install the dependencies of your project into the newly created env.
Anaconda is not just a collection of packages, it also comes with a command line tool called conda.
You can create a new environment with conda like this conda create -n <env_name> python=3.7 Then activate the new env with conda activate <env_name> and install any packages you need with conda install <package> (note: this will install the package into the currently active env, which means that it'll install it to the root env if you don't have another env activated)
As a side note: you don't have to use conda to install packages in a conda env, pip works just as well. So if your project has a requirements.txt (or something similar) you can just run pip install -r requirements.txt inside your conda env.

How to add python 3.6 kernel alongside 3.5 on jupyter

I am trying to test some code on Python 3.6 but my system default python version is python 3.5 I have installed python 3.6 and have tried to follow the jupyter documentation to install a new python kernel
python3.6 -m pip install ipykernel
python3.6 -m ipykernel install --user
But it didn't work since it continues to show a single kernel in the menu: Python3
Has anyone managed to have both 3.5 and 3.6 in the same jupyter installation?
one option is to create a kernel that you can use in jupyter notebook.
you can do this inside the virtual environment:
Open up your terminal and enter the following line by line
virtualenv -p python3.6 py_36_env
source py_36_env/bin/activate
pip install ipykernel
python -m ipykernel install --user --name=py_36_env
jupyter notebook
Then in jupyter notebook you can select the 3.6 environment (py_36_env) from the 'New' drop down menu shown above or from the 'Kernel' drop down menu within a given jupyter notebook.
Steps to install Python 3.6 in Windows
Open command prompt
conda install ipykernel
conda create -n Python3.6Test python=3.6
Activate Python3.6Test
pip install ipykernel
python -m ipykernel install --name Python3.6Test
Here you go with 3.6
C:\ProgramData\jupyter\kernels\Python3.6Test
Now open Jupitor Notebook and see, you will get the Python3.6Test option
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.6 Naming it as Python_3_6:
!virtualenv -p python3.6 Python_3_6
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=Python_3_6
This will create a new option with the name Python_3_6 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 is an old question, but like some commenters here I wanted to change the default jupyter completely without using a venv. It seems jupyter keeps this in a json file called kernel.json:
{
"display_name": "Python 3",
"language": "python",
"argv": [
"/path/to/some/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
]
}
There may be several paths containing this file which are searched in order. To find out what they are on your machine, run:
ipython kernel install --prefix tmp
ipython will throw a warning that the config file is being created in tmp and might not be found, but it will also output the paths it uses to find kernel.json, which you can then edit manually.
You should create a virtualenv for each Python version you are using. Create one for Python 3.5:
virtualenv -p /usr/bin/python3.5 py35
source py35/bin/activate
pip install jupyter
jupyter # Jupyter running Python 3.5
deactivate # Leave virtualenv
And create one for Python 3.6:
virtualenv -p /usr/bin/python3.6 py36
source py36/bin/activate
pip install jupyter
jupyter # Jupyter running Python 3.6
upper solution does not work in my case!!
I want to execute code in python3.8 but unfortunately I previous install pytohn3.6
I create, activate virtualenv of python3.8 but during lib installation all package installed in virtualenv of python3.6
Solution is that I deleted python3.6 env & Setup a new way of Jupyter kernel. After that mine code execute happily & required lib install in virtualenv of python3.8
For Windows users:
To add python 3.x ipykernel to jupyter notebook with name "Python 3.x", shown as 'Python 3.x', open your terminal (such as Anaconda Prompt), in base env and enter the following line by line.
For Python 3.10 ipykernel:
conda create -n py310 python=3.10 ipykernel
conda activate py310
python -m ipykernel install --user --name=py310 --display-name "Python 3.10"
For Python 3.7.7 ipykernel:
conda create -n py377 python=3.7.7 ipykernel
conda activate py377
python -m ipykernel install --user --name=py377 --display-name "Python 3.7"
Then close the specific env and go back to the base env, and type in jupyter notebook. Once it opens you'll see the following when you click the "New" drop down menu in the top right corner:
Then to check if the correct Python is installed, open a new notebook and run:
from platform import python_version
print(python_version())
For Python 3.10:
For Python 3.7.7
To remove a specific kernel, you can run the following in your terminal in the base env:
jupyter kernelspec uninstall <kernel name>

Using both Python 2.x and Python 3.x in IPython Notebook

I use IPython notebooks and would like to be able to select to create a 2.x or 3.x python notebook in IPython.
I initially had Anaconda. With Anaconda a global environment variable had to be changed to select what version of python you want and then IPython could be started. This is not what I was looking for so I uninstalled Anaconda and now have set up my own installation using MacPorts and PiP. It seems that I still have to use
port select --set python <python version>
to toggle between python 2.x and 3.x. which is no better than the anaconda solution.
Is there a way to select what version of python you want to use after you start an IPython notebook, preferably with my current MacPorts build?
The idea here is to install multiple ipython kernels. Here are instructions for anaconda. If you are not using anaconda, I recently added instructions using pure virtualenvs.
Anaconda >= 4.1.0
Since version 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. This makes using a new python version as easy as creating new conda environments:
conda create -n py27 python=2.7 ipykernel
conda create -n py36 python=3.6 ipykernel
After a restart of jupyter notebook, the new kernels are available over the graphical interface. Please note that new packages have to be explicitly installed into the new environments. The Managing environments section in conda's docs provides further information.
Manually registering kernels
Users who do not want to use nb_conda_kernels or still use older versions of anaconda can use the following steps to manually register ipython kernels.
configure the python2.7 environment:
conda create -n py27 python=2.7
conda activate py27
conda install notebook ipykernel
ipython kernel install --user
configure the python3.6 environment:
conda create -n py36 python=3.6
conda activate py36
conda install notebook ipykernel
ipython kernel install --user
After that you should be able to choose between python2
and python3 when creating a new notebook in the interface.
Additionally you can pass the --name and --display-name options to ipython kernel install if you want to change the names of your kernels. See ipython kernel install --help for more informations.
If you’re running Jupyter on Python 3, you can set up a Python 2 kernel like this:
python2 -m pip install ipykernel
python2 -m ipykernel install --user
http://ipython.readthedocs.io/en/stable/install/kernel_install.html
These instructions explain how to install a python2 and python3 kernel in separate virtual environments for non-anaconda users. If you are using anaconda, please find my other answer for a solution directly tailored to anaconda.
I assume that you already have jupyter notebook installed.
First make sure that you have a python2 and a python3 interpreter with pip available.
On ubuntu you would install these by:
sudo apt-get install python-dev python3-dev python-pip python3-pip
Next prepare and register the kernel environments
python -m pip install virtualenv --user
# configure python2 kernel
python -m virtualenv -p python2 ~/py2_kernel
source ~/py2_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py2 --user
deactivate
# configure python3 kernel
python -m virtualenv -p python3 ~/py3_kernel
source ~/py3_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py3 --user
deactivate
To make things easier, you may want to add shell aliases for the activation command to your shell config file. Depending on the system and shell you use, this can be e.g. ~/.bashrc, ~/.bash_profile or ~/.zshrc
alias kernel2='source ~/py2_kernel/bin/activate'
alias kernel3='source ~/py3_kernel/bin/activate'
After restarting your shell, you can now install new packages after activating the environment you want to use.
kernel2
python -m pip install <pkg-name>
deactivate
or
kernel3
python -m pip install <pkg-name>
deactivate
With a current version of the Notebook/Jupyter, you can create a Python3 kernel. After starting a new notebook application from the command line with Python 2 you should see an entry „Python 3“ in the dropdown menu „New“. This gives you a notebook that uses Python 3. So you can have two notebooks side-by-side with different Python versions.
The Details
Create this directory: mkdir -p ~/.ipython/kernels/python3
Create this file ~/.ipython/kernels/python3/kernel.json with this content:
{
"display_name": "IPython (Python 3)",
"language": "python",
"argv": [
"python3",
"-c", "from IPython.kernel.zmq.kernelapp import main; main()",
"-f", "{connection_file}"
],
"codemirror_mode": {
"version": 2,
"name": "ipython"
}
}
Restart the notebook server.
Select „Python 3“ from the dropdown menu „New“
Work with a Python 3 Notebook
Select „Python 2“ from the dropdown menu „New“
Work with a Python 2 Notebook
A solution is available that allows me to keep my MacPorts installation by configuring the Ipython kernelspec.
Requirements:
MacPorts is installed in the usual /opt directory
python 2.7 is installed through macports
python 3.4 is installed through macports
Ipython is installed for python 2.7
Ipython is installed for python 3.4
For python 2.x:
$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
$ sudo ./ipython kernelspec install-self
For python 3.x:
$ cd /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin
$ sudo ./ipython kernelspec install-self
Now you can open an Ipython notebook and then choose a python 2.x or a python 3.x notebook.
From my Linux installation I did:
sudo ipython2 kernelspec install-self
And now my python 2 is back on the list.
Reference:
http://ipython.readthedocs.org/en/latest/install/kernel_install.html
UPDATE:
The method above is now deprecated and will be dropped in the future. The new method should be:
sudo ipython2 kernel install
Following are the steps to add the python2 kernel to jupyter notebook::
open a terminal and create a new python 2 environment: conda create -n py27 python=2.7
activate the environment: Linux source activate py27 or windows activate py27
install the kernel in the env: conda install notebook ipykernel
install the kernel for outside the env: ipython kernel install --user
close the env: source deactivate
Although a late answer hope someone finds it useful :p
Use sudo pip3 install jupyter for installing jupyter for python3 and sudo pip install jupyter for installing jupyter notebook for python2. Then, you can call ipython kernel install command to enable both types of notebook to choose from in jupyter notebook.
I looked at this excellent info and then wondered, since
i have python2, python3 and IPython all installed,
i have PyCharm installed,
PyCharm uses IPython for its Python Console,
if PyCharm would use
IPython-py2 when Menu>File>Settings>Project>Project Interpreter == py2 AND
IPython-py3 when Menu>File>Settings>Project>Project Interpreter == py3
ANSWER: Yes!
P.S. i have Python Launcher for Windows installed as well.
Under Windows 7 I had anaconda and anaconda3 installed.
I went into \Users\me\anaconda\Scripts and executed
sudo .\ipython kernelspec install-self
then I went into \Users\me\anaconda3\Scripts and executed
sudo .\ipython kernel install
(I got jupyter kernelspec install-self is DEPRECATED as of 4.0. You probably want 'ipython kernel install' to install the IPython kernelspec.)
After starting jupyter notebook (in anaconda3) I got a neat dropdown menu in the upper right corner under "New" letting me choose between Python 2 odr Python 3 kernels.
If you are running anaconda in virtual environment.
And when you create a new notebook but i's not showing to select the virtual environment kernel.
Then you have to set it into the ipykernel using the following command
$ pip install --user ipykernel
$ python -m ipykernel install --user --name=test2

Categories

Resources