How to know which Python is running in Jupyter notebook?
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?
I had this problem, and for the second part of your problem I solved it by reinstalling jupyter using conda: conda install jupyter .
8 Answers 8
from platform import python_version print(python_version())
This will give you the exact version of python running your script. eg output:
The OP is asking to find which executable is running (system/anaconda), not which version. The answer by P. Camilleri is the solution
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.
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).
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 /bin/activate and then run conda init
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 you may have different intepreters on your machine with different versions. The OP is asking which interpreter is called, not which version.
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 —
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.
How can I display the version of my Jupyter notebook and run cells in Jupyter notebooks? I get an error: bad interpreter
I have installed Anaconda and I use the conda environments and the conda command to install software. When I type: jupyter notebook —version I get the following error: zsh: /Users/cr517/.local/bin/jupyter: bad interpreter: /Users/cr517/anaconda/envs/snakes/bin/python: no such file or directory 5.2.1 I can open a jupyter notebook by typing: jupyter notebook xyz.ipynb but I cannot run any cells. The cell is marked with * after I run it. Soon, this message appears in a pop-up box: The kernel has died, and the automatic restart has failed. It is possible the kernel cannot be restarted. If you are not able to restart the kernel, you will still be able to save the notebook, but running code will no longer work until the notebook is reopened. I get this in the terminal after opening a jupyter notebok:
zsh: /Users/cr517/.local/bin/jupyter: bad interpreter: /Users/cr517/anaconda/envs/snakes/bin/python: no such file or directory [I 21:01:55.558 NotebookApp] JupyterLab alpha preview extension loaded from /Users/cr517/anaconda/lib/python3.6/site-packages/jupyterlab [I 21:01:55.558 NotebookApp] JupyterLab application directory is /Users/cr517/anaconda/share/jupyter/lab [I 21:01:55.564 NotebookApp] Serving notebooks from local directory: /Users/cr517/Documents/teach/demonstrations/python/python-functions-and-modules-master [I 21:01:55.564 NotebookApp] 0 active kernels [I 21:01:55.564 NotebookApp] The Jupyter Notebook is running at: [I 21:01:55.564 NotebookApp] http://localhost:8888/?token=0a0b3368be051aceea02ddfdb677ba5b48724f050e78fa59 [I 21:01:55.564 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 21:01:55.584 NotebookApp] Copy/paste this URL into your browser when you connect for the first time, to login with a token: http://localhost:8888/?token=0a0b3368be051aceea02ddfdb677ba5b48724f050e78fa59 [I 21:01:55.852 NotebookApp] Accepting one-time-token-authenticated connection from ::1 [I 21:01:57.808 NotebookApp] Kernel started: 944ec290-db3a-4dc1-97fb-cf440cc384ce /Users/cr517/anaconda/bin/python: No module named ipykernel_launcher [I 21:02:00.804 NotebookApp] KernelRestarter: restarting kernel (1/5) /Users/cr517/anaconda/bin/python: No module named ipykernel_launcher [I 21:02:03.813 NotebookApp] KernelRestarter: restarting kernel (2/5) /Users/cr517/anaconda/bin/python: No module named ipykernel_launcher [I 21:02:06.824 NotebookApp] KernelRestarter: restarting kernel (3/5) /Users/cr517/anaconda/bin/python: No module named ipykernel_launcher [W 21:02:07.940 NotebookApp] Timeout waiting for kernel_info reply from 944ec290-db3a-4dc1-97fb-cf440cc384ce [I 21:02:09.833 NotebookApp] KernelRestarter: restarting kernel (4/5) kernel 944ec290-db3a-4dc1-97fb-cf440cc384ce restarted /Users/cr517/anaconda/bin/python: No module named ipykernel_launcher [W 21:02:12.841 NotebookApp] KernelRestarter: restart failed [W 21:02:12.842 NotebookApp] Kernel 944ec290-db3a-4dc1-97fb-cf440cc384ce died, removing from map. kernel 944ec290-db3a-4dc1-97fb-cf440cc384ce restarted failed! [W 21:02:12.873 NotebookApp] 410 DELETE /api/sessions/eaa457a0-7628-42c4-927f-1428b4ec3b68 (::1): Kernel deleted before session [W 21:02:12.873 NotebookApp] Kernel deleted before session [W 21:02:12.873 NotebookApp] 410 DELETE /api/sessions/eaa457a0-7628-42c4-927f-1428b4ec3b68 (::1) 2.10ms referer=http://localhost:8888/notebooks/python_fm_1.ipynb
Indeed there is no /Users/cr517/anaconda/envs/snakes/bin/python but why is Jupyter Notebook trying to use this? I have a well-functioning Python here:
which python /Users/cr517/anaconda/bin/python
which -a python /Users/cr517/anaconda/bin/python /usr/local/bin/python /usr/bin/python which -a jupyter /Users/cr517/.local/bin/jupyter /Users/cr517/.local/bin/jupyter /Users/cr517/.local/bin/jupyter echo $PATH /usr/local/opt/hdf5@1.8/bin:/Users/cr517/Documents/phd/project/output/software/DEXTRACTOR:/Users/cr517/Documents/phd/project/output/software/DAZZ_DB:/Users/cr517/Documents/phd/project/output/software/DASCRUBBER:/Users/cr517/Documents/phd/project/output/software/subread-1.5.2-MaxOSX-x86_64/bin:/Users/cr517/Documents/phd/project/output/software/SHRiMP_2_2_2/utils:/Users/cr517/Documents/phd/project/output/software:/Users/cr517/Documents/phd/project/output/software/fastx/bin:/Users/cr517/.local/bin:/Users/cr517/Documents/phd/project/output/software/DAZZ_DB:/Users/cr517/Documents/phd/project/output/software/DASCRUBBER:/Users/cr517/Documents/phd/project/output/software/subread-1.5.2-MaxOSX-x86_64/bin:/Users/cr517/Documents/phd/project/output/software/SHRiMP_2_2_2/utils:/Users/cr517/Documents/phd/project/output/software:/Users/cr517/Documents/phd/project/output/software/fastx/bin:/Users/cr517/.local/bin:/Users/cr517/Documents/phd/project/output/software/DASCRUBBER:/Users/cr517/Documents/phd/project/output/software/subread-1.5.2-MaxOSX-x86_64/bin:/Users/cr517/Documents/phd/project/output/software/SHRiMP_2_2_2/utils:/Users/cr517/Documents/phd/project/output/software:/Users/cr517/Documents/phd/project/output/software/fastx/bin:/Users/cr517/.local/bin:/Users/cr517/Documents/phd/project/output/software/subread-1.5.2-MaxOSX-x86_64/bin:/Users/cr517/Documents/phd/project/output/software/SHRiMP_2_2_2/utils:/Users/cr517/Documents/phd/project/output/software:/Users/cr517/Documents/phd/project/output/software/fastx/bin:/Users/cr517/.local/bin:/Users/cr517/Documents/phd/project/output/software:/Users/cr517/Documents/phd/project/output/software/STAR:/Users/cr517/Documents/phd/project/output/software/fastx/bin:/Users/cr517/.local/bin:/Users/cr517/.local/bin:/Users/cr517/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/ncbi/blast/bin:/Library/TeX/texbin:/Users/cr517/Documents/phd/project/multibreak-sv/lib:/Users/cr517/Documents/phd/project/output/software/PBSuite_15.8.24/pbsuite:/Users/cr517/Documents/phd/project/output/software/PBSuite_15.8.24/bin/:/Users/cr517/Documents/phd/project/output/software/blobology /Users/cr517/.local/bin/jupyter /Users/cr517/.local/bin/jupyter /Users/cr517/.local/bin/jupyter /Users/cr517/anaconda/bin/jupyter /usr/local/bin/jupyter jupyter kernelspec list zsh: /Users/cr517/.local/bin/jupyter: bad interpreter: /Users/cr517/anaconda/envs/snakes/bin/python: no such file or directory Available kernels: python3 /Users/cr517/anaconda/lib/python3.6/site-packages/ipykernel/resources ir /Users/cr517/anaconda/share/jupyter/kernels/ir python2 /usr/local/share/jupyter/kernels/python2
jupyter notebook opens my directory in a web browser but does not create a new notebook. I typed python —version in the terminal and got:
Python 3.6.3 :: Anaconda custom (x86_64)