Python Packages for Beginners

Python Packages are nothing but a namespace or a hierarchical file directory structure that may contain multiple python script,modules or sub-packages or Packages. They are nothing but a simple directories, but it contains a file called __init__.py .This __init__.py indicate that the folder or directory is the Python Packages.you can keep __init__.py file empty or may insert some code.

Python Packages

To Create Python Packages fallow the below steps:

  1. Create “Hello” directory/folder in Desktop [you can name it whatever you want ,create it in any folder/directory]
  2. Python Packages
  3. Now create 3 python file as shown in below images named as example.py , example2.py , __init__.py
  4. example.py contain a simple class based example
  5. Python Packages
  6. example2.py contains a simple function
  7. Python Packages
  8. __init__.py contain import statement,that makes code available that define in example.py and example2.py
  9. Python Packages
  10. Now create example1.py script outsite the “hello” directory [i.e in Desktop] and write code as given in image
  11. Python Packages
  12. We have created a package called “hello” and used it in example1.py script,now run the code in terminal as given in image,you will get the fallowing output .
  13. Python Packages

For more details you can visit python official website https://docs.python.org/2/tutorial/modules.html#packages

Hope guys you will understand the concept of Python Packages clearly.Try to implement it with more code,and if you find any difficulty, feel free to drop a comment on this post.

2 Comments

  1. Kamal Thakur February 4, 2016
  2. sanad February 12, 2016