云学编程的第16天—【微软官方python入门教程 P33笔记】2021-11-16 模块和包 Modules

本文介绍了Python中的模块概念,模块是包含函数和类等代码组件的文件。使用模块可以将大型程序分解为可重用的结构。我们学习了如何创建模块,通过`import`关键字导入模块,以及通过`from...import`导入特定功能。此外,还讨论了如何使用`pip`安装和管理Python包,包括从`requirements.txt`文件批量安装和理解包管理的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值