Get startedGet started for free

Dealing with dependencies

1. Dealing with dependencies

Great work so far. Your package is coming along nicely. Now it's time to start thinking about how to make your package usable by other people.

2. What are dependencies?

It's quite likely your package will use other packages inside of it. Maybe you use NumPy functions, or Pandas DataFrames or anything else. Being a good package developer means not reinventing packages that already exist. These extra packages that you use inside yours are the dependencies of your package. When someone else, or just you, tries to install your package, you need to make sure that they have these packages installed too.

3. Adding dependencies to setup.py

To ensure that your users have the right packages installed, you can set the install-requires parameter inside the setup function of the setup script. Here you list the packages which your package depends on. Then when someone uses pip to install your package, pip will install these extra packages as well, unless the user already has them. These dependencies are updated often, and the version number changes. In this example, any version number of these packages is allowed,

4. Controlling dependency version

but sometimes your code will depend on functions which only exist in specific versions of another package. Let's say that a function used in our package was only introduced in pandas version one-point-zero. Here you say the user must have this version of pandas or later. You can also specify an exact version of a package. Here the user must have scipy version one-point-one. No other version will do. And here you say that the version of matplotlib must be at least two-point-two-point-one but cannot be version three or more.

5. Controlling dependency version

You should try to allow as many versions of these dependent packages as possible. If you restrict the version numbers too much then your user will not be able to install your package at the same time as other packages they need. You should also not have any unnecessary dependencies.

6. Python versions

Just like you specify which version of dependent packages a user needs to have, you can specify which version of Python they need to have. Here we say that the version of Python installed must be two-point-seven or greater, but cannot be three-point-zero or three-point-one. The star must be used when we are excluding version numbers.

7. Choosing dependency and package versions

You may be wondering how you find out which dependency versions and Python versions will work with your package. You can search online for the functions you use, and find which version of the package they were introduced in. A good place to start is the package history or release notes. You can also perform tests with multiple different package and Python versions. We'll cover this in a later lesson.

8. Making an environment for developers

A really important part of developing any software is reproducibility. You and your package co-authors need to have the exact same versions of all the dependent packages so you can track down bugs. This is different to the install-requires dependencies where you try to allow as many different versions as possible, here you want to know exactly which versions you are using. You can show all the package versions you have installed using the pip freeze command.

9. Making an environment for developers

You should export this information into a text file which you include with your package. Then anyone can install all of the packages in this file later using this pip install command. Having the exact same set of packages makes it easier to hunt down any bugs.

10. Let's practice!

Okay, time to add dependencies to your package. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.