Computer >> Computer tutorials >  >> Programming >> Python

Create and Access a Python Package


In this article, we are going to learn about the packages in Python. Packages help us to structure packages and modules in an organized hierarchy. Let's see how to create packages in Python.

Creating Packages

We have included a __init__.py, file inside a directory to tell Python that the current directory is a package. Whenever you want to create a package, then you have to include __init__.py file in the directory. You can write code inside or leave it as blank as your wish. It doesn't bothers Python.

Follow the below steps to create a package in Python

  • Create a directory and include a __init__.py file in it to tell Python that the current directory is a package.
  • Include other sub-packages or files you want.
  • Next, access them with the valid import statements.

Let's create a simple package that has the following structure.

Package (university)

  • __init__.py
  • student.py
  • faculty.py

Go to any directory in your laptop or desktop and create the above folder structure. After creating the above folder structure include the following code in respective files.

Example

# student.py
class Student:

   def __init__(self, student):
      self.name = student['name']
      self.gender = student['gender']
      self.year = student['year']

   def get_student_details(self):
      return f"Name: {self.name}\nGender: {self.gender}\nYear: {self.year}"


# faculty.py
class Faculty:

   def __init__(self, faculty):
      self.name = faculty['name']
      self.subject = faculty['subject']

   def get_faculty_details(self):
      return f"Name: {self.name}\nSubject: {self.subject}"

We have the above in the student.py and faculty.py files. Let's create another file to access those classed inside it. Now, inside the package directory create a file named testing.py and include the following code.

Example

# testing.py
# importing the Student and Faculty classes from respective files
from student import Student
from faculty import Faculty

# creating dicts for student and faculty
student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'}
faculty_dict = {'name': 'Emma', 'subject': 'Programming'}

# creating instances of the Student and Faculty classes
student = Student(student_dict)
faculty = Faculty(faculty_dict)

# getting and printing the student and faculty details
print(student.get_student_details())
print()
print(faculty.get_faculty_details())

If you run the testing.py file, then you will get the following result.

Output

Name: John
Gender: Male
Year: 3

Name: Emma
Subject: Programming

We have seen how to create and to access a package in Python. And this is a simple package. There might be plenty of sub-packages and files inside a package. Let's see how to access subpackage modules.

Create a directory with the following structure

  • Package (university)
    • __init__.py
    • Subpackage (student)
      • __init__.py
      • main.py
      • ...
    • testing.py

Copy the above student code and place it here. Now, let's see how to access it in the testing.py file. Add the following in the testing.py file.

Example

# testing.py
from student.main import Student

# creating dicts for student
student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'}

# creating instances of the Student class
student = Student(student_dict)

# getting and printing the student details
print(student.get_student_details())

If you run the testing.py file, then you will get the following result.

Output

Name: John
Gender: Male
Year: 3

We have accessed the Student class from the main.py file inside the subpackage student using a dot (.). You can go to as much deeper as you want based on the package structure.

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.