Skip to main content

Section 9.5 Packages and Dependencies

Note 9.5.1.

One key tenant of computer science (or anything, for that matter) is that we don’t want to reinvent the wheel if we don’t have to. A lot of very smart people have made very useful code, so why not use it?
The Python Package Index (PyPI) is a website that maintains packages, or code that other people have written for our use. You might also hear the term dependencies, which describes essentially the same thing: code that our code depends on.
PyPI can be accessed with pip, which is the command that you will use when installing dependencies. Pip should come preinstalled with most Python installations on Mac and Windows.
Some common packages include: pytest (for testing your code), python-dateutil (for working with dates), flask (for making websites), and more.
Some common packages for data science include numpy, matplotlib, scipy, pandas and more.
But this is all sounding like a lot to keep track of, right? How can we ensure consistency between versions installed on different people’s computers?
requirements.txt is a file we can specify for others to be able to easily install dependencies (which are needed to run our code), since others may not have them installed on their own computers yet.
The following code represents a requirements.txt file that specifies numpy as a dependency:
numpy==1.26.2
Note that dependencies are not installed immediately. For someone to install the requirements which you specify, they would run the command: pip install -r requirements.txt.
If you’re unsure what version of a dependency you have installed, you can run the command: pip show DEPENDENCY, like pip show numpy. Somewhat ironically, there are also packages that can autogenerate a requirements.txt file for you.

Note 9.5.2.

You have attempted of activities on this page.