Difference b/w Module and Library . How to Import a "Module" in Python.

Difference b/w Module and Library . How to Import a "Module" in Python.

ยท

2 min read

What is "Module" ?

Module is nothing but a "Python File" . Now , how to identify which one is the python file it's a question which people often think of themselves. A python file is that file which has created with (.py) extension. In simple words it contains variables , functions and classes.

What is "Package" ?

Package is collection or group of "Modules" .

What is "Library" ?

Library is a group of "Packages"

What is "Import" ?

"Import" is a pre-defined keyword in python.

How to "Import"?

STEP 1 - Create a folder with any name you want.

STEP 2 - Now open that folder with any "Code Editor" you prefer. In my case I am using "Visual Studio Code". ๐Ÿ‘‡๐Ÿ‘‡

STEP 3 - Then , create a file with (.py) extension . ๐Ÿ‘‡๐Ÿ‘‡

STEP 4 - Create another file where you want to import stuff but these both files must be exist in same folder. ๐Ÿ‘‡๐Ÿ‘‡

STEP 5 - As you know that I have now two files which are A1 & A2 . Now , let's create a variable in A1 let's see how I can import it. ๐Ÿ‘‡๐Ÿ‘‡

STEP 6 - To import make sure that you have write "Import" keyword and the file name which you want to "Import' in that particular file where you actual want to use any function , class or variable. Like here in my case I want to use "x" which is present in A1 and I want to use it in A2. ๐Ÿ‘‡๐Ÿ‘‡

STEP 7 - Now A1 is "imported" let's try to print "x". ๐Ÿ‘‡๐Ÿ‘‡

STEP 8 - Ohh! ๐Ÿ˜ฒ There is an error while printing but why? Have I made any mistake NO! ๐Ÿค” Why I am unable to access the element of A1 ??

๐Ÿ‘‰ I am facing this issue because I should write like this "print(A1.x)" this to print element of A1. ๐Ÿ‘‡๐Ÿ‘‡

STEP 9 - Now Everything is working fine...........๐Ÿ˜ƒ๐Ÿ˜ƒ

ย