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.
It's a magic right after OS installation. Let's restart for the time being.
sudo apt update && sudo apt upgrade
sudo reboot
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
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.
sudo apt install mecab libmecab-dev
sudo apt install mysql-client libmysqlclient-dev
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"
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.
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
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 **.
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]
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