Basic learning of modules and packages in Python

Table of contents

module

import module

Use the from...import statement to introduce the specified variable or function in the module

Use the import...as statement to alias the module

Use the dir() function to view all variables and functions in a module

Use the __name__ variable to determine whether a module is imported or executed directly

Bag

Notice

example


 

module

A module in Python refers to a file that can contain functions, classes, variables, and executable code, etc., and can be imported and used by other programs. Using modules can make the code more modular and easier to maintain.

Modules in Python are divided into two types: built-in modules and external modules. Built-in modules refer to modules that come with Python, such as math, random, datetime, etc., which can be used directly. External modules refer to modules that need to be installed through tools such as pip, such as numpy, pandas, etc.

Modules in Python can be imported through import statements, for example:

import math 

print(math.pi)

In the above code, the math module is introduced and the variable pi is output.

import module

In addition to the import statement, Python also provides several other ways to introduce modules, such as from...import statement, import...as statement, etc. Here are some code samples for module operations:

Use the from...import statement to introduce the specified variable or function in the module

from math import pi 

print(pi) 

In the above code, we only introduced the pi variable in the math module, but nothing else.

Use the import...as statement to alias the module

import math as m 

print(m.pi) 

In the above code, we use the import...as statement to give the math module an alias m, and output the variable pi.

Use the dir() function to view all variables and functions in a module

import math 

print(dir(math))

In the above code, we use the dir() function to view all the variables and functions in the math module.

Use the __name__ variable to determine whether a module is imported or executed directly

if __name__ == '__main__': 
    print('This is the main program.') 
else: 
    print('This is a module.') 

In the above code, we use the __name__ variable to judge whether the module is imported or executed directly. If it is executed directly, then output This is the main program., otherwise output This is a module..

The above are the basic introduction and operation examples of some Python modules. In actual development, the use and operation of modules is also very common and important. Mastering these knowledge points can improve the reusability and maintainability of the code.

Bag

In Python, a package (Package) is a hierarchical file directory that contains modules (Module) and subpackages (Subpackage). The function of package is to organize modules together, provide better namespace management and module reuse mechanism.

Packages in Python need to meet the following two conditions:

1. The package directory must contain a file __init__.pynamed . This file can be empty or contain some Python code.

2. The package directory must be on Python's module search path.

Below is an example of a package, where mypackageis the name of the package and __init__.pyfile is required.

mypackage/
 ├── __init__.py 
 ├── module1.py
 ├── module2.py
 └── subpackage/
   ├── __init__.py
   └── module3.py 

In this example, mypackagea package that contains two modules module1.pyand module2.pya subpackage that contains subpackagea module module3.py.

Modules in a package can be used with importthe statement . For example, to use the functions defined module1.pyin , you can use the following code:

from mypackage import module1 

module1.my_function() 

If you want to use modules from subpackages, you can use the following code:

from mypackage.subpackage import module3 

module3.my_function() 

Notice

When using packages, you need to pay attention to the following points:

1. The package directory name cannot be the same as the Python built-in module name, otherwise it will cause an error when importing the built-in module.

2. The package name should follow the naming convention of lowercase letters and words separated by underscores, which can improve the readability of the code.

3. Package-level variables and functions can be defined in __init__.pythe file , and these variables and functions can be shared by modules in the package.

4. Variables can be used in the package's __init__.pyfile __all__to specify the public interface of the package, and only the modules and variables listed __all__in will be imported into from mypackage import *the statement.

example

Here is an example package, containing a __init__.pyfile and a module module1.py:

mypackage/
 ├── __init__.py
 └── module1.py
A variable __version__ and a function my_function() are defined in the __init__.py file:
__version__ = '1.0.0' 

def my_function(): 
    print('Hello from my_function()') 

module1.pyVariables and functions from the package are used in:

from mypackage import __version__, my_function

def main():
    print(f'My package version is {__version__}')
    my_function()

if __name__ == '__main__':
    main()

Guess you like

Origin blog.csdn.net/weixin_71646897/article/details/130548513