Until building a Python development environment using pyenv on Ubuntu 20.04

Python environment setup

Python is popular.

Python has become more popular lately, but it's very old and older than Java and PHP. Since it is an old language, it may not be able to incorporate the recent mainstream, or it may be troublesome to set up, so I will try to organize that area.

There are also many packages in Python, but newer versions may not work, or development may require an older version of Python in a hurry. The xxenv type pyenv is convenient in such cases.

However, pyenv builds by dropping the Python code, so it requires some OS libraries. Also, the Python build itself succeeds even without some libraries, but when installing the module with the pip command, an error may occur because it was built without the library. I will narrow down which is the minimum library.

Also, since it is a good idea, I will describe the procedure up to the point of building a development environment that even sets Linter/Formatter using poetry.

Immediately after installing Ubuntu 20.04

It's a magic right after OS installation. Let's restart for the time being.

sudo apt update && sudo apt upgrade
sudo reboot

Library installation required for Python build

The required libraries are as follows.

sudo apt install -y git build-essential libssl-dev libbz2-dev libreadline-dev libsqlite3-dev zlib1g-dev libffi-dev

Required libraries for each package

Depending on the package you pip install, you may need a separate library. These packages can be installed after building Python, so add them as appropriate.

When installing mecab

sudo apt install mecab libmecab-dev

When installing mysqlclient

sudo apt install mysql-client libmysqlclient-dev

pyenv setup

Almost as per the official page. Let's set up to use the pyenv command.

https://github.com/pyenv/pyenv

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bashrc
exec "$SHELL"

python build & install

Try installing Python 3.8.6 using pyenv. Since Python is built from the source code with pyenv install 3.8.6, it will take some time. Finally, don't forget global and rehash. This area is also as per the official page.

pyenv install 3.8.6
pyenv global 3.8.6
pyenv rehash

This completes the Python environment setup.

Development project setup

pip update and poetry installation

Now that Python is installed and in your PATH, you can use the pip command.

The pip command is always in the npm i -g state, like npm. That is, the package will be installed in the global environment. Since it is common sense these days to separate the packages used for each development project, we use poetry.

Besides poetry, there is also a tool called pipenv. Since it has become a tool under the jurisdiction of pypa, this may be the mainstream, but development tends to be delayed. .. The disadvantage is that dependency resolution is slow.

Poetry, on the other hand, uses pyproject.toml as proposed in PEP 518 for package management. It follows the python standard and is actively developed.

I still don't know which one will take the hegemony, but I use poetry.

pip install -U pip
pip install poetry

Create directory for your project

mkdir hellopy && cd hellopy
git init
poetry init

If you do poetry init, it will be an interactive screen, so for the time being, enter it appropriately. The package installation will be done later, so leave it as ** no **.

Image from Gyazo

linter & formatter

Install the Python linter/formatter that you always use.

Also, pre-commit is included to automatically execute these linter/formatter at the time of git commit.

Since these are development tools, they are devDependency.

poetry add -D flake8 mypy black isort pre-commit

pre-commit

This is the pre-commit configuration file Create the following .pre-commit-config.yaml under hellopy and execute the following command.

This completes the development environment.

pre-commit install
pre-commit

yaml .pre-commit-config.yaml


# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.3.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-json
      - id: flake8
      - id: check-yaml
        # - repo: https://github.com/asottile/seed-isort-config
        #   rev: v1.9.3
        #   hooks:
        #     - id: seed-isort-config
  - repo: https://github.com/pre-commit/mirrors-isort
    rev: v4.3.21
    hooks:
      - id: isort
  - repo: https://github.com/psf/black
    rev: 19.10b0
    hooks:
      - id: black
  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: "v0.740" # Use the sha / tag you want to point at
    hooks:
      - id: mypy
        additional_dependencies: [sqlalchemy-stubs]

At the end

I struggled somehow, so I tried to summarize it again, but it was surprisingly simple. The language that I personally find most difficult to set up a development environment is PHP. You can see various hells by using phpenv. I get the impression that the more OS-friendly languages ​​are, the harder it is to build an environment.

It would be nice if Python could be as simple as Node.js on the current boom.

That is all for the explanation. Have a good Python life.

Recommended Posts

Until building a Python development environment using pyenv on Ubuntu 20.04
Building a Python environment on Ubuntu
Build a Python development environment using pyenv on MacOS
[Pyenv] Building a python environment with ubuntu 16.04
Build a Python environment on your Mac using pyenv
Building a Python environment on Mac
Prepare Python development environment on Ubuntu
Introducing Python using pyenv on Ubuntu 20.04
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
Building a Jupyter Lab development environment on WSL2 using Anaconda3
Building a Python environment on a Mac and using Jupyter lab
Building a Python development environment for AI development
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Build python environment with pyenv on EC2 (ubuntu)
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Python development on Ubuntu on AWS EC2 (using JupyterLab)
Build a Python development environment on your Mac
[Python] Web development preparation (building a virtual environment)
[Venv] Create a python virtual environment on Ubuntu
Build a Kubernetes environment for development on Ubuntu
Set up a Python development environment on Marvericks
Building a Python environment on a Sakura VPS server
Create a Python virtual development environment on Windows
Build a Python development environment on Raspberry Pi
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
Building a Python virtual environment
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
Build a Django development environment using pyenv-virtualenv on Mac
How to set up a Python environment using pyenv
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
Notes on creating a python development environment on macOS Catalina
Create a comfortable Python 3 (Anaconda) development environment on windows
I made a Python3 environment on Ubuntu with direnv.
Build a GVim-based Python development environment on Windows 10 (1) Installation
Build a Python development environment on Mac OS X
Procedure for building a CDK environment on Windows (Python)
Memo for building a machine learning environment using Python
Create a Python development environment on OS X Lion
How to build a new python virtual environment on Ubuntu
Build an Ubuntu python development environment on Google Cloud Platform
Preparing python using vscode on ubuntu
Create a Python environment on Mac (2017/4)
Building a development environment for Android apps-creating Android apps in Python
Building a virtual environment with Python 3
Python development environment construction on macOS
How to create a Python 3.6.0 environment by putting pyenv on Amazon Linux and Ubuntu
Build a GVim-based Python development environment on Windows 10 (2) Basic settings
Notes on installing Python using PyEnv
Get a clean Python development environment with pyenv + pipx + Poetry
Using a serial console on Ubuntu 20.04
Install Python development environment on Windows 10
Install Python on CentOS using Pyenv
Build a python3 environment on CentOS7
Install Python on CentOS using pyenv
OpenCV3 & Python3 environment construction on Ubuntu
Example of building python development environment on windows (wsl2, vscode, pipenv)
Build a local development environment for Lambda + Python using Serverless Framework
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
Building a development environment with Maven on Google App Engine [Java]
Everything from building a Python environment to running it on Windows