- This is a step-by-step demonstration to divide Python code base into clean, efficient modules using Python packages.
- Additionally, you'll learn to import and use your own packages, thus building your own Python program.
-
Create a directory named 'package'.
-
Create an empty file named
__int__.py(this will tell the interpreter that its a package) -
Create a file named 'fn1.py' with a function in it.
def functionOne():
print("This is Function One")
return- Create another file named 'fn2.py' with a different function in it.
def functionTwo():
print("This is Function Two")
return- Create a file named 'test.py' with a set of python code to execute fn1 and fn2 functions.
import fn1, fn2
fn1.functionOne()
fn2.functionTwo()- Open Terminal and log into the folder 'package' and type in the follwoing command to execute test.py file:
$ cd package
$ chmod a+x test.py
$ python3 test.py- The program test.py will get executed, generating the following output:
This is Function One
This is Function TwoNote: The program will run for both Python 2 as well as Python 3 versions.