Skip to content

Instantly share code, notes, and snippets.

@brndnsmth
Last active March 9, 2024 15:00
Show Gist options
  • Save brndnsmth/a2f4dc123b5bfac66d0441a31a823fc9 to your computer and use it in GitHub Desktop.
Save brndnsmth/a2f4dc123b5bfac66d0441a31a823fc9 to your computer and use it in GitHub Desktop.
Setting Up a Python Project Environment with pip-tools

Setting Up a Python Project Environment with pip-tools

This guide will walk you through the process of setting up a Python project environment using pip-tools, a set of command-line tools designed to complement pip. You can find more information about pip-tools on their GitHub repository.

https://github.com/jazzband/pip-tools

Step 1: Create a Virtual Environment

A virtual environment is an isolated Python environment that allows you to manage dependencies for different projects separately. To create a virtual environment, run the following commands in your terminal:

python3 -m venv .venv
source .venv/bin/activate

Step 2: Install pip-tools

With your virtual environment activated, install pip-tools using pip. This tool will help you compile and synchronize your project’s dependencies effectively.

pip install pip-tools

Step 3: Create a requirements.in File

The requirements.in file is where you’ll specify your project’s direct dependencies. Create this file using the touch command:

touch requirements.in

Step 4: Add Dependencies to requirements.in

Open the requirements.in file and add your desired packages. You can specify packages in different ways:

To install the latest version available:

django

To install a specific version:

django==4.0.0

To install the latest patch version within a minor version:

django~=4.0.0

Step 5: Compile Your Dependencies

Run the pip-compile command to generate a requirements.txt file, which will contain all your project’s dependencies, including the transitive ones, with their exact versions.

pip-compile

Step 6: Install Your Dependencies

Finally, install all the dependencies specified in your requirements.txt file using pip:

pip install -r requirements.txt

You’ll should now have a fully set up Python project environment with all the necessary dependencies installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment