It's time to ditch the complicated and slow Python setup
Tags: python
Published: 2024-07-11 14:03:06

---
title: It's time to ditch the complicated and slow Python setup
published: true
description: A guide and tutorial on Rye
tags: python 
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2024-07-07 19:20 +0000
---

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](https://rye.astral.sh/guide/installation/). 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>
   ```

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

3. **Install the initial dependencies**:
   ```
   rye sync
   ```

   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.

4. **Add Dependencies**: Use Rye to add any necessary dependencies:
   ```
   rye add package_name
   ```

5. **Sync again to install dependencies**
   ```
   rye sync
   ```

5. **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
   ```

   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)

3. **Review and Adjust**: Check the newly created `pyproject.toml` file and make any necessary adjustments. Delete the `requirements.txt` as it is unused.

4. **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
{% embed https://youtu.be/q99TYA7LnuA %}

## 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.