4.4 KiB
title
title |
---|
Python Import Statements |
While learning programming and reading some resources you undoubtedly came across the word abstraction, which simply means to reduce and reuse code as much as possible.
Functions and modules facilitate abstraction. You create functions when you want to do something repeatedly within a file.
Modules come into the picture when you want to reuse a group of functions in different source files. Modules are also useful in structuring the program well.
Using Standard Libraries
Example: You can read about the methods/functions of all the standard libraries in the official Python Docs in detail.
import time
for i in range(100):
time.sleep(1) # Waits for 1 second and then executes the next command
print(str(i) + ' seconds have passed') # prints the number of seconds passed after the program was started
# To calculate the execution time of a part of program
import time
start = time.time() # Returns the number of seconds that have elapsed since the epoch
# code here
end = time.time()
print('Execution time:' , end-start)
# Using math Module
import math
print(math.sqrt(100)) # prints 10
Using third party Modules
Sometimes third party modules do not come bundled with Python, but need to be installed using package managers like pip, easy install, and pipenv.
# To make http requests
import requests
rq = requests.get(target_url)
print(rq.status_code)
Find out more about python-requests module here
Local imports
Files/Modules in the same project can be imported just by their name. import
also supports relative references to files/modules as shown below:
# To import a file within a project from "adjacent module"
import '../adjacent_module/file_to_import'
However, as a good practice, it is recommended not to use relative paths in imports.
To structure programs
We want to make a program that has various functions regarding prime numbers. So lets start. We will define all the functions in prime_functions.py
:
# prime_functions.py
from math import ceil, sqrt
def isPrime(a):
if a == 2:
return True
elif a % 2 == 0:
return False
else:
for i in range(3,ceil(sqrt(a)) + 1,2):
if a % i == 0:
return False
return True
def print_n_primes(a):
i = 0
m = 2
while True:
if isPrime(m) ==True:
print(m)
i += 1
m += 1
if i == a:
break
Now we want to use the functions that we just created in prime_functions.py
, so we create a new file called playground.py
to use those functions.
Please note that this example is far too simple to need two separate files -- it is just to demonstrate. But when there are large, complex programs, splitting the code into different files is really useful.
# playground.py
import prime_functions
print(prime_functions.isPrime(29)) # returns True
Sorting Imports
A good practice is to sort import
modules into three groups - standard library imports, related third-party imports, and local imports. Within each group it is sensible to sort alphabetically by module name. You can find more information in PEP8.
# Order of importing modules
import time # Standard library modules
from package import calculate_duration # Related third-party modules
from package.sibling import example # Local modules within the project
One of the most important things to keep in mind when writing Python is legibility, and alphabetically sorting modules makes them faster to read and search through. Also, it is easier to verify that something is imported, and avoid duplicated imports.