Welcome to new things

[Technical] [Electronic work] [Gadget] [Game] memo writing

Build Python3+Jupyter Notebook environment with pyenv on WSL

Since the version of Ubuntu in WSL (Windows Subsystem for Linux) was getting old at 16, we installed a new Ubuntu with version 18.

I also re-set up Python3 + Jupyter Notebook accordingly, but I ran into a few snags along the way, so here are some notes on building a Python environment for future re-setups.

Basics about the Python environment

  • Python has two systems, version 3 and 2.
  • Versions 3 and 2 have different commands, so it is possible to have both on one PC.

    • (I was under the mistaken impression that the two could not coexist, and that I had to use a Python version-switching tool to make them coexist, which got me into trouble.)
  • Pip is a so-called module management tool, and Python modules are installed using pip.
  • The Python module installation can be done using the
    For Python3, python3 -m pip install <module name.
    For Python2, python -m pip install <module name.
    The following is a list of the most common types of products that are used in the market.

Python version differences

The main differences between Python versions 3 and 2 are as follows

(data) item Version 3 Version 2
Execution command python3 python
How to install pip module python3 -m pip install \<module name python -m pip install \<module name
Ubuntu python package name python3 python
Ubuntu pip package name python3-pip python-pip

Jupyter Notebook

install

Jupyter Notebook is installed using pip.

python3 -m pip install jupyter

execution

Execution is done directly with the "jupyter" command.

jupyter notebook

Special note on Ubuntu

  • Ubuntu18 only includes Python3 by default.
  • Ubuntu18 does not include pip by default.

So, to install Jupyter Notebook, first install Python3 pip below, and then install Jupyter Notebook.

sudo apt-get install python3-pip
python3 -m pip install jupyter

attention (heed)

After installing Jupyter Notebook, jupyter will be placed in ~/.local and the jupyter command will invoke jupyter. (The configuration is done in ~/.profile)

Even if you use the Python version-switching tool described below, if jupyter refers to the one in ~/.local, the version of Python installed on the system will be used. 009" should not point to ~/.local. (I didn't realize this and got stuck.)

pyenv

You can install the Python3+Jupyter Notebook environment above, but the Python version will be the Python3 version installed with the Ubuntu package.

Since the Python version of the Ubuntu package is a little older than the latest, we will install "pyenv", a Python version control tool, so that Jupyter Notebook can be used with any Python version.

What is pyenv?

  • pyenv is a tool that allows you to switch Python versions at will.
  • The way it works is that by passing the pyenv path before $PATH, it intercepts the system Python being referenced and calls any version of Python.
  • You can set the user default Python version to anything you want, and you can set the Python version for each folder to anything you want.

Installing pyenv

1. download necessary files to ".pyenv" in your home directory

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

2. add the following to ".bashrc" or ".bash_profile

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
    eval "$(pyenv init -)"
fi

Shell Restart

If you run echo $PATH, you will see that "\~/.pyenv/shims" is added to the beginning of $PATH.

This will hide Python from the system, so that when you try to start Python, Python managed by pyenv and placed under "\~/.pyenv/shims" will be called.

How to use pyenv

Special note on Ubuntu

When installing Python with pyenv, the following packages must be installed in advance.

sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

This information is in pyenv wiki (I didn't notice it and got hooked), so please refer to it if you get an error about missing packages during Python installation (the Wiki also describes this for non-Ubuntu OS). (The Wiki also describes this for OS other than Ubuntu.)


Install the version of Python you want to use with pyenv, and then assign the installed Python to each user default or directory you specify.

List of Python versions that can be installed

pyenv install —list

Install the specified version of Python

pyenv install <バージョン名>

At this time, pip is also installed.

List of installed versions

pyenv versions

User default Python version to the specified version

pyenv global <バージョン名>

Set the Python version under the current directory to the specified version.

pyenv local <バージョン名>

Example of use

Example: Jupyter Notebook with Python version 3.7.2

pyenv install 3.7.2
pyenv local 3.7.2
python3 -m pip install jupyter
jupyter notebook

Example: Install Python versions 2.7.15 and 3.7.2 and assign version 2.7.15 to the command "python" by user default

You may want to be able to use both Python version 3 and 2.

In such cases, use pyenv global <version 2> <version 3 or pyenv local <version 2> <version 3 and list the two versions side by side.

Then both versions will be available, and the "python" command will be assigned to the first version specified of the two lined up.

The second version is 3, so it is assigned to the "python3" command.

$ pyenv install 2.7.15
$ pyenv install 3.7.2
$ pyenv global 2.7.15 3.7.2
$ python -V
Python 2.7.15
$ python3 -V
Python 3.7.2

If you are in need of help, please contact

If you can find out which/which version of Python/Juypter is being called, you may be able to find the cause of the problem.

  • To check the Python version
    For Python3, python3 -V.
    For Python2, python -V.
    The following is a list of the most common types of products that are used in the market.
  • To find out the Python version path from your program, you can do the following
    It is especially useful to know the version of Python being run from Jupyter Notebook.
import sys
print(sys.version_info)
print(sys.path)
  • The Python version assigned to the command "python" may be 3 depending on the environment.
    Therefore, it is better to check the version assigned to "python" in python -V.
  • Use which <command name>. to see which path the command is assigned to.
    For example, to find the path to "python", type which python.
    For example, to know the path to "jupyter", type which jupyter.
    Especially when using pyenv, it is useful to know if the Python/Jupyter is from the system or from pyenv.

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com