Python module

Every Python file is a module.

You can import modules from other files, which is the basis of any medium-complexity program, because it promotes wise organization and code reuse.

In a typical Python program, a file serves as the entry point. Other files are modules and expose functions that we can call from other files.

filedog.pyContains the following code:

def bark():

We can useimport, And then we can use dot notation to refer to the function,dog.bark():

import dog


Or we can usefrom .. importSyntax and call the function directly:

from dog import bark


The first strategy allows us to load all the content defined in the file.

The second strategy allows us to choose what we need.

These modules are specific to your program, and import depends on the location of the file in the file system.

Suppose you putdog.pyin alibsubfolder.

In that folder, you need to create a file This tells Python that the folder contains modules.

Now you can choose, you can importdogFromlib:

from lib import dog


Or you can refer todogImport module specific functions

from import bark


More python tutorials: