问题
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:
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.
回答1:
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.
回答2:
from text_preprocessing import TextToTensor.clean_text as clean_test
maybe this will solve your query
回答3:
Try importing:
from text_preprocessing import TextToTensor
Afterwards, try doing the following:
TextToTensor.clean_text(...)
回答4:
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).
回答5:
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?
来源:https://stackoverflow.com/questions/64243888/cannot-import-a-method-from-a-module