How to import a method or use that from another class in python?

Question:

I have two classes in the same folder;
filename

  • Filename: twisted, class name : Root,method :def render_GET(self, request):
  • Filename: ladpConnector, class name:MyClass, method : getMachines(self)

I want to call getMachines from the first file, inside Root class.

I tried following options;

  • MyClass().getMachines()

from ldapConnector import Myclass

MyClass().getMachines()

All gives issues, undefined method/undefined variable class Myclass etc..

What is teh right way to call that method?

Asked By: Ratha

||

Answers:

I’m not sure what you are asking, but you need to import that class in the file that has the Root class:

# twisted.py file
from ldapConnector import MyClass

class Root():
    def __init__(self):
        MyClass().getConnections()
Answered By: reptilicus

To access files within the same module you need to do relative imports: from .ldapConnector import MyClass should work.

Answered By: Gerges
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.