Get a quick Python development environment with Poetry

background

Poetry reached 1.0.0 at the end of last year on 12/12/2019, so I tried it.

Overview

Poetry is a packaging and dependency management tool for Python.

As a similar tool There are setuptools and Pipenv, but Poetry can cover both functions with this one.

** I'm attracted to the fact that "one toml file is all you need" **. Or rather, I thought it was ** wonderful **.

version

poetry v1.0.5 (latest as of May 11, 2020)

Creating a project

https://python-poetry.org/docs/basic-usage/ https://python-poetry.org/docs/cli/#new

$ poetry new <project name>

After execution, the following folders will be created.

<project name>
├── pyproject.toml
├── README.rst
├── poetry_demo
│   └── __init__.py
└── tests
    ├── __init__.py
    └── test_poetry_demo.py

The pyproject.toml contained in the created folder is the poetry configuration file. It is a file that replaces setup.py / setup.cfg / Pipfile, and the contents written in each file can be described in pyproject.toml. If you look it up, it will be replaced, so if you are interested, please see the link on the official page. https://python-poetry.org/docs/pyproject/

pyproject.tml


[tool.poetry]
name = "<project name>"
version = "0.1.0"
description = ""
authors = ["Sébastien Eustace <[email protected]>"]

[tool.poetry.dependencies]
python = "*"

[tool.poetry.dev-dependencies]
pytest = "^3.4"

Add library

https://python-poetry.org/docs/cli/#add

#Add dependent library
$ poetry add requests
#Addition of dependent libraries for development
$ poetry add --dev requests

Of course you can also specify the version. It doesn't seem to be much different from requirements.txt / setup.py / Pipfile. https://python-poetry.org/docs/dependency-specification/

Create virtual environment, install in development mode

https://python-poetry.org/docs/cli/#install

$ poetry install

Command execution in a virtual environment

https://python-poetry.org/docs/cli/#run https://python-poetry.org/docs/cli/#shell

#Run in a single shot
$ poetry run python -m pip list
#Run in shell
$ poetry shell

It seems that you can do something like npm-scripts by specifying the Python module you want to execute in pyproject.toml.

pyproject.tml


[tool.poetry.scripts]
my-script = "my_module:main"
$ poetry run my-script

Packaging

https://python-poetry.org/docs/cli/#build

$ poetry build

By default, both sdist / wheel are created. You can use either one with the --format option.

Output to requirements.txt

https://python-poetry.org/docs/cli/#export

$ poetry export -f requirements.txt > requirements.txt

It seems that v1.0.2 only supports the format of requirements.txt.

Cooperation with PyCharm

PyCharm (2019.03) recognized pyproject.toml and detected the virtual environment.

Linkage with Visual Studio Code

Visual Studio Code doesn't recognize it automatically.

It's a hassle, but either of the following is required.

  1. Specify the path where the virtual environment is saved in settings.json
  2. Create a virtual environment for poetry in the .venv folder that automatically recognizes it (* I personally prefer this)

https://github.com/microsoft/vscode-python/issues/8372#issuecomment-549924849

When setting to settings.json

This is an example on Mac.

settings.json


{
    "python.venvPath": "~/Library/Caches/pypoetry/virtualenvs"
}

When creating a virtual environment in the .venv folder

#Delete the Python environment that was created once
$ poetry env list
$ poetry env remove <Environment name>
# .Change settings to save in venv--> poetry.Output to toml.
$ poetry config virutalenv.local true --local
#Create virtual environment--> .Output to the venv folder.
$ poetry install

After that, if you reload the Python Interpreter settings of Visual Studio Code and look at it, you can select the .venv folder.

Recommended Posts

Get a quick Python development environment with Poetry
[Python] Build a Django development environment with Docker
Build a machine learning application development environment with Python
Manage Python runtime packages and development environment packages with Poetry
Create a virtual environment with Python!
Building a virtual environment with Python 3
Unify the environment of the Python development team starting with Poetry
Set up a Python development environment with Visual Studio Code
How to build a python2.7 series development environment with Vagrant
Create a simple Python development environment with VSCode & Docker Desktop
How to get into the python development environment with Vagrant
[Pyenv] Building a python environment with ubuntu 16.04
Building a Python3 environment with Amazon Linux2
Easily build a development environment with Laragon
[Development environment] Python with Xcode [With screen transition]
[Python] Create a virtual environment with Anaconda
Building a Python 3.6 environment with Windows + PowerShell
[Python] A quick web application with Bottle!
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
Building a Python development environment for AI development
Let's get along with Python # 0 (Environment construction)
Windows + gVim + Poetry python development environment construction
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Create a simple Python development environment with VS Code and Docker
[Python] Get environment variables
Build a C language development environment with a container
[Python] Get the files in a folder with Python
Get a ticket for a theme park with python
Build Python development environment with Visual Studio Code
Build a python environment with ansible on centos6
Get a list of packages installed in your current environment with python
Building a Python environment with WLS2 + Anaconda + PyCharm
Create a virtual environment with conda in Python
Python development environment construction
Create a python3 build environment with Sublime Text3
Build a Python development environment on your Mac
About Python development environment
[Python] Web development preparation (building a virtual environment)
A memo when creating a python environment with miniconda
Build a Django development environment with Doker Toolbox
Python environment with docker-compose
Get started with Python in 30 minutes! Development environment construction & learn basic grammar
Commands for creating a python3 environment with virtualenv
Work in a virtual environment with Python virtualenv.
python2.7 development environment construction
Get date with python
Set up a Python development environment on Marvericks
Build a Python environment with OSX El capitan
Create a Python environment
Development environment in Python
Quickly build a Python Django environment with IntelliJ
I tried to build a Mac Python development environment with pythonz + direnv
Virtual environment with Python 3.6
Build a Python machine learning environment with a container
Build a Python development environment on Raspberry Pi
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Virtualize (isolate) IBM i python development environment with chroot
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
Recommendation of building a portable Python environment with conda
Get financial data with python (then a little tinkering)