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 that 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.
In : Standard modules are coming already installed while installing Python.
## Already existing standard library, comes already installed while installing Python package. import os
In : Checking some functions of os module
## using list dir, listing the files and folders in that particular directory. for e in os.listdir(r'C:\Users\gargr\Documents\mlsite\python\getting started with python'): print(e)
## These are the output files conyained in the directory. .ipynb_checkpoints Conditional Statements.ipynb Dictionary.ipynb file1.txt file2.txt Files in Python.ipynb
Installing new module
If you are on simple Python machine you can install using 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 dependenices comes installed in that but suppose you want some library not already installed like 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 a 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 -:
print(‘My module is working!’)
for e in l:
## importing the custom module import my_module
My module is working!
## Importing system library to check where it will search for modules either standard or ## custom. import sys l = ['a','b','c'] my_module.print_name(l) ## Printing where system will check for modules in python. print(sys.path)
## This is the list of directories as per my machine location of conda install. ## You can also check in simple Python as well. abc['', 'C:\\Users\\gargr\\AppData\\Local\\Continuum\\anaconda2\\envs\\py35\\python35.zip', 'C:\\Users\\gargr\\AppData\\Local\\Continuum\\anaconda2\\envs\\py35\\DLLs', 'C:\\Users\\gargr\\AppData\\Local\\Continuum\\anaconda2\\envs\\py35\\lib', 'C:\\Users\\gargr\\AppData\\Local\\Continuum\\anaconda2\\envs\\py35', 'C:\\Users\\gargr\\AppData\\Local\\Continuum\\anaconda2\\envs\\py35\\lib\\site-packages', 'C:\\Users\\gargr\\AppData\\Local\\Continuum\\anaconda2\\envs\\py35\\lib\\site-packages\\win32', 'C:\\Users\\gargr\\AppData\\Local\\Continuum\\anaconda2\\envs\\py35\\lib\\site-packages\\win32\\lib', 'C:\\Users\\gargr\\AppData\\Local\\Continuum\\anaconda2\\envs\\py35\\lib\\site-packages\\Pythonwin', 'C:\\Users\\gargr\\AppData\\Local\\Continuum\\anaconda2\\envs\\py35\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\gargr\\.ipython']
Suppose if I will replicate the whole process in Idle. Suppose I am doing here in Python 2.7. Here we created 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:\Python27\Lib\idlelib’, ‘C:\WINDOWS\SYSTEM32\python27.zip’, ‘C:\Python27\DLLs’, ‘C:\Python27\lib’, ‘C:\Python27\lib\plat-win’, ‘C:\Python27\lib\lib-tk’, ‘C:\Python27’, ‘C:\Python27\lib\site-packages’]
.In you 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!