M220P: can't manually run projections test

I’ve got the mflix app in a venv. In that env, I run ‘pytest -m projections’ and get:

lib/python3.9/site-packages/_pytest/config.py:366: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   _pytest.config.ConftestImportFailure: ModuleNotFoundError("No module named 'requests'")
E     File "/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/_pytest/assertion/rewrite.py", line 212, in load_module
E       py.builtin.exec_(co, mod.__dict__)
E     File "/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/notebook/tests/selenium/conftest.py", line 4, in <module>
E       import requests

If I run the app itself and run the test from the Status web page it works. What is missing here? (well, the requests module, obviously) I’ve got python 3.9.7 running on Ubuntu.

thanks, Phil

Are you sure about the test name?

If I remember correction it is projection rather than projections.

1 Like

Thanks, Steeve. My bad - that was a typo when creating the question. I get the error with: ‘pytest -m projection’.

Post a screenshot of the whole terminal. The error message you posted is not sufficient for us to understand the context.

Here tis.

If that is too small, here is (all) the text:

(mflix) (base) pcrooker@phil-envy:~/mongo_courses/mflix$ pytest -m projection
================================== test session starts ===================================
platform linux -- Python 3.9.7, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: /home/pcrooker/mongo_courses/mflix, inifile: pytest.ini
plugins: flask-0.11.0
collected 0 items / 1 errors                                                             

========================================= ERRORS =========================================
___________________________________ ERROR collecting  ____________________________________
lib/python3.9/site-packages/_pytest/config.py:327: in _getconftestmodules
    return self._path2confmods[path]
E   KeyError: local('/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/notebook/tests/selenium')

During handling of the above exception, another exception occurred:
lib/python3.9/site-packages/_pytest/config.py:358: in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/notebook/tests/selenium/conftest.py')

During handling of the above exception, another exception occurred:
lib/python3.9/site-packages/_pytest/config.py:364: in _importconftest
    mod = conftestpath.pyimport()
lib/python3.9/site-packages/py/_path/local.py:701: in pyimport
    __import__(modname)
lib/python3.9/site-packages/_pytest/assertion/rewrite.py:212: in load_module
    py.builtin.exec_(co, mod.__dict__)
lib/python3.9/site-packages/notebook/tests/selenium/conftest.py:4: in <module>
    import requests
E   ModuleNotFoundError: No module named 'requests'

During handling of the above exception, another exception occurred:
lib/python3.9/site-packages/py/_path/common.py:377: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:418: in gen
    dirs = self.optsort([p for p in entries
lib/python3.9/site-packages/py/_path/common.py:419: in <listcomp>
    if p.check(dir=1) and (rec is None or rec(p))])
lib/python3.9/site-packages/_pytest/main.py:723: in _recurse
    ihook = self.gethookproxy(path)
lib/python3.9/site-packages/_pytest/main.py:627: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
lib/python3.9/site-packages/_pytest/config.py:341: in _getconftestmodules
    mod = self._importconftest(conftestpath)
lib/python3.9/site-packages/_pytest/config.py:366: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   _pytest.config.ConftestImportFailure: ModuleNotFoundError("No module named 'requests'")
E     File "/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/_pytest/assertion/rewrite.py", line 212, in load_module
E       py.builtin.exec_(co, mod.__dict__)
E     File "/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/notebook/tests/selenium/conftest.py", line 4, in <module>
E       import requests
!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!
================================ 1 error in 1.07 seconds =================================
(mflix) (base) pcrooker@phil-envy:~/mongo_courses/mflix$

Please post a screenshot of the terminal where you run the app.

Here it is

Also, here is the browser showing the successful test:

thanks, Phil

This

your prompt when you run the failing tests and this

(mflix) phil-envy:~/mongo_courses/mflix$

your prompt when you run the application tells me you are not setting up your virtual environment the same way when running the test as when you are running the application.

It means not the same modules are in your environment.

  • So start a new terminal as you did for running the application.
  • Change directory to the same directory you use to run the application
  • Execute the same . bin/activate command as you did when running the application
  • Your prompt should now have only (mflix) like in your last post
  • Run the test that used to failed

For some reason, I don’t even know how to do that, you reactivated the (base) virtual environment which does not have the same modules as (mflix).

