TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial…

Follow publication

PYTHON BASICS

Understanding Python imports, __init__.py and pythonpath — once and for all

Dr. Varshita Sher
TDS Archive
Published in
12 min readOct 7, 2021
Directory structure for learning Python imports

Let’s begin...

#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo(x: int):
print("You only LIve", x, "times.")
#example2.pyimport example1# imported string
print("The imported string is: ", example1.MY_EX1_STRING)
# imported function
example1.yolo(10)
Output from running example2.py
#example2.pyimport example1 as e1# imported string
print("The imported string is: ", e1.MY_EX1_STRING)
# imported function
e1.yolo(10)

What exactly happens when we write an ‘import’ statement?

#example2.py# import example1
# print("The imported string is: ", example1.MY_EX1_STRING)
# example1.yolo(10)
import sys
print(sys.path)
Output from sys.path

What if I only want to import certain, but not all, items from the imported module?

#example1.pyprint("Thanks for importing Example1 module.")MY_EX1_STRING = 'Welcome to Example1 module!'def yolo(x: int):
print("You only LIve", x, "times.")
yolo(10000)
#example1.pyprint("Thanks for importing Example1 module.")MY_EX1_STRING = 'Welcome to Example1 module!'def yolo(x: int):
print("You only LIve", x, "times.")
if __name__ == '__main__':
yolo(10000)
Output from running example1.py
#example2.pyfrom example1 import yolo
yolo(10)

What’s the need for PYTHONPATH?

#utils/length.pydef get_length(name: str):
return len(name)
#utils/lower.pydef to_lower(name: str):
return name.lower()
#utils/upper.pydef to_upper(name: str):
return name.upper()
#example3_outer.pyimport utils.lengthres = utils.length.get_length("Hello")
print("The length of the string is: ",res)
#example3_outer.pyimport os
import sys
fpath = os.path.join(os.path.dirname(__file__), 'utils')
sys.path.append(fpath)
print(sys.path)
import length
txt = "Hello"
res_len = length.get_length(txt)
print("The length of the string is: ",res_len)
#example3_outer.py#import os
#import sys
#fpath = os.path.join(os.path.dirname(__file__), 'utils')
#sys.path.append(fpath)
#print(sys.path)
import length
txt = "Hello"
res_len = length.get_length(txt)
print("The length of the string is: ",res_len)
#example3_outer.pyimport os
import sys
fpath = os.path.join(os.path.dirname(__file__), 'utils')
sys.path.append(fpath)
import length
import upper
import lower
txt = "Hello"res_len = length.get_length(txt)
print("The length of the string is: ",res_len)
res_up = upper.to_upper(txt)
print("Uppercase txt: ", res_up)
res_low = lower.to_lower(txt)
print("Uppercase txt: ", res_low)

When do we need __init__.py?

#example3_outer.pyimport utils
#example3_outer.pyimport utilstxt = "Hello"
res = utils.length.get_length(txt)
# utils/__init__.py (incorrect way of importing)from length import get_length
from lower import to_lower
from upper import to_upper
import utilstxt = "Hello"
res_low = utils.to_lower(txt)
print(res_low)
# utils/__init__.pyfrom .lower import to_lower
from .upper import to_upper
from .length import get_length
# utils/__init__.pyfrom utils.lower import to_lower
from utils.upper import to_upper
from utils.length import get_length
#example3_outer.pyimport utilstxt = "Hello"
res_len = utils.get_length(txt)
print(res_len)
res_up = utils.to_upper(txt)
print(res_up)
res_low = utils.to_lower(txt)
print(res_low)
# scripts/example3.pyimport os
import sys
PROJECT_ROOT = os.path.abspath(os.path.join(
os.path.dirname(__file__),
os.pardir)
)
sys.path.append(PROJECT_ROOT)

import utils
print(utils.get_length("Hello"))
************** OUTPUT *********
5
# utils/__init__.pyfrom utils.lower import to_lower
from utils.upper import to_upper
from utils.length import get_length
from scripts.example1 import yolo
# scripts/example3.pyimport os
import sys
PROJECT_ROOT = os.path.abspath(os.path.join(
os.path.dirname(__file__),
os.pardir)
)
sys.path.append(PROJECT_ROOT)
import utils
print(utils.get_length("Hello"))
utils.yolo(2)
************** OUTPUT *********
5
You only LIve 2 times.

Conclusion

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

TDS Archive
TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Dr. Varshita Sher
Dr. Varshita Sher

FTSE 100 Tech Leader 🚀 | Data Science & Generative AI | Oxford Alumni | 2x Top writer on Medium | Editor of Trusted Data Science @ Haleon | Synapgen.ai

Responses (28)

Write a response