Home  >  Article  >  Backend Development  >  What is a Python module and how to define and use it

What is a Python module and how to define and use it

王林
王林forward
2023-05-04 10:40:062500browse

1. What is a module

We learned about variable types (integer, string, list, tuple...etc.). Then I also learned about function types, which actually means combining some variables and then implementing some functions. In fact, modules are the same. A module combines functions, variables, etc. to form a Python file. The name of this file is also the name of the module. It can be said that the module is the essence of the Python code.

What is a module?
Module: It is a python file. When the python file is used as a module, the file name is the module name, demo.py( demo is the module name)

Function: You can call the code and functions of other python files, which can be implemented and used more flexibly, and add various effects

How to use: We pass import (key words) to import the module

2. Classification of modules

Modules in Python are divided into three categories:

1. Built-in modules

2. Third-party modules

3. Custom modules

(1) Built-in module

Description:

Built-in module: The module that comes with Python after installing it can be used directly, such as time, os, re, random...

Note: When using it, you need to import

For example: import time

(2) Third-party modules

Instructions:

Third-party modules are not included in Python and need to be installed externally into Python. Yes, these modules are written by some big guys, we can install and use them, such as pygame, requests... etc.

##Installation:

pip -- python's own downloader

install -- download
uninstall -- uninstall

Prerequisite: pip does not set python environment variables If so, then this configuration cannot be found, so remember to configure the environment variables when downloading python. The default python download library is to use pip. If the python environment is not set up, pip cannot be used-->Repair/Reinstall

Suggestion: pycharm download module (first select cmd to download and then pycharm)

Method: Enter cmd, directly pip install module name

For example:

What is a Python module and how to define and use it

The download is completed

pip related instructions:

#Download module

pip install module name

#View module

pip list

#Uninstall module

pip uninstall module name

#Update pip Sometimes the version of pip is too low and the new library cannot be upgraded

python -m pip install --upgrade pip -i

# Accelerate through (cdn) proxy, download third-party modules:

pip install library name --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install requests --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple

(3 ) Custom module
As the name suggests, it is a module that you make yourself and use it yourself. Of course, for us beginners, the modules we write ourselves are very common, and those third-party modules It is also written by individuals, but those people are big guys or some teams, and we still have to practice for another two and a half years.

3. Use of modules

Import module:

import module name

(import is Import meaning)

Use module:

import module name

Module name.Function name()

Single import

from module name import function name/variable name

For example: from random import randint

Import all functions of this module

from module name import *

Note: After importing, we can use these functions directly, just There is no need to use the module name.function name() method, just use the function name(). However, this method has a big disadvantage, that is, when we directly use the names of these functions or variables, It may conflict with the names of the variables or functions we define, resulting in overwriting. It is generally not recommended to use this method

Give the module an alias

If the module name is too long and difficult to remember, you can give it an alias through as

import module name as alias

We can use the alias of this module directly later. , for example: import random as r

r.random()

4. Custom module

We can define a module ourselves and then execute the file Import it and use it directly, see the example:

This is a module I customized. I put this module file in the same directory as the executable file. When we want to use it, we can just import it directly.

What is a Python module and how to define and use it

def fun(n):
    if n==1:
        return 1
    return n*fun(n-1)
 
a=99
 
def qj():
    print('这个是我的模块')

What is a Python module and how to define and use it

Just import it very directly

5. Judgment of modules and executable files

The module is a py file and can be executed. When we import a module, the system has actually executed the module in advance and then executed the main file, but I want to make some of the module If this part is not executed, then I have to use a method to determine whether it is a module. If it is a module, then this part of the content will not be executed.

Method:

print(__name__) # If you run the code and the output is __main__, it means that this file is an executable file. If it returns a module name, it means that this is an executable file. The module is used

#模块代码
def fun(n):
    if n==1:
        return 1
    return n*fun(n-1)
print(__name__)
a=99
 
def qj():
    print('这个是我的模块')
#执行文件的代码
import demo
 
print(__name__)
print(demo.fun(4))

Output result:

What is a Python module and how to define and use it

The demo is output first. This demo is actually the name of the module (you can see that the module It is executed first, and then the main file is executed), and __main__ means that this is an executable file, so we can use this method to determine whether a file is a module or an executable file.

For example:

#模块代码
def fun(n):
    if n==1:
        return 1
    return n*fun(n-1)
if __name__=='__main__':
    print(123456)

When I call this module, 123456 will not be output, because this is a module.

The above is the detailed content of What is a Python module and how to define and use it. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete