Four ways to import libraries in python

Table of contents


foreword

The library can be understood as a toolkit abstractly, and the methods in the library can be understood as various tools in the toolkit, and each tool has different functions.

The name of the example library in this article is pygame, which is the name of the toolkit. The example method is init, which is one of the tools in the toolkit of pygame, and its function is to initialize.


1. Import library name

import library directly

import pygame
pygame.init()

At this time, I found that when using the tools in the toolkit, it is troublesome to completely write the name of the toolkit.

Two, import library name as alias (variable name)

Take another alias after importing the library

import pygame as pg
pg.init()

At this time, I found that when using the tools in the toolkit, it is not necessary to write the full name of the toolkit, just use the alias.

But once the alias is duplicated with others, an error will occur.

Three, from library name import method name

Import one of the methods in the library

from pygame import init
init()

At this time, we found that when using the init tool, we don't need the name of the toolkit, just write the tool name directly.

But once the tool name is duplicated with others, an error will occur.

Four, from library name import *

import all methods in the library

from pygame import *
init()

At this time, we found that when using the init tool, we don't need the name of the toolkit, just write the tool name directly.

The same is true when using other tools.

But once the tool name is duplicated with others, an error will occur.

Guess you like

Origin blog.csdn.net/jjjzzz2/article/details/130130816
Recommended