Skip to content

Instantly share code, notes, and snippets.

@aladagemre
Last active June 26, 2025 15:20
Show Gist options
  • Save aladagemre/c0ab09a7bed191c4ef8d49c19b71af6c to your computer and use it in GitHub Desktop.
Save aladagemre/c0ab09a7bed191c4ef8d49c19b71af6c to your computer and use it in GitHub Desktop.
Converter: pip requirements to pyprojec.toml dependencies
"""
If you have a project with requirements/base.txt, requirements/local.txt, requirements/production.txt,
and you want to use uv for managing dependencies, this script will read all those deps from txt files
and adds to pyproject.toml so that you can install them with
uv sync --extra local
uv sync --extra production
"""
from pathlib import Path
import toml
base = Path("requirements/base.txt").read_text().splitlines()
local = Path("requirements/local.txt").read_text().splitlines()
prod = Path("requirements/production.txt").read_text().splitlines()
base = [dep.split("#")[0].strip() for dep in base if "#" in dep]
local = [dep.split("#")[0].strip() for dep in local if "#" in dep]
prod = [dep.split("#")[0].strip() for dep in prod if "#" in dep]
base = [dep for dep in base if dep]
local = [dep for dep in local if dep]
prod = [dep for dep in prod if dep]
with open("pyproject.toml", "r") as f:
pyproject = toml.load(f)
if not pyproject.get("project"):
pyproject["project"] = {"name": "Project Name", "version": "0.1"}
pyproject["project"]["dependencies"] = base
pyproject["project"]["optional-dependencies"] = {
"local": local,
"production": prod,
}
with open("pyproject.toml", "w") as f:
toml.dump(pyproject, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment