search
HomeBackend DevelopmentPython TutorialLearn the Python module import mechanism and the specifications of large projects

Python Video Tutorial column today introduces you to learn the Python module import mechanism and the specifications of large projects.

Learn the Python module import mechanism and the specifications of large projects

Preface

In the process of using Python in our daily projects, we often need to solve the import problems of each module, and we often encounter reference path searches. There are problems such as cross-importing modules and so on, so I wrote this article to describe Python’s module import mechanism and the module import specifications we should follow in our daily large-scale projects

Python module import

In daily programming, in order to reuse the code logic we have written, we will encapsulate these codes into modules. When needed, we can directly import and reuse them to improve our development efficiency. Modules can define functions, classes, variables, and can also contain executable code. There are three types of module sources: ①Python built-in modules (standard library); ②Third-party modules; ③ Custom modules;

Import principle

The import of modules is generally done by using the import keyword in the file header. Importing a module is equivalent to executing it once and then importing it. module, and then establishes a connection with the namespace of the imported module in this namespace, which is equivalent to creating a new variable in this namespace. The name of this variable is the name of the imported module and points to the namespace of the imported module. Therefore, the imported module is equivalent to a variable. Therefore, importing the same module multiple times will only be executed the first time it is imported. (Subsequent imports will determine that the module variable already exists and will not be executed)

Learn the Python module import mechanism and the specifications of large projects

Path search mechanism

Every imported module will be in the Python built-in dictionary sys.modules. Once Python is started, it will be Loaded in memory, when we import new modules, sys.modules will automatically record the module. Python's module search path mechanism is:

  1. Check whether the module exists in all paths in sys.path, and if so, open up a new space to load the module;
  2. Check whether there are built-in packages or installed third-party packages in sys.modules, and if so, open up a new space to load the module;

So for the modules we write ourselves, if we encapsulate and publish When it comes to PyPi, you can install it directly with pip install and load it into the memory at startup. You can view it through sys.modules For modules that only need to be reused in this project, we add their paths to sys.path in the reused code, and the module can also be referenced.

Absolute path import

All module imports start from the "root node". The location of the root node is determined by the path in sys.path. The root directory of the project is usually automatically in sys.path. If you want the program to be executed everywhere, you need to manually modify sys.path

import sys,os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))#项目根目录所在的绝对路径sys.path.append(BASE_DIR)import A, B #导入A、B包复制代码

Relative path import

Just care about the module location relative to your current directory. It cannot be executed directly inside the package (an error will be reported). No matter where the root node is, the relative position of the modules within the package is correct.

#from . import b2 #这种导入方式会报错,只有在包内部直接执行的时候才可以这样导入。import b2#正确b2.print_b2()复制代码

Python module import FAQ

  • When importing a package name individually, all submodules contained in the package will not be imported. Solution: The imported package also contains imports of other packages. At this time, you need to import all modules under the package in the init.py file of each package, so that the top layer can directly reference the bottom layer. The classes and methods of the package

initFile

When there is init.py in a folder, it means this file A folder is a package (package), and multiple modules (modules) under it form a whole, and these modules (modules) can be imported into other codes through the same package (package). The init.py file is used to organize packages, facilitate management of references between modules, and control the import behavior of packages.

This file can contain no content, that is, it is an empty file (when it is empty, nothing can be done by just using the import [package] form). It just needs to exist, which is equivalent to a mark.

In python3, even if there is no init.py file under the package, the import package will still not report an error, while in python2, the file must be under the package, otherwise import The package will report an error

allVariable

all is an important variable used to specify that this package (package) is imported * When, which modules will be imported into [the current scope]. Modules not in the all list will not be referenced by other programs. You can rewrite all, such as all= ['name of module 1 of the current package to which it belongs', 'name of module 1']. If this is written, the module name in the list will be used. Import

name变量

在包内部直接运行时,包的name == 'main',但是在外部导入包是,可以通过

if __name__ == '__main__':复制代码

来避免实现包内部调试时的逻辑

循环导入

当两个模块A和B之间相互import时,就会出现循环导入的问题,此时程序运行会报错:can not import name xxx,如:

# a.pyprint('from a.py')from b import x

y = 'a'复制代码
# b.pyprint('from b.py')from a import y

x = 'b'复制代码

我们来分析一下这种错误是怎么出现的:

  1. 在sys.modules中查找 符号“module b”;
  2. 如果符号“module b”存在,则获得符号“module b”对应的module对象; 从的dict中获得 符号“x”对应的对象。如果“x”不存在,则抛出异常“ImportError: cannot import name ‘x’”
  3. 如果符号“module b”不存在,则创建一个新的 module对象。不过此时该新module对象的dict为空。然后执行module b.py文件中的语句,填充的dict

因此在a.py中执行from b import x的顺序就是1->3,先引入b,b里面from a import y由相当于执行了a.py,顺序是1->2,因为此时b已经引入所以不会执行3,2中无法找到x对象,因为引入b时还没执行到x='b'这一步,所以报错了

解决办法

  1. 延迟导入,把import语句写在方法/函数里,将它的作用域限制在局部;
  2. 顶层先引入模块,再把from x import y改成import x.y形式;
  3. 其实出现循环引用问题的根本原因是程序设计不合理,每个包都应该由上层使用的模块去导入,而不应该在包与包之间各种相互导入,所以应该更改代码布局,可合并或分离竞争资源;

大型项目中Python模块导入规范

分离模块,将同一类别的模块放在同一目录下,形成类别分明的目录架构,如:

Learn the Python module import mechanism and the specifications of large projects

  1. 每一个模块目录都要写init.py文件,可以同时定义all限定可导入的范围;
  2. 源码根目录可以定义BASE_DIR,限定好根目录路径,启动py文件可以用绝对路径导入各个模块,将必要模块都加入到sys.path中;
  3. 各个服务之间(例如model需要引入common的模块方法),可以通过相对路径引用模块;
  4. 程序设计时避免循环导入,可由调用者(服务文件)作为上层第三方引入需要的各个模块,这样就可以减少各个模块的相互导入。

更多相关免费学习推荐:python视频教程

The above is the detailed content of Learn the Python module import mechanism and the specifications of large projects. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools