All technological notes.
module: A document with definitions of functions and various statements written in Python
functions, classes, and variables.Create a Module
.py.Syntax
Import Module
import module_nameRe-name Module
import module_name as aliasImport Function & Variable
from module_name import function_namefrom module_name import variable_namemodule_person.py
def greeting(name):
print("Hello, " + name)
print(person1)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
module_demo.py
print("\n--------Import a Module--------\n")
import module_person
# call the members of the imported module
module_person.greeting("John") # function
person = module_person.person1 # Variables
print(person)
# Hello, John
# {'name': 'John', 'age': 36, 'country': 'Norway'}
# {'name': 'John', 'age': 36, 'country': 'Norway'}
print("\n--------Re-naming a Module--------\n")
import module_person as p # rename imported module
p.greeting("John")
person = p.person1
print(person)
# Hello, John
# {'name': 'John', 'age': 36, 'country': 'Norway'}
# {'name': 'John', 'age': 36, 'country': 'Norway'}
print("\n--------Import a Function--------\n")
from module_person import greeting
greeting("John")
# Hello, John
# {'name': 'John', 'age': 36, 'country': 'Norway'}
print("\n--------Import a Variable--------\n")
from module_person import person1
person = person1
print(person)
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() Functiondir(): list all the function names (or variable names) in a module.
import platform
x = dir(platform)
print(x)
| 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 |
.py File# target file: main.py
python main.py
Pass arguments from command line
sys.argv to accept argumentspython file_name.py [arguement]py Programming_Language\python\lab.py john 16
import sys
print(sys.argv[1:]) # ['john', '16']
if __name__ == __main__:Unlike other languages, Python has no main() function that gets run automatically. The main() function is implicitly all the code at the top level.
the top-level code is an if block. To identify whether a .py file is original or a imported file:
if __name__ == "__main__":
__name__ is a built-in variable which evaluate to the name of the current module.
__name__ instead is set to the string "__main__".__name__ is name of the imported module, not "__main__".A module is basically You may choose to define functions, classes, or variables in a module.
module: a simple Python script with a .py extension file that defines functions, classes, or variables .
package: a directory of a collection of modules.
__init__.py file by which the interpreter interprets it as a package.
__init__.py Python File works as a Constructor for the Python Package.library: a reusable chunk of code.
package is a collection of modules, a library is a collection of packages.Python frameworks: a collection of modules and packages that help programmers to fast track the development process.
pip: The standard package manager for Python
Download pip: https://pypi.org/project/pip/
Find pacakge: https://pypi.org/
import a Package: import <package>
Pip Commands
| 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 |