Import variables from another file as class variables in python3

47 Views Asked by At

I have a set of messages in a file which later I want to import them as class variables to the classes which I create later and these classes are inherited from other classes.

This was working fine with python2

class Linux_logs(logBase):

    from messages import *

    def __init__(self):
        super(linux_logs, self).__init__()
        
    def print_hello(self):
        print(HELLO)

print(Linux_logs.HELLO)

But in python3 I'm unable to do the same. I am facing this error

import * only allowed at module level
1

There are 1 best solutions below

0
Nagasai Madamshetti On

Since the messages.py consists of only variables I tried to keep all the variables in a class Messages() and then used it while creating the class Linux_logs.

from messages import *
class Linux_logs(logBase, Messages):

    def __init__(self):
        super(linux_logs, self).__init__()
    
    def print_hello(self):
        print(HELLO)

print(Linux_logs.HELLO)