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
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).
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.
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
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"
After that, you can build and install a Python virtual environment with the following command.
poetry install
If you want to install a new library in this virtual environment, specify ʻaddoption as follows. Here, let's install
black, ʻisort
, flake8
, mypy
for development ( -D
option).
poetry add flake8 -D
poetry add black -D
poetry add isort -D
poetry add mypy -D
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
If you want to run the shell in the virtual environment, type the following to start the shell.
poetry shell
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
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) ”.
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
.
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
install
poetry add black -D
poetry add flake8 -D
--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
install
poetry add isort -D
--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
}
}
```
-From Python package development using Poetry to PyPI release
Recommended Posts