Python modules allows us to reuse our own code or sometimes use somebody else's code.
We can write our own modules or we can use modules written by someone else like requests
,datetime
and etc
.
Note: It's just a python
file.
There built-in python modules that come by default. List can be found here LIST OF MODULES
- We can import modules by using
import
keyword - We can also give modules an
alias
when we have long module names likeimport random as r
. - We can also import few functions from the modules
from random import randint
. - If you want to import everything from random we do something like this
from random import *
Custom module is just file with python code.
# file1.py
def hello():
return "Hello"
def hey(name):
return f'Hey {name}'
# Importing a custom module
import file1 as fn
fn.hello()
fn.hey('Jake')
External modules can be found here PyPi
- We can install modules using
pip
,pip install <package-name>
. - Pip comes default in
Python 3.4
we can run usingpython3 -m pip install <package-name>
. print(dir(<package_name>))
This tells us about the attributes.print(help(package_name))
This tells us everything about the package
- We can use
autopep8
to fix whitespaces and ident our code - We can use
autopep8 --in-place <file_name>
- The
__name__
variable usually refers to the file name if its the file then it will interpreted as__main__
- If it's a module then
__name__
will be interpreted as the__file_name__
.
Note : use if __name__ = '__main__'