Write a module in python

Creating Python modules is something that most Python programmers do every day without even thinking about it. Any time you save a new Python script, you have created a new module. You can import your module into other modules.

Video: Python Modules

A package is a collection of related modules. The things you import into your scripts from the standard library are modules or packages.

write a module in python

In this chapter, we will learn how to create modules and packages. We will begin by creating a super simple module. This module will provide us with basic arithmetic and no error handling. This pyython has issues, of course.

write a module in python

The point is that if you save this code, you have a fully qualified module. Now what can you do with a module anyway? You can import it and use any of the defined functions or classes that are inside it.

write a module in python

And we could make it executable with a little spit and polish. You should spend some time to figure out one of these modules as a learning exercise. In the meantime, we will move write a module in python to packages!

Now we just need z replicate this structure in our own package. Create each of these files in a folder tree like the above example.

Locating Modules

For the add, subtract, multiply and divide files, you can use the functions we created in the write a module in python example. For the sqrt. Now we should be able to use lython module once we have it on our Python path. Alternatively, you can edit the path on the fly in your test code.]

write a module in python