Using Poetry to manage PyPI packages seemed to be more convenient than using requirements.txt in the following ways:
--You can manage packages used in development / production environment separately (like npm devdependencies) --You can manage only the packages that the product directly depends on. --The version of the package that depends on the dependent package is managed by poetry.lock, and the version of the dependent package does not go up and get stuck (like npm, package-lock.json, yarn.lock). ) --You can upgrade the version of dependent packages with one command. --virtualenv.in-project If true is set, venv will be used to install the package in the .venv directory (it is convenient because the .venv directory is automatically recognized by VS Code).
I have summarized the commands that I think I will use.
pip install --user poetry
poetry config virtualenvs.in-project true
poetry add -D black
poetry add flask
#Creating a separate environment
python -m venv .venv
#Package installation
poetry install
The package is installed under .venv, so run it in .venv
.venv/bin/python api.py
Or run via Poetry
poetry run python api.py
poetry update
Do not use .venv in the container, use Poetry's environment isolation function (because it is not shared with VSCode and does not need to be a .venv directory in particular)
FROM python:3.8.2
#Poetry installation
RUN pip install poetry==1.0.5
WORKDIR /app
#Installation of dependent packages
COPY poetry.lock pyproject.toml ./
#except dev
RUN poetry install --no-dev
#Application storage
COPY api.py ./
# ...
#Run via Poetry
CMD ["poetry", "run", "python", "api.py"]
Recommended Posts