Next, we are going to look at the modules i.e, how to use standard modules of python in your code in order to use its functionality.
In this, we will also cover how to use our own created modules in another python file. There are some standard modules like random, os, sys etc.
There are some modules which we can install in our system and use it.
Standard modules are coming already installed while installing Python and will check some functions of os module.
Installing a new module
If you are on simple Python machine you can install using the command.
If Python 2 and Python 3 both are installed Otherwise simple pip will work.
For Python 3
pip3 install numpy (package name)
For Python 2
pip install numpy (package name)
If you are using anaconda then most of the dependencies come installed in that but suppose you want some library not already installed like the pillow, you can install it as follows -:
-> Generally conda install package name.
In : Here first I created a my_module.py file which contains functions and will use as a module. If you are creating in the same current working directory then it will call easily. As if you are importing module then python will check in some specific directories only such as cwd, lib directory of where python is installed etc.
My_module.py file is as follows -:
We will check the importing module.
Suppose if I will replicate the whole process in Idle. Suppose I am doing here in Python 2.7. Here we created the same my_module.py file and one file which is calling this created module. The output will be as follows -:
My module is working
[‘C:Python27Libidlelib’, ‘C:WINDOWSSYSTEM32python27.zip’, ‘C:Python27DLLs’, ‘C:Python27lib’, ‘C:Python27libplat-win’, ‘C:Python27liblib-tk’, ‘C:Python27’, ‘C:Python27libsite-packages’]
.In your machine you will get different paths as per your installed location. Hope you understand how to create custom modules and call them in your files.
Stay tuned! Keep learning with us!