惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
D
DataBreaches.Net
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Recent Announcements
Recent Announcements
The Register - Security
The Register - Security
S
Secure Thoughts
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
V2EX - 技术
V2EX - 技术
腾讯CDC
GbyAI
GbyAI
G
Google Developers Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
Help Net Security
Help Net Security
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
AI
AI
MongoDB | Blog
MongoDB | Blog
Scott Helme
Scott Helme
J
Java Code Geeks
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
H
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
S
Security Affairs
TaoSecurity Blog
TaoSecurity Blog
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
Martin Fowler
Martin Fowler
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI

🛫Qifei's Blog

如何选择搭建梯子用的VPS? - 🛫Qifei's Blog Hexo博客页面引入足迹地图 - 🛫Qifei's Blog 机器学习和scikit-learn库基础学习笔记 - 🛫Qifei's Blog 利用国内服务器加速v2ray访问速度和规避检查 - 🛫Qifei's Blog Matplotlib_Bar3d绘制彩色带颜色标尺的3D柱形图 - 🛫Qifei's Blog Linux后台运行nostr虚荣公钥的挖掘 - 🛫Qifei's Blog Hexo博客添加Nostr_NIP-05认证 - 🛫Qifei's Blog Nostr_NIP-05认证服务简介和配置 - 🛫Qifei's Blog Onedrive_API大文件上传记录 - 🛫Qifei's Blog 反爬虫-如何检测有没有使用Puppeteer - 🛫Qifei's Blog Vuejs学习笔记 - 🛫Qifei's Blog OpenOffice连接Mysql数据库 - 🛫Qifei's Blog 安卓ToyVPN服务端从零开始读 - 🛫Qifei's Blog 在控制器中用JS验证CSS_Selector和Xpath - 🛫Qifei's Blog RWTH自习室自动预定程序(RWTH_Lernraum) - 🛫Qifei's Blog RWTH自习室自动预定程序(RWTH_Lernraum) - 🛫Qifei's Blog 如何利用Selenium实现更加高效的爬虫 - 🛫Qifei's Blog Python如何准确的计算Http请求中的Content_Length - 🛫Qifei's Blog 破解图片等资源跨域和防盗链阻拦 - 🛫Qifei's Blog Selenium-Webdriver接口 - 🛫Qifei's Blog
Python的模块构建和调用方式 - 🛫Qifei's Blog
Qifei · 2024-03-25 · via 🛫Qifei's Blog

1. 简单模块文件

当项目简单,模块单一时,我们无需建立复杂的目录,只用简单地在相同文件夹下建立一个模块文件,将以供调用的函数都存放其中。在另一个文件中,我们通过import模块文件来调用其中的函数。

目录结构:

1
2
-module.py
-main.py

在模块文件 module.py中书写两个函数

1
2
3
4
5
def hello():
print('hello')

def bye():
print('bye')

在测试文件 main.py中调用模块中的函数

1
2
3
4
from module import *

hello()
bye()

2. 构建Python包

2.1 基础用法

当我们的项目较大时,可以通过将所有功能封装在一个或多个文件夹下,建立复杂的Python包,并在主文件中引用包导出的函数来实现项目的分离和集成。

一个简单的项目目录结构:

1
2
3
4
-modules
-__init__.py
-module.py
-main.py

首先,我们在项目根目录下构建里一个名为modules的文件夹,文件夹里存放了2个文件,分别是 __init__.py用于表明此文件夹为Python包,内部不需要写任何的代码;module.py里写入模块的函数。

module.py内容如下

1
2
3
4
5
def hello():
print('hello')

def bye():
print('bye')

main.py里调用modules.module文件内的函数。

1
2
3
4
from modules.module import * 


hello()

2.2 进阶用法

在每次导入module内的函数时,我们都需要完整的写modules.module,有没有办法可以直接从modules导入所有函数呢?答案是有的。通过修改 __init__.py,我们可以在 main.py里直接用 from modules import *导入我们想用的所有函数。目录结构与2.1节一致,向 __init__.py中添加字段:

__init__.py内容

1
2
3
4
from .module import * 


__all__ = ['hello','bye']

需要注意的是:无论在包内还是包外,亦或者包的深层包的py文件,使用import引用其他包导出的函数,都是以最外层的文件,即main.py的路径为参考。换句话说,import绝对路径写法,包内py文件引用可以等价于在main.py中引用。而相对路径写法则参考的是当前py文件的路径。上面两种写法分别为相对路径和绝对路径写法。.module表示与init.py相同目录下的module文件,而from module import * 这种写法会报错,这是由于用了绝对路径,而在main.py的相同目录下无法找到module模块。

main.py内容

1
2
3
4
from modules import *

hello()
bye()