aliases: []
title: How to create a new Poetry project
tags:
- python
- poetry
permalink: how-to-create-poetry-project
date created: Friday, October 20th 2023, 9:40:14 pm
date modified: Sunday, August 3rd 2025, 9:52:07 am
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 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
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 these helpful dependencies.
poetry add python-dotenv python-decouple
poetry add --group dev black pre-commit pylint perflint ruff bandit pytest detect-secrets
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
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"]
}
]
}