Skip to main content

Estimated reading time: 3 minute(s).

Avoid Python Dependency Hell When Creating New Packages

Example Scenario: Using Pydantic as a dependency in a package

When developing a Python package that depends on Pydantic, or any other library, and aiming to minimize dependency issues for users, there are several best practices you can follow to reduce the likelihood of creating or exacerbating dependency conflicts. Here’s a comprehensive guide:

1. Specify Dependency Versions Carefully

Use version specifiers to manage dependencies in a way that balances the need for specific functionality and stability with the flexibility required by other packages that might use yours. A few common strategies are:

Here’s how you might specify these in your setup.py or pyproject.toml :

# setup.py example
install_requires=[
    'pydantic>=1.8,<2.0'
]
# pyproject.toml example
[tool.poetry.dependencies]
pydantic = "^1.8"

2. Regularly Update Your Dependencies

3. Testing Against Multiple Dependency Versions

Here’s an example tox.ini configuration that tests multiple Pydantic versions:

[tox]
envlist = py{36,37,38,39}-pydantic{1.8,1.9}

[testenv]
deps =
    pydantic1.8: pydantic>=1.8,<1.9
    pydantic1.9: pydantic>=1.9,<2.0
commands =
    pytest tests/

4. Documentation

5. Isolate Your Package Environment

6. Provide an Escape Hatch

Using these strategies, you can help minimize dependency conflicts and provide a more robust and user-friendly Python package.