Saloni Mude Saloni Mude - 1 year ago 84
Python Question

Attribute Error when importing dictionary from module to another

So I have 4 different classes which I need to combine into one class at the end in a program called "Main" . However whenever I run the Main program, it keeps throwing up an attribute error when it comes to accessing the

dictionary from the
class into the other classes so I can use the generated Employee ID stored in the dictionary. (There are two more classes called
but I've only mentioned 3 for the sake of the brevity and because the error seems to be universal).
(All 3 classes are in different files.)

The error I keep getting:

Traceback (most recent call last):
File "C:/Users/Saloni/Documents/Case Sudy 3/", line 28, in <module>
File "C:/Users/Saloni/Documents/Case Sudy 3\", line 24, in MaintainTimecard
if emp_id in Employee.emp_det:
AttributeError: module 'Employee' has no attribute 'emp_det'

Employee Class:

# ATTRIBUTES: emp_nm, emp_ph, emp_add,emp_id, emp_dob
from random import *
class Employee:
emp_det={} #dictioary to save the details of the employees

def add_emp(self):
lst=[] #to store all inputed details
print("Enter Employee Details:")
emp_nm=input("Name: ")
emp_ph=input("Contact Number: ")
emp_add=input("Address: ")
emp_dob=input("Date of Birth:")
lst.extend([emp_nm,emp_ph,emp_add,emp_dob]) #store the details
while emp_id in Employee.emp_det: # avoid repetition
Employee.emp_det[emp_id]=lst # make dictionary key and store in list
print("Your Employee ID is",emp_id)

def del_emp(self):
t=0 # to count number of invalid inputs
while t<=3:
emp_id=input("Employee ID:")
if emp_id in Employee.emp_det:
del Employee.emp_det[emp_id]
t=4 # to get the program out of the loop
print("Invalid ID. Try Again.")

def edit_emp(self):
t=0 # counting invalid inputs
while t<=3:
emp_id=input("Employee ID:")
if emp_id in Employee.emp_det: # checking validity
print("\n Edit: \n 1.Contact \n 2.Address \n")
if ch==1:
Employee.emp_det[emp_id][1]=input("New Contact Number:")
elif ch==2:
Employee.emp_det[emp_id][2]=input("New Address:")
print("Invalid Option")
t=4 #t o get the program out of the loop
print("Invalid ID. Try Again.")

def Edisplay(self):
print("The Employees are:")
print(" ID \t Name \t Contact \t Address \t Date of Birth")
for i in Employee.emp_det: # access to each dictionary element
print(i,"\t",end=" ")
for j in Employee.emp_det[i]: # access every value under the key
print(j,"\t",end=" ")

Monthly-Paid Class

import Employee
import Timecard

class Monthly_Paid:
fixSalary = 40000
def AcceptTimeCard (self):
print ("Timecard details are:")
for i in Timecard.emp_tcinfo:
print(i, "\t", end ="")
for j in Timecard.emp_tcinfo[i]:
print(j,"\t",end=" ")

def Gen_Paycheck (self):
emp_id = input("please enter employee ID")
if emp_id in Employee.emp_det:
print ("Total Salary of " + emp_id + " is :" + fixSalary)

def MaintainTimecard (self):
emp_id = input("Please enter your employee ID")
if emp_id in Employee.emp_det:
print("\n 1.Edit Start Time Hour "
"\n 2.Edit Start Time Minute "
"\n 3. Edit End Time Hour "
"\n 4.Edit End Time Minute")
ch = int(input("Input option"))
if ch == 1:
Timecard.emp_tcinfo[emp_id][1] = input(
"Input new Start Time Hour")
if ch ==2:
Timecard.emp_tcinfo[emp_id][2] = input(
"Input new Start Time Minute")
if ch == 3:
Timecard.emp_tcinfo[emp_id][3] = input(
"Input new End Time Hour")
if ch == 4:
Timecard.emp_tcinfo[emp_id][4] = input(
"Input new End Time Minute")
print("Invalid input")

Main script

print ("Welcome to Employee Time Card System")

import Employee

e= Employee.Employee()

print("What kind of employee are you?")
print ("\n 1.Monthly Paid \n 2.Weekly Paid")

ch= int(input("Enter Choice"))

if ch ==1:
import Monthly_Paid
import Timecard
s = Monthly_Paid.Monthly_Paid()
w = Timecard.Timecard()
print("Monthly Paid")
t1= "y"
while t1=="y" or t1=="Y":
print ("\n 1.See Time Card \n2.Edit TimeCard \n 3.See Paycheck")
ch1 = int(input("Enter Choice"))
if ch1 == 1:
if ch1 == 2:
if ch1 == 3:
print("Invalid Choice")
t1 = input("Continue with Monthly Paid? Y/N")
elif ch == 2:
import Weekly_Paid
a= Weekly_Paid.Weekly_Paid()
t2= "y"
print ("Weekly Paid")
while t2=="y" or t2=="Y":
print ("\n 1.See Time Card \n2.Edit TimeCard \n 3.See Paycheck")
ch1 = int(input("Enter Choice"))
if ch1 == 1:
if ch1 == 2:
if ch1 == 3:
print("Invalid Choice")
t2 = input("Continue with Weekly Paid? Y/N")
print("Invalid choice")

Answer Source

import Employee will look for a module If there is no file called then you won't be able to make that import work.

So in the Monthly-Paid Class file you have to do something like:

from import Employee

The problem then arises from the fact that there's a module called Employee but that contains a class called Employee. Importing the module Employee doesn't give you access to the class automatically. To access the attribute of the Employee class called emp_det you have to specify the class. So if you imported using

from Employee import Employee

To access you need:


Alternatively if you imported with:

import Employee

then to access you need:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download