No, the (base) prompt is to do with anaconda, nothing to do with the python venv. I commented out the anaconda stuff that was in the .bashrc which produces the (base) prompt - it runs conda.sh. I was annoyed with the prompt.

Anyway, I just ran the projection test in the ‘new’ environment and got the same error:

phil-envy:~$ cd mongo_courses/mflix/
phil-envy:~/mongo_courses/mflix$ . bin/activate
(mflix) phil-envy:~/mongo_courses/mflix$ python run.py 
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 279-327-356
127.0.0.1 - - [12/May/2022 10:27:45] "GET /status HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2022 10:27:45] "GET /static/js/1.908cc23a.chunk.js HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2022 10:27:45] "GET /static/js/main.02d67aeb.chunk.js HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2022 10:27:45] "GET /static/css/main.d2c98b4b.chunk.css HTTP/1.1" 304 -
127.0.0.1 - - [12/May/2022 10:27:46] "GET /static/media/mongoleaf.0ebc1843.png HTTP/1.1" 304 -
127.0.0.1 - - [12/May/2022 10:27:46] "GET /static/media/pixelatedLeaf.6c93bd20.svg HTTP/1.1" 304 -
127.0.0.1 - - [12/May/2022 10:27:46] "GET /manifest.json HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2022 10:27:46] "GET /favicon.ico HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2022 10:27:59] "GET /api/v1/movies/countries?countries=Australia HTTP/1.1" 200 -
^Z
[1]+  Stopped                 python run.py
(mflix) phil-envy:~/mongo_courses/mflix$ bg
[1]+ python run.py &
(mflix) phil-envy:~/mongo_courses/mflix$ pytest -m projection
============================================================================== test session starts ===============================================================================
platform linux -- Python 3.9.7, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: /home/pcrooker/mongo_courses/mflix, inifile: pytest.ini
plugins: flask-0.11.0
collected 0 items / 1 errors                                                                                                                                                     

===================================================================================== ERRORS =====================================================================================
_______________________________________________________________________________ ERROR collecting  ________________________________________________________________________________
lib/python3.9/site-packages/_pytest/config.py:327: in _getconftestmodules
    return self._path2confmods[path]
E   KeyError: local('/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/notebook/tests/selenium')

During handling of the above exception, another exception occurred:
lib/python3.9/site-packages/_pytest/config.py:358: in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/notebook/tests/selenium/conftest.py')

During handling of the above exception, another exception occurred:
lib/python3.9/site-packages/_pytest/config.py:364: in _importconftest
    mod = conftestpath.pyimport()
lib/python3.9/site-packages/py/_path/local.py:701: in pyimport
    __import__(modname)
lib/python3.9/site-packages/_pytest/assertion/rewrite.py:212: in load_module
    py.builtin.exec_(co, mod.__dict__)
lib/python3.9/site-packages/notebook/tests/selenium/conftest.py:4: in <module>
    import requests
E   ModuleNotFoundError: No module named 'requests'

During handling of the above exception, another exception occurred:
lib/python3.9/site-packages/py/_path/common.py:377: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:418: in gen
    dirs = self.optsort([p for p in entries
lib/python3.9/site-packages/py/_path/common.py:419: in <listcomp>
    if p.check(dir=1) and (rec is None or rec(p))])
lib/python3.9/site-packages/_pytest/main.py:723: in _recurse
    ihook = self.gethookproxy(path)
lib/python3.9/site-packages/_pytest/main.py:627: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
lib/python3.9/site-packages/_pytest/config.py:341: in _getconftestmodules
    mod = self._importconftest(conftestpath)
lib/python3.9/site-packages/_pytest/config.py:366: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   _pytest.config.ConftestImportFailure: ModuleNotFoundError("No module named 'requests'")
E     File "/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/_pytest/assertion/rewrite.py", line 212, in load_module
E       py.builtin.exec_(co, mod.__dict__)
E     File "/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/notebook/tests/selenium/conftest.py", line 4, in <module>
E       import requests
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================ 1 error in 1.22 seconds =============================================================================
(mflix) phil-envy:~/mongo_courses/mflix$ 

Share your configuration file.

I responded via the notification email and obviously that didn’t go anywhere. What config file? I only see pyvenv.cfg:

