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