1

I have a script that I am currently working on, named exp1.py and it's located in

/project/exp1.py 

In this script, I am trying to call a function named computelikelihood(), which is inside the class Class(), which is in script method.py, in a different directory:

 /project/methods/c_CLASS/method.py

So, in my code in exp1.py, I do this:

import sys

sys.path.append('/project/methods/c_CLASS/')

Which gets me to the folder where method.py is located, but when I want to call the Class() from the method.py, so that I get the function computelikelihood(), that I actually want, I get error. I try this:

from method import Class
from Class import computelikelihood

But I get ImportError: No module named Class. Can anyone help?

EDIT This is how the __init__ of my Class looks like:

class Class:
    def __init__(self,e2wl,w2el,label_set):
        self.e2wl = e2wl
        self.w2el = w2el
        self.workers = self.w2el.keys()
        self.examples = self.e2wl.keys()
        self.label_set = label_set
6
  • just from method.Class import computelikelihood Commented Mar 18, 2020 at 19:53
  • 2
    You can't "import functions from classes." If computelikelihood is a static/class method, you can call it from Class, e.g. Class.compputelikelihood(...). If it is a method, you'll need to instantiate a Class first. Commented Mar 18, 2020 at 19:55
  • @Brian Not sure what you mean. Can you be more specific? computelikelihood is just a function , but I don't understand what you mean by "static/class method" or "method". Or also by instantiating the Class first. Or how to use Class.compputelikelihood() in my exp1.py script. Commented Mar 18, 2020 at 20:08
  • @joasa It seems like you're not familiar with how classes work in Python. You may find the official tutorial on classes helpful. If computelikelihood were just a function, you wouldn't need to reference Class at all. Commented Mar 18, 2020 at 20:13
  • @joasa don't take this the wrong way, but if you don't know what those things mean then why are you using classes to begin with? Those are pretty basic concepts for understanding what a class is. Commented Mar 18, 2020 at 20:32

1 Answer 1

1

Since you are trying to use a method from a Class, you should do so via the class. Do not import the function alone as it isn't intended to be used as such:

from method import Class

Class.computelikelihood()

However, this only works if computelikelihood is a static/class method:

class Class:

    @classmethod
    def computelikelihood(cls):
        ...

    # or

    @staticmethod
    def computelikelihood():
        ...

If it's an instance method:

class Class:
    def computelikelihood(self):
        ...

You'll need to first instantiate an object of class Class:

from method import Class

classObject = Class()
classObject.computelikelihood()
Sign up to request clarification or add additional context in comments.

10 Comments

Your post makes the distinction between functions, methods, static methods, and class methods rather ambiguous, given that you include none of the syntax that makes these distinction. I've suggested an edit to help clarify this.
Thanks that solves the error that I had! However, I edited my question so that you see what arguments my Class takes, because now I have this error classObject = GLAD() TypeError: __init__() takes exactly 4 arguments (1 given). How can I define these arguments in my exp1.py file when i call the Class?
def computelikelihood(cls): isn't a static/class method. It is still an instance method as you've defined it.
@joasa that's a totally different question. In any case, you need to provide whatever parameters that class is expecting. You've authored this class, presumably, so you would know.
@juanpa.arrivillaga i don't think it's a totally different question, as my question is how to call a class from a script in a different directory and get the function i want. so i'm trying to call the class by using the same arguments as in __init__, but i need to define them, so I'm not sure how to do that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.