突然无法使用类为pytest导入python文件
我检查了一个关于导入python文件的类似问题,它似乎与主父文件夹中的突然无法使用类为pytest导入python文件,python,oop,import,pytest,importerror,Python,Oop,Import,Pytest,Importerror,我检查了一个关于导入python文件的类似问题,它似乎与主父文件夹中的\u init\upy文件有关,该文件比用于导入类的测试文件夹(包含所有测试文件)高一级,但不确定。也许这与此无关 我正在使用pytest进行测试,并希望将多个测试分组到一个类中,以帮助我测试多个句子的语法分析: 现在,当我从同一文件夹中的另一个文件(test\u pipelines.py)尝试运行import test\u text时,我无法执行此操作。为什么会这样 这里的主要问题是,在test\u管道中,来自test\u
\u init\upy
文件有关,该文件比用于导入类的测试文件夹(包含所有测试文件)高一级,但不确定。也许这与此无关
我正在使用pytest
进行测试,并希望将多个测试分组到一个类中,以帮助我测试多个句子的语法分析:
现在,当我从同一文件夹中的另一个文件(test\u pipelines.py
)尝试运行import test\u text
时,我无法执行此操作。为什么会这样
这里的主要问题是,在test\u管道中,来自test\u text import demission\u句的这条语句。py
甚至没有提取文件test\u text
,更不用说其中的demission\u句了
测试文本的内容.py
UNEMPLOYMENT_SENTENCES = [
"The number of people out of work in Brazil is 2% of the population",
"2% of Brazil is out of work",
"2% of Brazil's citizens are jobless",
"About 9% of Brazil's citizens are out of work or looking for jobs",
"Brazil is in trouble; it has an unemployment rate of 7%",
"Brazil is facing issues - it has an unemployment % of 6%"
]
def test_sample_sentences():
"""
:param sentence_array: array
:return: tests all sentences with 3 tests
"""
for sentence in UNEMPLOYMENT_SENTENCES:
doc = Text(sentence)
test_instance = UnemploymentTestClass(doc)
test_instance.sentence_tokenization()
test_instance.entities()
test_instance.claim_candidates()
class UnemploymentTestClass:
def __init__(self,s):
"""
Initiates a test sentence
"""
self.s=s
....What follows is a list of functions that call self as an argument but don't contain 'test' at the front for pytest. e.g. def sentence_tokenization(self), def entities(self), def claim_candidates(self), using self.s as a parameter to play with.
class ComplexTestClass:
"""
Complex and more nuanced tests for specific sentences
"""
...Note, these are all @staticmethod functions which start with the word 'test_' e.g. def test_multiple_sentences().... so pytest can pick them up.
文件\uuuu init\uuuuu.py
用于构建模块并对其进行分组
如果将test_text.py
放在脚本导入的同一文件夹中,则不会出现任何问题。如果您的test\u text.py
位于子文件夹中,我建议添加\uuuuu init\uuuuuuuuuuuuupy
文件
包结构是这样的:
sound/ Top-level package
__init__.py Initialize the sound package
formats/ Subpackage for file format conversions
__init__.py
wavread.py
wavwrite.py
aiffread.py
aiffwrite.py
auread.py
auwrite.py
...
effects/ Subpackage for sound effects
__init__.py
echo.py
surround.py
reverse.py
...
filters/ Subpackage for filters
__init__.py
equalizer.py
vocoder.py
karaoke.py
...
在\uuuu init\uuuuuuuuuuuupy
中,您可以声明变量,如\uuuuuuuuuuuuuu all=[“echo”、“round”、“reverse”]
,它将从sound.effects import*导入您指定的所有子模块
PS:我已经复制了你的代码,从同一个文件夹导入时没有任何问题
PS2:来源:
当我将prueba.py(您的代码片段)与导入它的脚本放在同一个文件夹中时,这段代码对我有效
from prueba import UnemploymentTestClass
a = UnemploymentTestClass("asd")
print a.s
# print result
# asd
Hi@Manuel导入来自与test\u text.py
相同的文件夹,因此这不是问题所在。我认为问题可能与我的类有关。尝试这样做:从文件导入类
,也可以将这行代码添加到\uuuu init\uuuuuuuuuuuuy.py
并修改\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu理想情况下,在我从test\u pipelines.py
导入test\u text.py
时导入。i、 e.UnemploymentTestClass
,completestclass
和失业
。固定变量是最重要的,但指定特定变量并不重要。这里的主要问题是,这条来自测试文本导入失业句子的语句甚至没有拾取测试文本,更不用说其中的失业句子了。使用您在上面复制的代码片段,这段代码似乎对我有效(请参阅答案中的编辑)你能分享更多关于错误的细节吗?