Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the import statement in python

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use import sentences in python". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is the use of the import statement? The import statement is used to import other python files (called module module) and use the classes, methods, or variables defined in the module for code reuse.

Format 1:

Import module name 1 [as alias 1], module name 2 [as alias 2],...

Description: 1) import the whole module; 2) [as alias] part can be absent, take aliases to simplify the reference; 3) use the format of the members in the import module: module name [or alias]. Members

Members include properties (variables) or functions.

Format 2:

From module name import member name 1 [as alias 1], member name 2 [as alias 2],...

Description: 1) Import a member of the module; 2) you can use * to represent all members, such as the from module name import *

If you import the module in the package, change the module name in the above syntax to the package name. Module name

Tip: import xxx and from xxx import yyy, the difference between the two is:

Import xxx, you need to use variables, functions, classes, etc., in the import module through xxx.yyy.

From xxx import yyy, which can be called directly as yyy.

For example

Import turtleturtle.circle (100)

Or

Import turtle as tt.circle (100)

Or

From turtle import * circle (100)

Import module_name . That is, the module name is directly followed by import. In this case, Python will look for this module in two places, the first is sys.path (you can run the code import sys; print (sys.path) to check its location), the directory where the os module is located is in the list sys.path, and the general installation directory of the Python library can be found in sys.path (the premise is to add the Python installation directory to the computer's environment variable), so for the installed library, we can directly import. The second place is the directory where the file is running.

To have a deeper understanding, you need to be familiar with several basic concepts.

Module (module)

Python, in general, is a file with the suffix .py, and other file types that can be used as module are ".pyo", ".pyc", ".pyd", ".pyw" and ".dll", but Python beginners hardly need it. They are the smallest units that belong to the Python code carrier, so separate files are called "modules".

Module can define functions, classes, variables, and can also contain executable code. There are three sources of module:

① Python built-in modules (standard library)

② third-party module

③ custom module.

Package (package)

To avoid module name conflicts, Python introduces a method of organizing modules by directory, called package. A package is a folder that contains multiple modules.

Before Python 3. 3, a directory that wanted to be imported as package had to contain the _ _ init__.py file, while in Python 3. 3 and later, the _ _ init__.py file could not be needed.

Contains the _ _ init__.py file package, the _ _ init__.py file file can not write anything, that is, the empty file, can exist, equivalent to a tag. This kind of package is called "Regular packages" (ordinary package).

The "Namespace Packages" (namespace package) has been introduced since the Python3.3 version, which does not have _ _ init__.py.

Absolute import and relative import

Suppose the directory structure of our project (Project) is shown in the following figure:

Absolute import

The absolute path requires us to start with the top-level folder and provide a complete and detailed import path for each package or module. Such as:

From package1 import mudule1from package1.module2 import Fxfrom package2 import Cxfrom package2.subpackage1.module5 import Fy relative Import:

When we use relative import, we need to give the relative and current location, where we want to import the resource.

Relative import can be divided into "implicit relative import" and "explicit relative import". For example, if we want to reference the module4 module in package2/module3.py, we can write this.

Import module4 # implicitly relative import from. Import module4 # explicit relative Import from package2 import module4 # absolute Import

In the code. Represents the directory where the current file is located, if it is.. It means that the directory above the directory, three, four. And so on. As you can see, the implicit relative import only implies the condition of the current directory compared to the explicit relative import, but it is easy to cause confusion, so it is officially eliminated in PEP 328, after all, "Explicit is better than implicit".

To import class Cx and function Fy into package2/module3.py, you can write

# package2/module3.pyimport Cx # implicitly relative import from. Import Cx # explicitly imports from .subpackage1.module5 import Fy "how to use import statements in python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report