python - 用 Python 分离 src 和 test 目录
问题描述
from
当我将测试和 src 文件放在不同的子文件夹中时,我在测试文件的行中遇到编译错误: "Unable to import 'classA' pylint(import-error)
. 当 src 和 test 文件位于同一目录中时,它正在工作。这让我觉得这与 settings.json 文件有关,但我不确定。关于如何解决它的任何想法?
repo
├── .vscode
│ └── settings.json
├── src
│ ├── classA.py
│ └── classB.py
└── tests
├── classA_test.py
└── classB_test.py
类A.py:
from datetime import date
class fetchData():
var = ""
def __init__(self, thing):
self.var = thing
def getInfo(self, x):
..process things..
return info
类A_test.py
import unittest
from classA import fetchData
class classA(unittest.TestCase):
def testStuff(self):
..testStuff..
设置.json
{
"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"*_test.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
}
解决方案
根据这个链接
您可以在测试中执行以下操作:
# import the package
import antigravity
# import the antigravity module
from antigravity import antigravity
# or an object inside the antigravity module
from antigravity.antigravity import my_object
但请确保您__init__.py
在每个目录中添加了文件以使其成为一个包(您可以将它们留空)
希望有效。
推荐阅读
- python - 什么是 python / numpy "~" 运算符
- extjs - 在 Ext.tab.Panel 中添加输入字段
- eclipse - XText - 多种语法和示例网络编辑器
- javascript - 我可以在没有 Webpack 的情况下默认导入共享其父目录名称的文件吗?
- java - 使用 Axis 2 中的 WSDL2Java 时如何生成基础包下的所有类?
- java - Spring Boot @SpringBootTest 给出 NoClassDefFoundError: ...DataAccessException
- javascript - 带有 Firebase ML 套件的 Nativescript Js OCR
- android - 如何在 Angular 中使用 Nativescript 关闭模态窗口?
- keras - ImportError:无法导入名称'_obtain_input_shape'
- android - 颤动的蓝色示例无法连接到 gatt 服务器