Create new Poetry project

Create a new Python Poetry project.


Create project

Ensure pyenv is installed on the desired Python version.

pyenv local 3.13.2

Create a project.

poetry new --src ${new_project}

Configure the project to use local virtual environments.

poetry config virtualenvs.in-project true --local

Your poetry.toml file will look like this:

[virtualenvs]
in-project = true

Create local .python-version

Important

Create a .python-version file within the project to avoid Python version problems.

pyenv local 3.13.2

Initialize Poetry.

poetry install

Check the Python environment to ensure it matches your local Python version.

poetry env info
Mismatched Python version

If poetry env info lists another Python version, tell it to use another with poetry env use, e.g., poetry env use 3.12.2

Add dependencies

Add these helpful dependencies.

poetry add python-dotenv python-decouple
poetry add --group dev black pre-commit pylint perflint ruff bandit pytest detect-secrets 

Configure perflint

Configure pylint to use perflint for performance linting.

Here is a helpful Makefile to get started.

CODE = packages
REQUIREMENTS = requirements.txt

# Export the requirements with no environment markers.
$(REQUIREMENTS):
	@poetry export --without-hashes -f $(REQUIREMENTS) --output $(REQUIREMENTS)
	@sed -i '' 's/;.*//' $(REQUIREMENTS)

# Lint code with pylint and perflint, for performance.
lint:
	@poetry run pylint $(CODE) --load-plugins perflint

# Lint code with ruff to check for unused imports, etc.
ruff:
	@poetry run ruff check $(CODE)


.PHONY: lint ruff

Other configurations

  • Configure VS Code to automatically format files, trim whitespace, and more.
  • Configure gitignore to ignore common development artifacts.

Optional: Debug calling Poetry

Install Poetry as a dev dependency

If Poetry is a module installed in the virtual environment, VS Code will be able to call it from launch.json.

poetry add --group debug poetry
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "poetry install", // Poetry command
      "type": "python",
      "request": "launch",
      "module": "poetry",
      "args": [
        "-vvv",
        "install"
      ]
    },
    {
      "name": "poetry update", // Poetry command
      "type": "python",
      "request": "launch",
      "module": "poetry",
      "args": [
        "-vvv",
        "update"
      ]
    },
    {
      "name": "List results", // Your command using Poetry
      "type": "python",
      "request": "launch",
      "module": "poetry",
      "args": ["run", "list", "input/results"]
    }
  ]
}
Create new Poetry project
Interactive graph
On this page
Create project
Create local .python-version
Add dependencies
Configure perflint
Other configurations
Optional: Debug calling Poetry