home = /usr/bin
include-system-site-packages = true
version = 3.9.7

Looking at the error, it showed that the requests module was missing. I installed that, now I get this error:

(mflix) phil-envy:~/mongo_courses/mflix$ pytest -m projections
========================================================================= test session starts =========================================================================
platform linux -- Python 3.9.7, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: /home/pcrooker/mongo_courses/mflix, inifile: pytest.ini
plugins: flask-0.11.0
collected 0 items / 1 errors                                                                                                                                          

=============================================================================== ERRORS ================================================================================
__________________________________________________________________________ ERROR collecting  __________________________________________________________________________
lib/python3.9/site-packages/_pytest/config.py:327: in _getconftestmodules
    return self._path2confmods[path]
E   KeyError: local('/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/notebook/tests/selenium')

During handling of the above exception, another exception occurred:
lib/python3.9/site-packages/_pytest/config.py:358: in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/notebook/tests/selenium/conftest.py')

During handling of the above exception, another exception occurred:
lib/python3.9/site-packages/_pytest/config.py:364: in _importconftest
    mod = conftestpath.pyimport()
lib/python3.9/site-packages/py/_path/local.py:701: in pyimport
    __import__(modname)
lib/python3.9/site-packages/_pytest/assertion/rewrite.py:212: in load_module
    py.builtin.exec_(co, mod.__dict__)
lib/python3.9/site-packages/notebook/tests/selenium/conftest.py:11: in <module>
    from selenium.webdriver import Firefox, Remote, Chrome
E   ModuleNotFoundError: No module named 'selenium'

During handling of the above exception, another exception occurred:
lib/python3.9/site-packages/py/_path/common.py:377: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
lib/python3.9/site-packages/py/_path/common.py:418: in gen
    dirs = self.optsort([p for p in entries
lib/python3.9/site-packages/py/_path/common.py:419: in <listcomp>
    if p.check(dir=1) and (rec is None or rec(p))])
lib/python3.9/site-packages/_pytest/main.py:723: in _recurse
    ihook = self.gethookproxy(path)
lib/python3.9/site-packages/_pytest/main.py:627: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
lib/python3.9/site-packages/_pytest/config.py:341: in _getconftestmodules
    mod = self._importconftest(conftestpath)
lib/python3.9/site-packages/_pytest/config.py:366: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   _pytest.config.ConftestImportFailure: ModuleNotFoundError("No module named 'selenium'")
E     File "/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/_pytest/assertion/rewrite.py", line 212, in load_module
E       py.builtin.exec_(co, mod.__dict__)
E     File "/home/pcrooker/mongo_courses/mflix/lib/python3.9/site-packages/notebook/tests/selenium/conftest.py", line 11, in <module>
E       from selenium.webdriver import Firefox, Remote, Chrome
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================================= 1 error in 1.16 seconds =======================================================================
(mflix) phil-envy:~/mongo_courses/mflix$ 

Are you sure these test scripts actually run at the command line? Thanks.

The configuration file mentioned in Chapter 0 - Introduction and Setup - README: Setting Up mflix.

Rename this file to .ini with the following command:

mv dotini_unix .ini # on Unix
ren dotini_win .ini # on Windows

Here it is:

hil-envy:~/mongo_courses/mflix$ ls -al .ini
-rw-rw-r-- 1 pcrooker pcrooker 553 May  7 13:51 .ini
phil-envy:~/mongo_courses/mflix$ cat .ini
# Ticket: Connection
# Rename this file to .ini after filling in your MFLIX_DB_URI and your SECRET_KEY
# Do not surround the URI with quotes

[PROD]
SECRET_KEY = super_secret_key_you_should_change
#MFLIX_DB_URI = mongodb+srv://m220student:m220password@<your-atlas-cluster-address>
MFLIX_DB_URI = mongodb+srv://m001-user:xxxxxxxxxxx@sandbox.y769h.mongodb.net/test
MFLIX_NS = sample_mflix

[TEST]
SECRET_KEY = super_secret_testing_key
MFLIX_DB_URI = mongodb+srv://m001-user:xxxxxxxxxxx@sandbox.y769h.mongodb.net/test
MFLIX_NS = sample_mflix

I have read in some other thread that python-3.8 should work.

Could I have a look at this thread?