I want to be able to run both Python 3.8 (currrent version) and Python 3.7 in my Jupyter Notebook. I understand creating different IPython kernels from virtual environments is the way. So I downloaded Python 3.7 and locally installed it in my home directory. Used this python binary file to create a virtual environment by
> virtualenv -p ~/Python3.7/bin/python3 py37
> source py37/bin/activate
This works perfectly and gives 'Python 3.7' correctly on checking with python --version
and sys.version
.
Then for creating IPython kernel,
(py37) > ipython kernel install --user --name py37 --display-name "Python 3.7"
(py37) > jupyter notebook
This also runs without error and the kernel can be confirmed to be added in the Notebook. However it does not run Python 3.7 like the virtual environment, but Python 3.8 like the default kernel. (confirmed with sys.version
)
I checked ~/.local/share/jupyter/kernels/py37/kernel.json
and saw its contents as
{
"argv": [
"/usr/bin/python3",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3.7",
"language": "python"
So naturally I tried editing the /usr/bin/python3
to point to my Python 3.7 binary file path that is ~/Python3.7/bin/python3
, but then even the kernel doesn't work properly in the notebook.
What can I possibly do?
NB: I use Arch Linux, so I installed jupyter, virtualenv, ... through pacman not pip as its recommended in Arch.
The command conda create -c conda-forge python=3.9 -n Python3_9 installs Python version 3.9 in a new virtual environment Python3_9. Let's activate Python3_9 environment by 'conda activate Python3_9' command. Type 'Jupyter Notebook' to start the notebook.
If you need to run a different, older, version of Python in Jupyter Lab, you can do write the code that you see in the next image. On my computer, for example, I have the version 3.6 (as default version) and also the new 3.7 version.
If you are using Python 3.8. 0 and have installed the pyxll-jupyter package using "pip install pyxll-jupyter", but are not seeing the Jupyter Notebook icon in Excel then the problem may be your version of Python. The first thing to check is your PyXLL log file.
Found it myself, the hard way. Let me share anyway, in case this helps anyone.
I guess, the problem was that, jupyter notebook installed through pacman searches for python binary files in the PATH variable and not in the path specified by the virtual environment. Since I installed Python 3.7 locally in my home directory, Jupyter can't find it and it might have defaulted to the default python version.
So the possible solutions are:
> wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz > tar -xvf Python-3.7.4.tgz > cd Python-3.5.1/ > ./configure --prefix=$HOME/Python37 > make > make install > virtualenv -p ~/Python3.7/bin/python3 py37 > source py37/bin/activate (py37) > pip install notebook (py37) > python -m notebook
> python
points to the updated global Python 3.8 version and > python3
or > python3.7
points to newly installed Python 3.7> wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz > tar -xvf Python-3.7.4.tgz > cd Python-3.5.1/ > ./configure > make > sudo make install > virtualenv -p $(which python3.7) py37 > source py37/bin/activate (py37) > ipython kernel install --user --name py37 --display-name "Python 3.7" (py37) > jupyter notebook
> wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz > tar -xvf Python-3.7.4.tgz > cd Python-3.5.1/ > ./configure --prefix=$HOME/Python37 > make > make install > export PATH="$HOME/Python37/bin:$PATH" > virtualenv -p py37 > source py37/bin/activate (py37) > ipython kernel install --user --name py37 --display-name "Python 3.7" (py37) > jupyter notebook
Another approach is to just run the notebook application directly using the version of Python you require - provided it's installed for that version of Python (e.g. on a Mac with brew installed version of Python3.8):
/usr/local/opt/[email protected]/bin/python3 -m notebook
Also if you want to install packages for that version:
/usr/local/opt/[email protected]/bin/pip3 install that_package
in the official jupyter dcoumentation, there is a description now to do it, when customizing jupyter stacks. I think the mamba command will solve the problem. The commands starting with RUN can also just executed on a linux system.
# Choose your desired base image
FROM jupyter/minimal-notebook:latest
# name your environment and choose the python version
ARG conda_env=python37
ARG py_ver=3.7
# you can add additional libraries you want mamba to install by listing them below the first line and ending with "&& \"
RUN mamba create --quiet --yes -p "${CONDA_DIR}/envs/${conda_env}" python=${py_ver} ipython ipykernel && \
mamba clean --all -f -y
# alternatively, you can comment out the lines above and uncomment those below
# if you'd prefer to use a YAML file present in the docker build context
# COPY --chown=${NB_UID}:${NB_GID} environment.yml "/home/${NB_USER}/tmp/"
# RUN cd "/home/${NB_USER}/tmp/" && \
# mamba env create -p "${CONDA_DIR}/envs/${conda_env}" -f environment.yml && \
# mamba clean --all -f -y
# create Python kernel and link it to jupyter
RUN "${CONDA_DIR}/envs/${conda_env}/bin/python" -m ipykernel install --user --name="${conda_env}" && \
fix-permissions "${CONDA_DIR}" && \
fix-permissions "/home/${NB_USER}"
# any additional pip installs can be added by uncommenting the following line
# RUN "${CONDA_DIR}/envs/${conda_env}/bin/pip" install --quiet --no-cache-dir
# if you want this environment to be the default one, uncomment the following line:
# RUN echo "conda activate ${conda_env}" >> "${HOME}/.bashrc"
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With