Importing Modules in Python
--
In this article, we’ll talk about how to get and use code that other people have written in your own programs and libraries.
With the core functionality introduced in the previous articles, we can do pretty much anything we want to do. Doing things from scratch, however, takes either a lot of time or a lot of experience.
- Previous Article
- Next Article
- All Articles
For example, say you wanted to make a program that could generate math-based animations like the ones in my other articles. At a minimum, you would need to
- convert mathematical descriptions of objects into vertices, paths, and planes,
- render those vertices, paths, and planes,
- render text,
- render math equations and formulas,
- allow users to control and plan out scenes,
- and allow users to generate multiple frames that you can then combine into an image.
Even in this description, I’m compressing a lot of implementation details and leaving out some features. While it is possible for one person to do all these things, it’s rarely necessary because people have already done some of these things and given out free access to the code to everyone. In this article, we’re going to talk about how you can access this code and how you can import it into your project.
Topics Covered
As always, here are the topics covered. If you know these well, skip this article (and maybe read one of my other articles).
import
and Modulesre
and Regular Expressions- Installing Libraries with
pip3
This article is a little weird because we’re combining program entry points with the more general topic of importing modules, but they should flow into each other.
import
and Modules
You might notice that up until this point, we’ve been writing everything we need in one file. As our programs grow in size and complexity, we’ll often want to break it apart into multiple files for both ease on the programmers who’ll work on the project and reuse of code.