- 此问题在此处已有答案**:
Running unittest with typical test directory structure(24个答案)
9小时前关门了。
我在python包中遇到了一个奇怪的问题,我有一个名为manchinetranslator的python包,我在其中创建了一个单元测试的子文件夹,如下所示。
manchinetranslator/translator.py
manchinetranslator/__init__.py
manchinetranslator/tests/tests.py
在www.example.com中,两个函数定义如下:translator.py, two functions are defined as:
def english_to_french(text):
...
def french_to_english(text):
...
- 初始化**. py为:
from . import translator
www.example.com包含www.example.com中两个函数的单元测试,如下所示: tests.py contains unit tests of the two functions in translator.py and is as follows:
import unittest
from translator import english_to_french, french_to_english
class Test_english_to_french(unittest.TestCase):
...
class Test_french_to_english(unittest.TestCase):
...
但在运行www.example.com时,它会给出如下错误:tests.py, it gives an error as follows:
Traceback (most recent call last):
File "C:\Python\manchinetranslator\tests\tests.py", line 3, in <module>
from manchinetranslator.translator import english_to_french, french_to_english
ModuleNotFoundError: No module named 'manchinetranslator'
但是,当www.example.com与www.example.com放在同一个文件夹中时,它工作正常。我猜它可能需要设置$PYTHONPATH,但我不确定如何设置。因此,我需要帮助来解决这个问题。 tests.py is put in the same folder as translator.py , it works fine. I guess it may need to set $PYTHONPATH, but I am not sure how to do it. So I need help with this problem.
1条答案
按热度按时间xzv2uavs1#
您正尝试从父目录导入,但这不起作用。
tests.py
无法从translator
导入,因为它位于父目录中。请参见此SO答案,了解如何操作:https://stackoverflow.com/a/24266885/30581