0

I'm trying to import a method from a python module but getting it's throwing an error mentioned below.

cannot import name 'methodname' from 'modulename'

my directory structure:

enter image description here

there are solutions to this problem but those are already satisfied in my case. Below is how I tried to import the method from the module.

from text_preprocessing import TextToTensor, clean_text

here TextToTensor is aclass name and clean_text is a method in TextToTensor class.

of Course, I can create an instance of TextToTensor and use to call the clean_text method, but I'm desperate to import the function.

thanks in advance.

Venkatesh Dharavath
  • 500
  • 1
  • 5
  • 18
  • 1
    Sound like `clean_text` is a candidate for a "class method" (use decorator `@classmethod`)? Then importing `TextToTensor` is enough: You can use `clean_text` then via `TextToTensor.clean_text`, without instantiating. – Timus Oct 07 '20 at 12:58

5 Answers5

1
from text_preprocessing import TextToTensor.clean_text as clean_test

maybe this will solve your query

1

Try importing:

from text_preprocessing import TextToTensor

Afterwards, try doing the following:

TextToTensor.clean_text(...)

lime
  • 801
  • 8
  • 21
  • yes, we can do that, even I mentioned the same method in my question. but I'm desperate to import "from text_preprocessing import clean_text". I got the answer from Timus's solution – Venkatesh Dharavath Oct 08 '20 at 11:08
0

This thread may have the answer to your problem.

Your python code can't import the module because you've not properly set your import function (or folder structure depending on your preference).

QLR
  • 1
  • 3
0

The issue here is Python's inability to import dotted names:

(base) fathead:tmp sholden$ cat module.py
class One:
    def meth(self):
        return 42

(base) fathead:tmp sholden$ python
Python 3.6.10 |Anaconda, Inc.| (default, Jan  7 2020, 15:01:53)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from module import One.meth
  File "<stdin>", line 1
    from module import One.meth
                          ^
SyntaxError: invalid syntax

The correct way to proceed is to import the class. You can then establish a local name for it if you want. In my simple example this would look like

from module import One

my_func = One.meth

Note, however, that my_func_ is a function (i.e. it is not associated with any instance), but its first argument will be self unless it's defined as a classmethod or staticmethod, so you will need to provide a One instance as the first argument in calling it.

Are you sure you need this function?

holdenweb
  • 33,305
  • 7
  • 57
  • 77
0

I think you have the following alternatives:

File test.py:

class MyClass:

    @classmethod
    def my_method(cls):
        return "class method"

    @staticmethod
    def my_other_method():
        return "static method"

    def yet_another_method(self):
        return yet_another_method()

# Class definition ends here. You can now define another function that
# can be imported like you want to do it, and, if needed, used to define
# a class method of the same name.

def yet_another_method():
    return "works too"

And then in file main.py:

from test import MyClass, yet_another_method

print(MyClass.my_method())
function = MyClass.my_method
print(function())

print(MyClass.my_other_method())
function = MyClass.my_other_method
print(function())

print(yet_another_method())

Alternative one/two produces a class method/static method that doesn't need an instance of MyClass to work. This is only viable if the definition of the functions doesn't involve self.xyz (in fact, such defintions would produce an error).

The third alternative is simple: Define the function inside test.py and then use it in the definition of the corresponding method of MyClass. Since you said "you're desparate to import the function" that might be the way to go.

Timus
  • 10,974
  • 5
  • 14
  • 28