Dependency management in Python -2024

Satvik Goyal
ScaleReal
Published in
4 min readJun 5, 2024

Welcome to the dawn of a new era of Django Setup 2024! Get ready to embark on a journey of exploration and discovery as we uncover the secrets of the Modern Django Setup. Keep your eyes open for more exciting blog posts in this series, coming soon!

Today, we’re going to talk about an important aspect of Python 🐍 development: dependency management and version management. You might be thinking, “What are these fancy terms?” Well, don’t worry, we’ll break it down for you in a way that’s easy to understand and enjoyable to read. So grab your favourite beverage 🍹, sit back, and let’s dive in!

At ScaleReal we work extensively on Python/Django Applications and have built a lot of highly scalable applications for various clients of ours.

Here’s a list of everything that will be covered in this article:

  1. What is Dependency Management?
  2. Types of Dependencies?
  3. Dependency hell
  4. Simplifying Python Dependency Management
  5. Conclusion

What is Dependency management?

In programming, dependency refers to the degree to which one piece of code relies on another for correct functionality. A software dependency is a relationship between software components where one component relies on the other to work properly.

Let’s take a scenario, suppose we are developing an e-commerce application in Python. We would require certain things for that like a framework in Python to handle our backend services, a library or package to handle the database operations and a package to manage the payment gateway. All of these components which are very fundamental to a simple e-commerce platform require many developers and development hours to be built. The silver lining is that they are already created and available for us and we can use them in our application so that we won’t have to develop them on our own like we don’t need to make a screwdriver to unscrew a screw. This makes our development very simple and efficient as we can focus on the main application as all the dependencies are already taken care of.

Types of Dependencies:

Direct Dependency

A software dependency is used by a software component directly.
Example: a Python function may require the pandas library, meaning the file has a direct dependency on that package.
The package can be imported using import, like this:

import pandas

Transitive Dependency

In the above example, the function uses the pandas package. But the pandas package itself uses the NumPy package. This means that the function has a transitive dependency on the numpy package. It might not be explicitly declared in the Python file, but it is indirectly used by the Pandas package.

Dependency Hell

Dependency hell is a situation in which the packages and libraries used in our software development become hard to manage resulting in errors and conflicts. This may occur because different components of software require the same dependency but different versions of it. That is why it is important to have proper dependency managers and strategies in place for smooth and efficient development.

Simplifying Python Dependency Management

https://imgs.xkcd.com/comics/python_environment.png

Below are some of the Dependency Management tools for python

  1. Pip: Pip is the default package manager for Python. It is used to install, upgrade, and manage Python packages. Pip is usually pre-installed with Python.
  2. Pyenv: Pyenv is a popular tool for managing multiple Python installations on a single machine. It allows you to easily switch between different Python versions and set specific Python versions for different projects.
    Basic installation command:
    curl <https://pyenv.run> (for Unix-based systems)
    Virtual environment creation command:
    pyenv virtualenv <python_version> <env_name>
  3. Pipenv: Pipenv is a tool that combines package management (using Pip) and virtual environment management in Python. It simplifies managing dependencies and isolates Python environments for different projects.
    Basic installation command:
    pip install pipenv
    Virtual environment creation command:
    pipenv install (inside the project directory).
  4. Asdf: Asdf is a language-agnostic version manager that can be used for managing multiple programming languages, including Python. It provides a consistent way to manage different versions of Python and other languages.
    Basic installation command:
    git clone <https://github.com/asdf-vm/asdf.git> ~/.asdf --branch v0.8.0
    (for Unix-based systems)
    Virtual environment creation command:
    asdf virtualenv <language> <env_name> <version>
    (e.g. asdf virtualenv python myenv 3.9.7).
  5. Poetry: Poetry is a modern Python dependency and project management tool. It simplifies the process of managing dependencies, building virtual environments, and handling other aspects of Python project management. Basic installation command:
    curl -sSL <https://install.python-poetry.org> | python3 -
    Virtual environment creation command:
    poetry install
    (inside the project directory).

Conclusion

Dependencies are a very crucial part of any software development as they abstract a lot of functionality which we don’t have to work on as they are already built. But they come with their own sets of difficulties like dependency hell. To manage all that we need efficient dependency managers. For Python development Pip, Pipenv and Poetry are some of the best dependency managers out there.

At Scalereal We believe in Sharing and Open Source. Thanks for reading! If you enjoyed reading this article, please click the 👏 button and share it with everyone. Feel free to leave a comment 💬 below.

Sharing is Caring! Thank You :) 🙏

If you need any help, you can contact me on LinkedIn or Github.

~Satvik Goyal

--

--