Importing in Python
Import functionality in Python allows you to use outside code.
You don’t have to write all code by yourself.
Of course you can write, all code on your own, if you wish too.
Biggest advantage of Python is, its huge collection of readily available collections of packages and modules.
What is Module ?
Module is a collection of self contained Python code in single or multiple files.
With standard Python installation some useful modules are pre-installed.(that’s why its called as batteries included)
When you start building application,
you will find, a need of some features in programming language.
That’s where Python’s huge collection of modules comes to your rescue.
More often than not, you need features like
file handling, system commands, networking, web-services,
document manipulation, data validation, etc..
With import keyword you can include required module,
and start building application in no time.
How to check what modules available?
Open Python shell and type
help() is standard built in function, which is part of standard Python installation.
more about this in built-in function. “modules” is string, an input parameter to help() function.
How to import a module?
A module can be imported fully or partially.
Style 1: Import whole module and include all its elements in your code.
In this example os module is getting imported.
os is pre-installed module, comes with standard Python installation.
getcwd() is a method in os module.
This method returns the current working directory.
If you wish to check current working directory. Type the below
With this style of import, (.) dot notation used to get the elements of the module.
You may also import module with an alias, or a short hand like
import <module_name> as <alias>
import random as rn
Where rn is short hand for random. random is also a built in module like os.
To get a random number between 1, 20
>>> import random as rn
or if you have your, own module let’s say my_module.py then
import my_module as mm
Style 2: Partial import of module
You can import specific elements from module.
here only getcwd() method is being included in your code.
from os import getcwd
Here only getcwd is imported from os module .
Notice while importing getcwd() , brackets are not required, only name of the element.
In your code getcwd() can be used directly, without using os name and dot notation.
getcwd() #notice os. is not needed
multiple specific elements can be imported with a comma separation.
from os import getcwd,close,chdir
note the syntax, while importing , () parenthesis are not used, but while calling a method () are used.
you can also import all elements by using
from os import *
It’s advised that you avoid this style, as during development.
You may get unexpected results due to namespace violation.
More about this in modules and packages.
What happens if you import a module multiple times ?
A module is loaded only once.
open a file editor and write the below code and save it as myfile.py, now you have your own module ready for import.
name = " Samson"
dob = "10/01/2000"
emp_id = 200012
print("name: ", name, "dob :",dob,"emp_id :",emp_id)
Start Python shell, and import myfile
>>> import myfile
name: Samson dob : 10/01/2000 emp_id : 200012
>>>now import myfile one more time.
>>> import myfile
Here myfile is imported twice.
You can see the employee data getting printed during the first import, and nothing is printed in the next import.
Which shows, the module is loaded only once during the first import.
Before moving on to the next section take a look at The Zen of Python, by Tim Peters
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!