Running imported class methods in other files

This is from the tutorial about Abstract Classes Python Abstract Class

In this tutorial they state: " To structure the program, we’ll use modules, where each class is placed in a separate module (or file)"

I created 5 files called employee.py, payroll.py fulltimeemployee.py etc and called them in app.py

All files in same folder. Here is the code of the app.py

from employee import Employee
from fulltimeemployee import FulltimeEmployee
from hourlyemployee import HourlyEmployee
from payroll import Payroll

payroll = Payroll()

payroll.add(FulltimeEmployee('John', 'Doe', 6000))
payroll.add(FulltimeEmployee('Jane', 'Doe', 6500))
payroll.add(HourlyEmployee('Jenifer', 'Smith', 200, 50))
payroll.add(HourlyEmployee('David', 'Wilson', 150, 100))
payroll.add(HourlyEmployee('Kevin', 'Miller', 100, 150))

Here is the abstract class in employee.py

from abc import ABC, abstractmethod


class Employee(ABC):
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    @property
    def full_name(self):
        return f"{self.first_name} {self.last_name}"

    @abstractmethod
    def get_salary(self):
        pass

Here is what I get in the shell when I run app.py:

= RESTART: /Volumes/HOMEX/brianx/Training n Studies/Brilliant courses/Python/pythontutorial.net/Test/app.py
Traceback (most recent call last):
File “/Volumes/HOMEX/brianx/Training n Studies/Brilliant courses/Python/pythontutorial.net/Test/app.py”, line 2, in
from fulltimeemployee import FulltimeEmployee
File “/Volumes/HOMEX/brianx/Training n Studies/Brilliant courses/Python/pythontutorial.net/Test/fulltimeemployee.py”, line 1, in
class FulltimeEmployee(Employee):
NameError: name ‘Employee’ is not defined

So it seems to me that python is seeing the files, but not importing them and executing the code.

This is my first run at importing modules in external files. Not sure where I screwed up. (but pretty sure the error is me!)

Any help, appreciated.

Cheers

Every module needs to import the modules it wants to use. In your example, it looks like your fulltimeemployee module relies on the Employee class from the employee module. If that is the case, then fulltimeemployee.py needs to import employee (e.g., with from employee import Employee). It doesn’t matter that you imported employee in app.py; since fulltimeemployee uses Employee, it needs its own import.

As an aside, the idea of “one class per file” is not really a convention in Python and can be cumbersome. When learning, it can be useful to split things into modules to understand how to do imports and stuff, but there isn’t any particular reason to put every class in its own module. Normally, you would just split one module into multiple ones when it becomes too big and unwieldy.

3 Likes

Thanks Brendan, that worked. I see the connections now.

As for multiple modules being in a single file, that would certainly make it easier to call. I was curious as to how Python would import code written on other files…turns out to be … seamlessly!

The more time I spend working through these tutorials, the more intuitive it is becoming…with a bit of expert guidance along the way.

I had some coding experience 30+ years ago in Pascal, Fortran, Basic and C+ but it has been decades since. Now that I am retired, I am diving into Python, as I have some app ideas I want to bring to fruition.

Python is remarkable in it’s flexibility, and almost natural language syntax. I am loving it!

Cheers