HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

5) Virtual Environments and Packages Lesson

What are Dependencies

4 min to complete · By Martin Breuss

Now that you've opened the floodgates to access PyPI through pip, you might be tempted to race ahead and install some of the exciting external packages that you may--or may not--have heard of.

Horse racing in Korea - Photo by https://unsplash.com/@cadop Mathew Schwartz on Unsplash

Photo by Mathew Schwartz https://unsplash.com/@cadop

But hold your horses!

With the bustling activity as a developer, when the gears are oiled, and grinding feels like a morning jog, your dependencies can quickly get out of hand.

Python Dependencies

Eventually, you'll want to build Python projects that use the rich ecosystem of modules and packages that are out there.

A dependency is code that someone else wrote and that you use in your own project. The functionality of your own code depends on the code you pulled in from an external source.

The developers who wrote the package put a lot of effort into writing code that abstracts away implementation details and makes it possible for you to build on top of that. They did that so that you won't have to reinvent the wheels they've already built.

Using dependencies is very common when you build out more complex programs, and it's extremely helpful for a quicker and more productive development process.

However, as a professional developer, you're likely to work on more than just one project at any given time. Each project might require different dependencies. Or, what makes it even more complicated is that your projects might require the same packages but different versions of them.

Python Dependencies Example

You might work in web development and use Django, a Python web framework that you can install as an external package through pip.

Colorful illustration of a light bulb

Info: Django is just an example of a dependency you might have in your code. Anything that you python3 -m pip install is a dependency.

As a sought-after web developer, you might be maintaining three different Django projects for three different clients. And it turns out that they're all using different versions of Django.

Your system-wide Python can have only one version of Django installed at a time, so you'd quickly run into trouble in such a situation.

Using virtual environments represents a solution to this issue. They allow you to compartmentalize the dependencies for each project into a specific environment and keep them separate from all your other projects. You'll learn more about virtual environments in the next lesson.

Summary: What Are Dependencies

  • Any code that you install from external sources is called a dependency.
  • Dependencies can clash with each other, so you should avoid installing packages system-wide and instead use virtual environments to keep them separate.