从测试文件夹导入烧瓶模块失败 [英] flask module import failure from test folder
问题描述
如何导入位于(位于测试文件夹之外的)模块,然后回退应用文件夹结构?
How do I import modules located up (out of a test folder) and then back down my app folder structure?
我想要的app_test.py文件夹中
in the app_test.py folder I want to
from app.models import User
运行
/bin/python app_test.py
我知道
no modules named app.models
我有一个app_test.py文件,在我进行了一些改组之前一直有效,现在我不确定为什么从我的测试文件夹运行测试时为什么我的导入无法正常工作.我将vitrualenv放入我的应用程序主文件夹,这是基本布局减去多余的文件夹/文件:
I have an app_test.py file that worked until i did some shuffling, and now i'm not sure why My imports are not working when running tests from my test folder. I'm vitrualenv'd into my apps main folder and here's the basic layout minus superfluous folders/files:
/application
/(leaving out folders bin build lib local...)
/src
/test
__init__.py
app_test.py
/app
models.py
forms.py
__init__.py
views.py
/static
/templates
/tmp
我的导入内容应该是什么?我注意到,大型教程仅使用test.py文件而不是文件夹.这是最好的方法,如果没有,应该将我的测试文件夹放在哪里?真让我发疯.
What should my import be? I notice that the mega tutorial just uses a test.py file instead of a folder. is that the best way to do it, and if not, where should my test folder be located? It is driving me nuts.
推荐答案
您要么需要从顶部运行app_test.py(即"python test/app_test.py"),要么在app_test中修复python路径.py:
You either need to run your app_test.py from the top, (ie, "python test/app_test.py") or fixup the python path in app_test.py:
import os
import sys
topdir = os.path.join(os.path.dirname(__file__), "..")
sys.path.append(topdir)
这篇关于从测试文件夹导入烧瓶模块失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!