-
Notifications
You must be signed in to change notification settings - Fork 1
New Python project workflow with poetry
Guillaume Gautier edited this page Dec 15, 2022
·
4 revisions
- New Python project workflow with poetry
- structure a python project
- commit often
- declare/run tests
- build documentation
- package project
- ?what is project/(sub)package/module
cd PATH
poetry new my-project --src --name my-package
cd my-project
code .
# treegit init
git status
git add -A
git status
git commit -m "poetry: create new project"
git statuspoetry config virtualenvs.in-project true --local
code poetry.toml
git status
git add poetry.toml
git commit -m "poetry: configure local virtual environment"poetry shell
# exitcode pyproject.toml
poetry install
code poetry.lock
git add ...
git commit -m "poetry: install package in editable mode"poetry run python>>> import my_package
>>> my_package.__version__
>>> exit() # or quit()See also .venv/lib/pythonXXX/site-packages/my_package/RECORD.
code pyproject.toml
poetry add numpyAny trouble? (:
poetry add pytest --group dev
poetry install --only dev
poetry run pytestConfiguration from pyproject.toml requires pytest>=6.0
# pyproject.toml
[tool.poetry.group.dev.dependencies]
pytest = "^6.0"
[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q -vv"
testpaths = ["tests"]poetry update
poetry run pytestcode tests/test_my_package.pycode src/my_package/operations.py # and save the file# src/my_package/operations.py
def addition(x, y):
return x + y
# and save the filecode tests/test_addition.py # and save the filefrom my_package.operations import addition
def test_addition():
assert addition(1, 2) == 3
# and save the filepoetry run pytestIF TESTS PASS
git status
git add ...
git commit -m "tests: new feature addition"poetry add sphinx --group docs
code pyproject.toml# pyproject.toml
[tool.poetry.group.docs.dependencies]
Sphinx = { version = "^4.0.3" }Add an optional section and flag above the previous section
# pyproject.toml
[tool.poetry.group.docs]
optional = true
[tool.poetry.group.docs.dependencies]
Sphinx = { version = "^4.0.3" }# poetry update
poetry install --with docs
# poetry install --only docs
poetry run sphinx-quickstart docspoetry run sphinx-build -b html docs docs/_build/html
# or equivalently
# cd docs
# poetry run make html
open docs/_build/html/index.htmlIn docs/conf.py, uncomment and replace with the following
code docs/conf.pyimport os
import sys
sys.path.insert(0, os.path.abspath("../src"))IF TESTS PASS
poetry run pytestgit status
git add docs
git commit -m "docs: initialize documentation with sphinx"poetry build