P30 模块和包
What's a module?
A python file with functions, classes and other components
Why use modules?
Break code down into reusable structures
Creating a module
#create a file
#then add in your appropriate code
def display(message,is_warning=False):
if is_warning:
print('Warning!!')
print(message)
Importing a module
#import module as namespace
import helpers
helpers.display('Not a warning')
#import all into current namespace
from helpers import *
display('Not a Warning')
#import specific items into current namespace
from helpers import display
display('Not a warning')
Installing packages
#Install an individual package
pip install colorama
#Install from a list of packages
pip install -r requirements.txt
#requirements.txt
colorama #colorama being a package that help you change the color of text when you print
本文介绍了Python中的模块概念,模块是包含函数和类等代码组件的文件。使用模块可以将大型程序分解为可重用的结构。我们学习了如何创建模块,通过`import`关键字导入模块,以及通过`from...import`导入特定功能。此外,还讨论了如何使用`pip`安装和管理Python包,包括从`requirements.txt`文件批量安装和理解包管理的重要性。

被折叠的 条评论
为什么被折叠?



