DEV Community

Patrice Gauthier
Patrice Gauthier

Posted on

It's time to ditch the complicated and slow Python setup

At my work I was faced with a complicated installation process for Python. The Python version of the project was older than the one shipped by my OS. I've wasted a morning to set it right. So I've wondered if there was a more straight forward way to install it and if there was a more developer friendly approch to managing dependencies than manually setting the version in requirements.txt. I've looked for a solution and I found one. It's called Rye.

Rye: The Modern Python Project Management Tool

Rye is an awesome project management tool for Python that aims to simplify dependency management and project set-up. It offers a user-friendly approach to handling Python projects, making it easier for developers to manage their work efficiently.

Key Features of Rye

  • Simplified Dependency Management: Rye streamlines the process of managing project dependencies.
  • Virtual Environment Handling: It automatically creates and manages virtual environments for your projects.
  • Project Initialization: Rye provides easy-to-use commands for setting up new projects or integrating with existing ones.
  • Consistent Environments: Ensures consistency across different development environments.

Setting it up in a New Project

To start a new project with Rye, follow these steps:

  1. Install it: Follow your OS instructions. It will ask you which shims you want to use (choose the one of Rye's) then which Python version to use as it manages python versions.

  2. Initialize a python project:

   rye init <directory_name>
Enter fullscreen mode Exit fullscreen mode

This command will create some files including the new pyproject.toml configuration file.

  1. Install the initial dependencies:
   rye sync
Enter fullscreen mode Exit fullscreen mode

Running this command set up a virtual environment for your project as .venv and install the dependencies specified in the pyproject.toml. It uses uv by default (it's own Python tool for handling dependencies written in Rust) or pip-tools. Notice that there's no requirements.txt. It's all handled by Rye now using lock files.

  1. Add Dependencies: Use Rye to add any necessary dependencies:
   rye add package_name
Enter fullscreen mode Exit fullscreen mode
  1. Sync again to install dependencies
   rye sync
Enter fullscreen mode Exit fullscreen mode
  1. That's it!: Just repeat the sync step when adding dependencies or modifying the project file!

Integrating Rye into an Existing Project

If you have an existing Python project that uses a requirements.txt file, you can easily move to Rye. Here's how:

  1. Initialize Rye with Existing Requirements:
   rye init -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

This command will:

  • Create a pyproject.toml file and include your dependencies in it.
  • Set up a .venv virtual environment (or it will asks if it exists already)
  1. Review and Adjust: Check the newly created pyproject.toml file and make any necessary adjustments. Delete the requirements.txt as it is unused.

  2. Update Your Development Process: Start using Rye commands for managing dependencies and your development environment.

Using Rye to manage Python versions

Rye is your main tool to manage the python ecosystem.

Best Practices When Using Rye

  • Use rye run to execute scripts in your project's environment
  • Leverage rye add and rye remove for managing project dependencies
  • Commit your pyproject.toml file to version control, but exclude the .venv directory

A 15 min walktrough by the author

Conclusion

Rye offers a modern and efficient approach to Python project management. Whether you're starting a new project or integrating it into an existing one, Rye simplifies many aspects of Python development. By following the steps outlined above, you can harness the power of Rye to streamline your Python project workflow.

Top comments (0)