问题描述
我正在为一个关于从另一个子目录导入类的虚拟问题而苦苦挣扎,我找不到令人满意的解决方案.这是上下文:我有一个根文件夹,我们称之为项目,其中包含两个子文件夹,分别称为 app 和 test.
在应用程序中,我有我的类文件,class1.py 等等.在测试中,我有 test_class1.py 来保存我的单元测试类.对我来说似乎是一些标准的文件夹结构.如何从 test_class1.py 导入 class1?到目前为止,我将../app"附加到我的 sys.path 中,但对我来说它看起来很丑!我尝试了 from ..app import class1
和许多其他组合都没有成功.
第二个问题:python 3.6+ 还需要 __init__.py
吗?感谢您的帮助
1/关于你的第一个问题,根据我自己的经验,我的项目结构如下:
project/应用程序/类1.py__init__.py...测试/上下文.pytest_class1.py__init__.py...设置文件...
显然,这些测试模块必须导入你打包的模块才能测试它.您可以通过以下几种方式执行此操作:
- 期望将包安装在站点包中.
- 使用简单(但显式)的路径修改来正确解析包.
我强烈推荐后者.要求开发人员运行 setup.py开发以测试积极变化的代码库也需要他们为代码库的每个实例设置一个独立的环境.
要给单个测试导入上下文,请创建一个 tests/context.py文件:
context.py* 应该引导测试上下文如下:
导入系统导入操作系统sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))导入应用从应用导入 class1...
然后在您的 test_class1.py 文件中,我将按如下方式导入测试类:
from unittest import TestCasefrom .context import class1 # 或 from context.class1 import MyClass类 TestClass1(TestCase):定义设置(自我):self.to_be_tested = class1.MyClass() # 或 MyClass()
2/关于您关于在 python 3.6+ 中是否需要 __init__.py 文件的第二个问题,我让您阅读以下现有答案:Python 3 中的包不需要 __init__.py 吗?
可能的有趣参考:
- https://docs.python-guide.org/writing/structure/#structure-of-the-repository
- https://docs.python-guide.org/writing/结构/#test-suite
- 使用典型的测试目录结构运行单元测试
- Python 的最佳项目结构是什么申请?
- https://packaging.python.org/
I am struggling with a dummy question about importing classes from another sub-directory and I can’t find satisfying solution. Here is the context: I have a root folder, let’s call it project, that contains two sub folders, called app and test.
In app, I have my classes files, class1.py and so on. In test, I have test_class1.py to hold my unit test class. Seems to be some standard folder structure to me. How do I import class1 from test_class1.py? So far I append ‘../app’ to my sys.path but it looks so ugly to me! I tried from ..app import class1
and so many other combinations unsuccessfully.
Second question: Are __init__.py
still needed in python 3.6+?Thanks for your help
1/ Regarding your first question, based on my own experience, I would structure the project as follow:
project/
app/
class1.py
__init__.py
...
test/
context.py
test_class1.py
__init__.py
...
setup.py
...
context.py* should bootstrap the testing context as follow:
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import app
from app import class1
...
Then in your test_class1.py file, I would import the tested Class as follow:
from unittest import TestCase
from .context import class1 # or from context.class1 import MyClass
class TestClass1(TestCase):
def setUp(self):
self.to_be_tested = class1.MyClass() # or MyClass()
2/ Regarding your second question about __init__.py file being needed or not in python 3.6+, I let you read the following existing answer:Is __init__.py not required for packages in Python 3?
Possible Interesting references:
- https://docs.python-guide.org/writing/structure/#structure-of-the-repository
- https://docs.python-guide.org/writing/structure/#test-suite
- Running unittest with typical test directory structure
- What is the best project structure for a Python application?
- https://packaging.python.org/
这篇关于如何以优雅的方式导入我自己的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!