我。前言:应用目录结构和模块在文末列出。
二。问题陈述:如果未设置 PYTHONPATH,应用程序会运行,但单元测试失败并显示 ImportError: No module named models.transactions。尝试导入时会发生这种情况
app.py 中的事务。如果 PYTHONPATH 设置为 /sandbox/app
应用程序和
unittest 运行没有错误。解决方案的约束是不必设置 PYTHONPATH,并且
不应以编程方式修改 sys.path。
三。详细信息:考虑设置 PYTHONPATH 并且 test_app.py
作为包 /sandbox$ python -m unittest tests.test_app
运行的情况。查看 __main__
的打印语句
散布在整个代码中:
models : app.models.transactions
models : models.transactions
resources: app.resources.transactions
app : app.app
test : tests.test_app
单元测试首先导入应用程序,因此有 app.models.transactions
。接下来导入那个app
attempts 是 resources.transactions
。导入时,它会自己导入 models.transactions
,并且
然后我们看到 app.resources.transactions
的 __name__
。接下来是 app.app
导入,最后是单元测试模块 tests.test.app
。设置 PYTHONPATH 允许应用程序解析 models.transactions!
一种解决方案是将 models.transactions
放在 resources.transaction
中。但是还有另一种方法来处理这个问题吗?
为了完整起见,当应用程序运行时,__main__
的打印语句是:
models : models.transactions
resources: resources.transactions
app : __main__
这是预期的,并且没有尝试在 /sandbox/app
之上或横向导入。
四。附录
A.1 目录结构:
|-- sandbox
|-- app
|-- models
|-- __init__.py
|-- transactions.py
|-- resources
|-- __init__.py
|-- transactions.py
|-- __init__.py
|-- app.py
|-- tests
|-- __init__.py
|-- test_app.py
A.2 模块:
(1) 应用:
from flask import Flask
from models.transactions import TransactionsModel
from resources.transactions import Transactions
print ' app : ', __name__
def create_app():
app = Flask(__name__)
return app
app = create_app()
if __name__ == '__main__':
app.run(host='127.0.0.1', port=5000, debug=True)
(2) 模型.交易
print ' model : ', __name__
class TransactionsModel:
pass
(3) 资源.事务:
from models.transactions import TransactionsModel
print ' resources: ', __name__
class Transactions:
pass
(4) 测试.test_app
import unittest
from app.app import create_app
from app.resources.transactions import Transactions
print ' test : ', __name__
class DonationTestCase(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_transactions_get_with_none_ids(self):
self.assertEqual(0, 0)
if __name__ == '__main__':
unittest.main()
最佳答案
值得一提的是,Flask 文档说将应用程序作为一个包运行,并设置环境变量:FLASK_APP
.然后应用程序从项目根目录运行:$ python -m flask run
.现在导入将包括应用程序根目录,例如 app.models.transactions
.由于 unittest 以相同的方式运行,作为项目根目录中的包,所有导入也在那里解析。
问题的症结可以用下面的方式来描述。 test_app.py 需要访问横向导入,但如果它作为脚本运行,例如:
/sandbox/test$ python test_app.py
它有<code>__name__</code>==<code>__main__</code>
.这意味着导入,例如 from models.transactions import TransactionsModel
, 将不会被解决,因为它们在横向上并且在层次结构中不较低。要解决此问题,可以将 test_app.py 作为一个包运行:
/sandbox$ python unittest -m test.test_app
-m
switch 告诉 Python 这样做。现在包可以访问 app.model
因为它在 /sandbox
中运行. test_app.py
中的进口必须反射(reflect)这种变化并变成类似这样的东西:
from app.models.transactions import TransactionsModel
要使测试运行,应用程序中的导入现在必须是相对的。例如,在 app.resources 中:
from ..models.transactions import TransactionsModel
所以测试成功运行,但如果应用程序运行失败!这就是问题的症结所在。当应用程序作为脚本从 /sandbox/app$ python app.py
运行时它达到了这个相对进口 ..models.transactions
并返回程序试图导入顶层以上的错误。修复一个,破坏另一个。
如何在不设置 PYTHONPATH 的情况下解决这个问题?一个可能的解决方案是在包中使用条件 <code>__init__</code>.py
做有条件的进口。 resources
的示例包是:
if __name__ == 'resources':
from models.transactions import TransactionsModel
from controllers.transactions import get_transactions
elif __name__ == 'app.resources':
from ..models.transactions import TransactionsModel
from ..controllers.transactions import get_transactions
要克服的最后一个障碍是我们如何将其拉入 resources.py
.在 <code>__init__</code>.py
中完成的进口绑定(bind)到该文件并且不可用于 resources.py
.通常一个人会在 resources.py
中包含以下导入:
import resources
但是,再一次,是resources
吗?或 app.resources
?对我们来说,困难似乎已经越走越远了。 importlib
提供的工具可以在这里提供帮助,例如以下内容将进行正确的导入:
from importlib import import_module
import_module(__name__)
还有其他方法可以使用。例如,
TransactionsModel = getattr(import_module(__name__), 'TransactionsModel')
这修复了当前上下文中的错误。
另一个更直接的解决方案是在模块本身中使用绝对导入。例如,在资源中:
models_root = os.path.join(os.path.dirname(__file__), '..', 'models')
fp, file_path, desc = imp.find_module(module_name, [models_root])
TransactionsModel = imp.load_module(module_name, fp, file_path,
desc).TransactionsModel
TransactionType = imp.load_module(module_name, fp, file_path,
desc).TransactionType
关于使用 sys.path.append(app_root)
更改 PYTHONPATH 的注意事项
在 resources.py
.这很好用,只需几行代码就可以了。此外,它只更改执行文件的路径并在完成后恢复。似乎是单元测试的一个很好的用例。一个问题可能是何时将应用程序移动到不同的环境。
关于Python 运行 Unittest 作为包导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47935707/