pyproject.toml file for Python
Thepyproject.toml file is a modern way to specify all your project’s build system requirements and dependencies in a single file. This guide shows how to create, configure and use pyproject.toml to ensure consistency between development and production environments.
What is a pyproject.toml file?
Apyproject.toml lists dependencies, build system and other metadata for your Python project. This allows:
- You and your team to maintain identical environments
- New collaborators to reproduce the environment exactly
- Set up development and production dependencies separately
Create the pyproject.toml file
First, create a file named
pyproject.toml in the root of your project.Define your project details
In the
The project name must consist of ASCII letters, digits, underscores “_”, hyphens “-” and periods “.”. It must not start or end with an underscore, hyphen or period. For example:
pyproject.toml file, start by defining your project metadata. We will define the project name, version, description, and other relevant information.The project name must consist of ASCII letters, digits, underscores “_”, hyphens “-” and periods “.”. It must not start or end with an underscore, hyphen or period. For example:
pyproject.toml
List project dependencies
Next, we will list the dependencies required for our project. We can specify both development and production dependencies. The production dependencies are listed under the
You can specify versions for your dependencies using comparison operators like
dependencies section, while development dependencies can be listed under optional-dependencies with a specific group name (e.g., dev).You can specify versions for your dependencies using comparison operators like
==, >=, <… In the example below, we do not specify versions. This means that the latest will be installed.
It is recommended to specify exact versions to ensure reproducibility and prevent breakage from incompatible updates in production.pyproject.toml
Set up the build system
Finally, we need to specify the build system requirements. This is necessary for tools like pip to know how to build and install your project. We will use
setuptools as our build backend, which is a common choice for Python projects, but you can choose others like poetry.pyproject.toml
Install dependencies with pip
To install the dependencies listed in yourpyproject.toml file, you can use pip or other tools that support pyproject.toml. This allows you to work on your project while having the dependencies installed.
pyproject.toml file, install the dependencies listed under dependencies, and set up your project for development. If you want to install the development dependencies as well, you can use:
Best practices and tips:
- Keep updated: Review and update
pyproject.tomlregularly as you add new dependencies - Use exact versions: Always use
==instead of>,>=or no version to avoid surprises in production - Versioning: Commit
pyproject.tomlto git to track dependency changes and version updates - Isolated environment: Maintain one virtual environment per project to avoid global conflicts
- Test locally: Before deploying, test the file with
pip install .in a new environment
Next steps
With yourpyproject.toml file configured:
- Commit to your Git repository.
- Host your project on Square Cloud.
- Configure automatic deploy via GitHub.
- Welcome to professional Python application hosting!
pyproject.toml file is the foundation for any successful Python application, ensuring your application works perfectly on Square Cloud.
