Jupyter Notebook kernel not upgrading - python

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.

Related

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 run Spyder with Python 3.7 with Anaconda

I have installed Anaconda on a Windows 10 machine which comes with Spyder and Python 3.6 but I wish to upgrade to Python 3.7
To create an Anaconda Environment with Python 3.7 is easy by using:
conda create --name py37 python=3.7
or:
conda create --name py370 python=3.7.0 --channel conda-forge
However starting Spyder in this environment will throw it back to Python 3.6.
I tried specifing the python.exe (for version 3.7) directly in Tools -> Settings of Spyder, however upon restarting the Spyder Kernels can't be launched and will display that they need the packages: ipykernel and cloudpickle .
When trying to conda install them in the environment the following appears:
The following packages will be DOWNGRADED:
python: 3.7.0-hea74fb7_0 --> 3.6.6-hea74fb7_0
Which would downgreade python from 3.7 to 3.6 again.
My final try was to use the command:
conda install python==3.7
which has failed with the output
Solving environment: failed
UnsatisfiableError: The following specifications were found to be in conflict:
- python-dateutil -> python[version='>=2.7,<2.8.0a0']
- python-dateutil -> six
- python==3.7
Use "conda info <package>" to see the dependencies for each package.
The question is not how to upgrade Conda to Python 3.7, but how can I get Spyder to work with Python 3.7 in its own environment
When you run spyder from the CMD/terminal, your operating system tries to find the spyder executable on your system's PATH. In this case, it will default back to the base environment's version of spyder, which runs Python 3.6.
The best way I have found so far is to install spyder to the new environment; activate the environment, and then run spyder (which should fire up the version in the local environment).
conda create --name py37 python=3.7
conda install --name py37 spyder -c conda-forge
conda activate py37
spyder
However, this requires the version of spyder to support python 3.7. Currently that is not available (as of 2 July 2018), but it should not be too long coming.
EDIT:
Spyder for Python 3.7 is available.

Is it true that tensorflow ONLY work on python3.5.2?

Im a windows 10 user and this is my version of python at the moment
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
and when i try to install tensorflow with pip this pop up
tensorflow_gpu-0.12.1-cp35-cp35m-win_amd64.whl is not a supported wheel on this platform.
I ve looking on the internet which suggest that tensorflow now only work on python 3.5.2. Is it true? if not then how do i bypass that error?
TensorFlow only supports Python 3.5 64-bit as of now. Support for Python 3.6 is a work in progress and you can track it here as well as chime in the discussion.
The only alternative to use Python 3.6 with TensorFlow on Windows currently is building TF from source.
If you don't want to uninstall your Anaconda distribution for Python 3.6 and install a previous release you can create a conda environment for Python=3.5 as in:
conda create --name tensorflow python=3.5
activate tensorflow
pip install tensorflow-gpu
Also one more alternative
If you are using anaconda distribution, you can do the following to use python 3.5 on the new environnement "tensorflow":
conda create --name tensorflow python=3.5
activate tensorflow
conda install jupyter
conda install scipy
pip install tensorflow
or
pip install tensorflow-gpu
It is important to add python=3.5 at the end of the first line, because it will install Python 3.5.

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>

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?
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)

Categories

Resources