【发布时间】:2021-08-28 19:13:48
【问题描述】:
感谢之前有人问过这个问题,但我尝试了这些建议无济于事。
a snip of flask not loading, with pip show flask output
VSCode 告诉我:无法从 sourcePylancereportMissingModuleSource 解析导入“flask”)
运行export 向我展示:
FLASK_APP=app.py
FLASK_ENV=development
我可以运行 flask run 并且我的应用程序可以运行,但是当我运行 pytest 时,我会收到:
______________________ ERROR collecting tests/unit/test_app.py ______________________
ImportError while importing test module '/Users/myname/projects/flask-stock-portfolio/tests/unit/test_app.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/unit/test_app.py:4: in <module>
from app import app
E ModuleNotFoundError: No module named 'app'
test_app.py 的内容如下:
"""
This file (test_app.py) contains the unit tests for the app.py file.
"""
from app import app
def test_index_page():
"""
GIVEN a Flask application
WHEN the '/' page is requested (GET)
THEN check the response is valid
"""
with app.test_client() as client:
response = client.get('/')
assert response.status_code == 200
assert b'Flask Stock Portfolio App' in response.data
assert b'Welcome to the Flask Stock Portfolio App!' in response.data
您能做的任何事情来为我指明正确的方向将不胜感激。
【问题讨论】:
-
您问的是 2 个问题:1- pylance 一个是关于 VS Code 设置/配置,2- pytest 一个是关于在正确的虚拟环境中运行 pytest。标题只是关于第一个。两者看起来都与虚拟环境有关,但实际上是两个独立的问题(例如,您可以使 pylance 工作但 pytest 仍然失败)。某处可能有重复。您能否重新集中您的问题,一次选择一个问题?