0

W10, using the Command Prompt (not Cygwin). Python 3.8.5.

I've installed (using pip) pytest and have started using it OK. I've also installed BeautifulSoup4 using pip and started using it OK (in a non-test file).

But when I try to run a file with pytest which includes the line import bs4 it complains:

_

ImportError while importing test module 'D:\more software projects\Visual_Studio\caat-testing\caat-test1\url_test\test_caat_url.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
d:\apps\python\python36\lib\importlib\__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_caat_url.py:56: in <module>
    from bs4 import BeautifulSoup
E   ModuleNotFoundError: No module named 'bs4'

... anyone know what the problem might be?

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • What is the command that u used to install ```BeautifulSoup```? Did u use ```pip install bs4```? – Sushil Oct 13 '20 at 15:19
  • no, pip install beautifulsoup4, I believe. "import bs4" works OK in a non-test file though. – mike rodent Oct 13 '20 at 15:20
  • It should be ```pip install bs4```. At least that's what python's official website says: https://pypi.org/project/bs4/ – Sushil Oct 13 '20 at 15:21
  • 1
    Just tried it... same error. – mike rodent Oct 13 '20 at 15:22
  • pytest is installed for Python 3.6, as can be seen in the error log. Make sure you are using the correct interpreter version when running tests. – hoefling Oct 13 '20 at 15:47
  • @hoefling Thanks, hadn't spotted that. I'm a low-level Python-user and am currently floundering. When I do `python --version` at the command prompt it says "3.8.5". I have no idea why it would be using the 3.6 version. Could this be a reason why pytest can't see the BS module? NB I have found a couple of other questions here on the same subject: my question may be a duplicate. – mike rodent Oct 13 '20 at 15:58
  • @hoefling Specifically, answer by yourself: https://stackoverflow.com/a/49033954/595305, for a question which you then voted to close as dupe. NB I have so far tried your solution, and also __init__.py. Not working so far. – mike rodent Oct 13 '20 at 16:01
  • No, your error is different from the question linked. You have multiple versions of Python installed. `pytest` is installed for Python 3.6, while your code dependencies are installed for Python 3.8. If you have used installers from python.org, run `py -3.8 -m pip install pytest` to install `pytest` for 3.8. Afterwards, running `python -m pytest` should work. If `pytest` still doesn't work, the issue is in the wrong paths order in `PATH` env var - make sure the path to Python 3.8 comes before the 3.6 one. – hoefling Oct 13 '20 at 17:19
  • @hoefling. Thanks for the advice. In fact I've now set up a Python virtual environment, and then installed (using `pip`) `requests`, `beautifulsoup4` and `pytest`... and everything works! – mike rodent Oct 13 '20 at 17:24

1 Answers1

0

A low-level Python user here. All I know is that I installed a Python virtual environment in W10 and using the same version (3.8.5) then installed (using pip), requests, beautifulsoup4 and pytest. And everything just works.

mike rodent
  • 14,126
  • 11
  • 103
  • 157