What is a Module in Python
Modules are Python scripts, they are files ending with the .py extension.
Modules can contain functions and attributes, as well as, other modules. Similar to functions, modules allow us to reuse code blocks.
Import Module
To import a module we use the below syntax:
1
import <module_name>
Example:
1
import os
Type will be <class ‘module’>
To know what functions are available within a module we can use help.
1
help(os)
[!WARNING] This will return a very large output.
Helpful Functions in the OS Module
Get the current working directory:
1
os.getcwd()
- This will return the current working directory name, including any directories above it.
Example Output:
1
'/home/user_name/python_learning'
Changing directory:
1
os.chdir("/home/new_user")
We can check the current directory now:
1
os.getcwd()
Output:
1
'/home/new_user'
Module Attributes
Modules also are attributes. In contrast to functions, these are values that perform a task.
For example, we can use the os.environ attribute to get information about out local environment.
1
os.environ
[!NOTE] This is an attribute not a function, as we don’t include any parenthesis afterward. As we aren’t calling it like we would a function.
- Output will return a dictionary with values such as the location where Python is installed, and the timezone we are based in.
Important to Know
Importing a whole memory can require a lot of memory. Knowing this, we can import a single function from a module if that’s all we need from that module.
The syntax is below:
1
from os import chdir
We can import more than one function doing the following:
1
from os import chdir, getcwd
Now we can call the function without referring to the module first, because we haven’t imported the entire module so Python won’t know what os means.
1
getcwd()
Will return the same as our previous output:
1
'/home/new_user'