Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tutorial] Pytest don't find flaskr package #2908

Closed
lvlsun opened this issue Sep 15, 2018 · 4 comments
Closed

[Tutorial] Pytest don't find flaskr package #2908

lvlsun opened this issue Sep 15, 2018 · 4 comments

Comments

@lvlsun
Copy link

lvlsun commented Sep 15, 2018

Expected Behavior

I follow the tutorial on http://flask.pocoo.org/docs/1.0/tutorial/ and try to run tests

(venv) ...\flaskproj>pytest

Actual Behavior

  File "...\flaskproj\venv\lib\site-packages\_pytest\config\__init__.py", line 381, in _getconftestmodules
    return self._path2confmods[path]
KeyError: local('...\\flaskproj\\tests')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "...\flaskproj\venv\lib\site-packages\_pytest\config\__init__.py", line 412, in _importconftest
    return self._conftestpath2mod[conftestpath]
KeyError: local('...\\flaskproj\\tests\\conftest.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "...\flaskproj\venv\lib\site-packages\_pytest\config\__init__.py", line 418, in _importconftest
    mod = conftestpath.pyimport()
  File "...\flaskproj\venv\lib\site-packages\py\_path\local.py", line 668, in pyimport
    __import__(modname)
  File "...\flaskproj\venv\lib\site-packages\_pytest\assertion\rewrite.py", line 296, in load_module
    six.exec_(co, mod.__dict__)
  File "...\flaskproj\tests\conftest.py", line 5, in <module>
    from flaskr import create_app
ModuleNotFoundError: No module named 'flaskr'
ERROR: could not load ...\flaskproj\tests\conftest.py

Environment

  • Python version: 3.6.5
  • Flask version: 1.0.2
  • Werkzeug version: 0.14.1
@davidism
Copy link
Member

davidism commented Sep 16, 2018

Sounds like you didn't install the project (pip install -e .) and you're running from inside the tests directory.

I do know all steps complete successfully when followed in order, so without information about the issue there's not much I can do here. If you figure out what's going on and think the tutorial can be clearer, please submit a PR for review.

@lvlsun
Copy link
Author

lvlsun commented Sep 16, 2018

Flaskr has been installed:

(venv) ...\flaskproj>pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
atomicwrites (1.2.1)
attrs (18.2.0)
click (6.7)
colorama (0.3.9)
Flask (1.0.2)
flaskr (1.0.0, ...\flaskproj\flaskr)
itsdangerous (0.24)
Jinja2 (2.10)
MarkupSafe (1.0)
more-itertools (4.3.0)
pip (9.0.3)
pluggy (0.7.1)
py (1.6.0)
pytest (3.8.0)
setuptools (39.0.1)
six (1.11.0)
Werkzeug (0.14.1)
You are using pip version 9.0.3, however version 18.0 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

If I run python -m pytest, terminal produce

(venv) ...\flaskproj>python -m pytest
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: ...\flaskproj, inifile: setup.cfg
collected 24 items                                                             

tests\test_auth.py ........                                              [ 33%]
tests\test_blog.py ............                                          [ 83%]
tests\test_db.py ..                                                      [ 91%]
tests\test_factory.py ..                                                 [100%]

========================== 24 passed in 1.47 seconds ==========================

but pytest failed.

@hliang
Copy link

hliang commented Mar 22, 2019

I had a similar error if I put the setup.py inside package folder flaskr. After moving it out and place it next to flaskr (the same project layout as in tutorial), followed by reinstallation, the error was gone.

@brymck
Copy link

brymck commented Mar 25, 2019

I had a similar issue due to a pytest executable existing elsewhere on my $PATH.

$ which pytest
/usr/local/bin/pytest
$ pytest
ImportError while loading conftest '/Users/bryan/Sandbox/flaskr/tests/conftest.py'.
tests/conftest.py:2: in <module>
    from flaskr import create_app
E   ModuleNotFoundError: No module named 'flaskr'
$ which pytest
/Users/bryan/venv/bin/pytest
$ pytest
================================ test session starts =================================
platform darwin -- Python 3.7.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0
rootdir: /Users/bryan/Sandbox/flaskr, inifile: setup.cfg
collected 2 items

tests/test_factory.py ..                                                       [100%]

============================== 2 passed in 0.02 seconds ==============================

I believe what's happening in this case is that flaskr was installed in my virtual environment (i.e. ~/venv), but that's not accessible to /usr/local/bin/pytest, which I suppose belonged to my Python 3 installation via Homebrew.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants