Smooth Python development environment for teams [Poetry + pyenv + black + isort]

Smooth Python development environment for teams [Poetry + pyenv + black + isort]

If the development environment can be unified as a team, collaboration will be much easier. In this article, the virtual environment is unified by poetry + pyenv, and [black](https: //) github.com/psf/black) + isort sets the automatic formatting of the code.

Editor

Please use the one you like. With vim, pycharm or VScode. Here, the settings are described on the premise of VS code.

How to install vscode is below

--Mac: Procedure to install Visual Studio Code on MacOS --ubuntu: Easy way to install VS Code (Visual Studio Code) on Ubuntu

Virtual environment

By using a virtual environment, the execution environment can be aligned when writing code as a team, and the efficiency of collaborative work is greatly improved. Here, the following virtual environment tools are used.

The installation method is [this article](https://qiita.com/MasashiSD/items/a22a6f352b37e2316367#%E4%BB%AE%E6%83%B3%E7%92%B0%E5%A2%83%E3%81 Please refer to% AE% E3% 82% A4% E3% 83% B3% E3% 82% B9% E3% 83% 88% E3% 83% BC% E3% 83% AB).

Install Python to use using pyenv.

You can list the Python versions that can be used with the following command.

pyenv install --list

Let's introduce Python 3.7 this time.

pyenv install 3.7

For details on how to use pyenv, refer to This article.

Creating a new project with poetry

To create a new project, execute the following. Execute poetry config virtualenvs.in-project true at the same time to put the virtual environment directly under the project directory. (Necessary for VS Code to work properly)

poetry config virtualenvs.in-project true
poetry new new_project

Settings for pyproject.toml

Then, a file called pyproject.toml will be generated in the project. This file will be the configuration file and will be edited in the future. Let's specify the version of Python installed in the previous section. You can specify that Python 3.7 series is used by writing as follows.

[tool.poetry.dependencies]
python = "^3.7"

Building a virtual environment

After that, you can build and install a Python virtual environment with the following command.

poetry install

Library installation

If you want to install a new library in this virtual environment, specify ʻaddoption as follows. Here, let's installblack, ʻisort, flake8, mypy for development ( -D option).

poetry add flake8 -D
poetry add black -D
poetry add isort -D
poetry add mypy -D

Running in a virtual environment

If you want to execute it in a virtual environment, if you put the program as your_program.py, you can execute it as follows.

poetry run python your_program.py

Running a shell in a virtual environment

If you want to run the shell in the virtual environment, type the following to start the shell.

poetry shell

Introducing poetry into existing projects

If the project your_project already exists, do the following: Execute poetry config virtualenvs.in-project true to put the virtual environment directly under the project directory. (Necessary for VS Code to work properly)

cd your_project
poetry config virtualenvs.in-project true
poetry init
poetry install

If you do not want to create a directory such as test when doing init, specify the following --no-interaction option.

poetry init --no-interaction

Detailed usage of poetry

For detailed usage, see "Official document" or "This article: Poetry: Helps with Python dependency management and packaging. Tools to do "," [This article: From developing Python packages using Poetry to publishing PyPI](https://kk6.hateblo. jp / entry / 2018/12/20/124151) ”.

Use Python of virtual environment created by poetry with VS Code as interpreter

Open the created project with VS Code. Create or select an appropriate Python file, press select interpreter at the bottom left, and select Python in .venv.

Introduction of Python formatter

When writing code as a team, if the writing style is unified, the code will be easier to read and coding will proceed smoothly. Here, black is used to automatically format the code, and issort is used to align the order of the import statements so that the writing style is automatically unified. I will describe how to install Formatter on the premise that the above poetry is installed. (For VS Code)

black

--Set VS Code. Since the setting screen can be displayed with ctrl +,, set as follows. The following settings will format the code each time you save.

Setting name function Set value
python.linting.enabled Whether to enable the Lint function true
python.linting.pylintEnabled Whether to use pylint for Lint false
python.linting.flake8Enabled Whether to use flake8 for Linter true
python.linting.lintOnSave Whether to run Lint when saving a file true
python.formatting.provider What to use for formatting Python code black
editor.formatOnSave Whether to automatically format when saving a file true

--Reference: Black clean and automatic shaping! Introduce and run flake8 and Black / VS Code settings

isort

--Set vscode so that the order of import statements is automatically aligned when saving. --Enter the command screen by typing ctrl + shift + p on VSCode. --Type Preferences: Open Settings (JSON) to display setting.json. --Add the following.

```json
{
  "[python]":{
    "editor.codeActionsOnSave":{
       "source.organizeImports":true
    }
}
```

Reference link

-From Python package development using Poetry to PyPI release

Recommended Posts

Smooth Python development environment for teams [Poetry + pyenv + black + isort]
[For organizing] Python development environment
Python development environment options for May 2020
Python environment construction (pyenv + poetry + pipx)
Emacs settings for Python development environment
Get a clean Python development environment with pyenv + pipx + Poetry
Python development environment for macOS using venv 2016
Building a Python development environment for AI development
Windows + gVim + Poetry python development environment construction
Building an Anaconda environment for Python with pyenv
python development environment -use of pyenv and virtualenv-
Get a quick Python development environment with Poetry
Python development environment construction
About Python development environment
python2.7 development environment construction
Development environment in Python
Python environment for projects
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
Use Python installed with pyenv for PL / Python execution environment
Python development environment construction 2020 [From Python installation to poetry introduction]
Manage Python runtime packages and development environment packages with Poetry
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
Build a Python development environment using pyenv on MacOS
python memo (for myself): About the development environment virtualenv
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
Development environment suitable for ArcPy
Building a development environment for Android apps-creating Android apps in Python
Python environment construction For Mac
Organize your Python development environment
[ev3dev × Python] Build ev3dev development environment
[MEMO] [Development environment construction] Python
Until building a Python development environment using pyenv on Ubuntu 20.04
Build a local development environment for Lambda + Python using Serverless Framework
Construction of Python local development environment Part 1 (pyenv, pyenv-virtualenv, pip installation)
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Prepare Python development environment on Ubuntu
Prepare your first Python development environment
Python development environment construction on macOS
Vim + Python development environment setting memo
Install Python development environment on Windows 10
Python3 TensorFlow for Mac environment construction
Emacs Python development environment construction memo
Checking the NAOqi Python development environment
Prepare Python development environment with Atom
Prepare Python development environment for each project in Windows environment (VSCode + virtualEnvWrapper + Pylint)
After buying a new Mac, use pyenv + poetry to build a Python environment.
Create a Python development environment locally at the fastest speed (for beginners)
[Pyenv] Building a python environment with ubuntu 16.04
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.4
Build an environment for Blender built-in Python
[Development environment] Python with Xcode [With screen transition]
Use smbus with python3 under pyenv environment
Python project environment construction procedure (for windows)
Blender 2.82 or later + python development environment notes
Python environment tool comparison chart for Rubyist
Let's create a virtual environment for Python
Creating amateur python environment settings (for MAC)
How to prepare Python development environment [Mac]
Python3 + venv + VSCode + macOS development environment construction
[Mac] Building a virtual environment for Python