243

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?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Victor
  • 2,521
  • 2
  • 11
  • 8

8 Answers8

378
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
Community
  • 1
  • 1
Davies Odu
  • 3,939
  • 1
  • 9
  • 4
  • 4
    this should be selected as the answer to the posted question. – Dark Templar Feb 11 '20 at 09:09
  • 13
    The OP is asking to find which executable is running (system/anaconda), not which version. The answer by P. Camilleri is the solution – MrMartin Mar 10 '20 at 08:56
  • 1
    Answer fails to answer the question. Answer fails to succinctly print the version (and related metadata) of the active Python interpreter with a terse one-liner like `from sys import version; version`. Naturally, 240 upvotes ensue. Makes sense. As @MrMartin suggests, **see literally any other answer than this.** – Cecil Curry Jun 11 '21 at 06:04
  • this works: from platform import python_version print(python_version()) – jayanti prasad Feb 22 '23 at 11:49
  • This tells you the version, but how does it tell you the *path* of the python executable that's currently running? That seems to be more what OP is about (and what I was looking for when searching led me to this page). – sh37211 Mar 18 '23 at 19:20
107
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

P. Camilleri
  • 12,664
  • 7
  • 41
  • 76
  • I'm running this in Spyder and no results but Jupyter is ok. I wonder why? – TokyoToo May 31 '20 at 00:56
  • 2
    The Conda docs recommend against adding to the PATH like that. – AMC Jun 11 '20 at 00:24
  • @AMC you're right, thank you. I have edited to point towards `conda init` – P. Camilleri Jun 11 '20 at 08:43
  • Running `sys.executable` returns `'C:\\Program Files\\Anaconda3\\python.exe'` - not very useful. We already know that it's Python (from the heading of the OP) and what we want to know is the version of Python being used. Thank you. – Confounded Jul 22 '21 at 14:19
  • 2
    @Confounded you may have different intepreters on your machine with different versions. The OP is asking which interpreter is called, not which version. – P. Camilleri Jul 22 '21 at 14:56
88
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.

Cesare
  • 9,139
  • 16
  • 78
  • 130
Rohit Dhankar
  • 1,574
  • 18
  • 25
20

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

Suhas_Pote
  • 3,620
  • 1
  • 23
  • 38
  • This returns the main interpreter in the environment, however, if Jupyter is using a different one the wrong version is returned. E.g. If the Jupyter kernel is using 3.10 but 3.11 is also installed and callable through the "python" command (as main), 3.11 will be returned. – chjortlund Jun 02 '23 at 07:50
8

Looking the Python version

Jupyter menu help/about will show the Python version

petezurich
  • 9,280
  • 9
  • 43
  • 57
denys
  • 81
  • 1
  • 2
4

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.

AChampion
  • 29,683
  • 4
  • 59
  • 75
  • To add more kernelspecs, see also the [IPython kernel install docs](http://ipython.readthedocs.io/en/stable/install/kernel_install.html) – Thomas K Nov 19 '16 at 18:47
2

Check the Python Version

import sys
print(sys.version)
Marioanzas
  • 1,663
  • 2
  • 10
  • 33
-1

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
Daisuke Aramaki
  • 316
  • 3
  • 7