1

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
  • "Module level" just means in a part of the script that's not in a class or function. Any names you define there go directly into the module namespace. see https://stackoverflow.com/questions/63597899/unique-import-only-allowed-at-module-level – pippo1980 Oct 03 '22 at 10:48
  • Duplicate: [Import all functions and classes inside a module into a class](https://stackoverflow.com/q/58584171/984421). – ekhumoro Oct 03 '22 at 12:43

1 Answers1

0

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)