Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Python - Modules & Package

Back



Module


Example


Built-in Modules

import platform

x = platform.system()
print(x)  # Windows

Module Attributes

[print(idf, ":",  eval(idf)) for idf in dir()]
# __annotations__ : {}
# __builtins__ : <module 'builtins' (built-in)>
# __cached__ : None
# __doc__ : None
# __file__ : c:\Users\simon\Documents\IIS\Tech_Notes\Programming_Language\python\trial.py
# __loader__ : <_frozen_importlib_external.SourceFileLoader object at 0x0000028A498E6CD0>
# __name__ : __main__
# __package__ : None
# __spec__ : None

dir() Function

import platform

x = dir(platform)
print(x)

Python Built-in Module

Module Description
math math utilities
re regular expressions
json JSON
datetime date and time
sqlite3 use SQLite
os os utilities
random random number generation
statistics statistics generation
requests perform HTTP network requests
http to create HTTP servers
urllib to manage urlsservers

Run a .py File

# target file: main.py
python main.py
py Programming_Language\python\lab.py john 16
import sys

print(sys.argv[1:])   # ['john', '16']

if __name__ == __main__:

if __name__ == "__main__":

Package

A module is basically You may choose to define functions, classes, or variables in a module.


Pip

Command Description
pip --version check the pip version
pip list List installed packages
pip show <package> Show information about a package
pip install <package> downloaded and installed package
pip install <package>==1.0.4 installed specific version
pip install --upgrade <package> Upgrade package
pip uninstall <package> Uninstall a package.
pip install -r requirements.txt Install a list of requirements specified in a file
pip freeze Generate output suitable for a requirements file
pip freeze > requirements.txt Generate a txt requirements file

TOP