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

pytest showing ModuleNotFoundError: No module named #731

Open
bicepjai opened this issue Aug 13, 2023 · 1 comment
Open

pytest showing ModuleNotFoundError: No module named #731

bicepjai opened this issue Aug 13, 2023 · 1 comment
Labels
waiting response/feedback Blocked while waiting information/feeback from the issue/PR/discussion author.

Comments

@bicepjai
Copy link

bicepjai commented Aug 13, 2023

Using pyscaffold 4.5, tests are successfully run using tox, but pytest is unable to run the tests

a minimal, self-contained, and reproducible example.

putup --very-verbose sample-project
cd sample-project
tox
pytest tests

test run output

$ tox                                                                                                                                                                         (python-env)  23-08-13 00:47
.pkg: install_requires> python -I -m pip install 'setuptools>=46.1.0' 'setuptools_scm[toml]>=5'
.pkg: _optional_hooks> python /Users/username/Programs/anaconda3/envs/python-env/lib/python3.10/site-packages/pyproject_api/_backend.py True setuptools.build_meta
.pkg: get_requires_for_build_sdist> python /Users/username/Programs/anaconda3/envs/python-env/lib/python3.10/site-packages/pyproject_api/_backend.py True setuptools.build_meta
.pkg: get_requires_for_build_wheel> python /Users/username/Programs/anaconda3/envs/python-env/lib/python3.10/site-packages/pyproject_api/_backend.py True setuptools.build_meta
.pkg: install_requires_for_build_wheel> python -I -m pip install wheel
.pkg: prepare_metadata_for_build_wheel> python /Users/username/Programs/anaconda3/envs/python-env/lib/python3.10/site-packages/pyproject_api/_backend.py True setuptools.build_meta
.pkg: build_sdist> python /Users/username/Programs/anaconda3/envs/python-env/lib/python3.10/site-packages/pyproject_api/_backend.py True setuptools.build_meta
default: install_package_deps> python -I -m pip install 'importlib-metadata; python_version < "3.8"' pytest pytest-cov setuptools
default: install_package> python -I -m pip install --force-reinstall --no-deps /Users/username/Projects/sample-project/.tox/.tmp/package/1/sample-project-0.0.post1.dev1+gc20e218.tar.gz
default: commands[0]> pytest
============================================================================================= test session starts =============================================================================================
platform darwin -- Python 3.10.12, pytest-7.4.0, pluggy-1.2.0 -- /Users/username/Projects/sample-project/.tox/default/bin/python
cachedir: .tox/default/.pytest_cache
rootdir: /Users/username/Projects/sample-project
configfile: setup.cfg
testpaths: tests
plugins: cov-4.1.0
collected 2 items                                                                                                                                                                                             

tests/test_skeleton.py::test_fib PASSED                                                                                                                                                                 [ 50%]
tests/test_skeleton.py::test_main PASSED                                                                                                                                                                [100%]

--------- coverage: platform darwin, python 3.10.12-final-0 ----------
Name                             Stmts   Miss Branch BrPart  Cover   Missing
----------------------------------------------------------------------------
src/sample_project/__init__.py       6      0      0      0   100%
src/sample_project/skeleton.py      32      1      2      0    97%   135
----------------------------------------------------------------------------
TOTAL                               38      1      2      0    98%


============================================================================================== 2 passed in 0.02s ==============================================================================================
.pkg: _exit> python /Users/username/Programs/anaconda3/envs/python-env/lib/python3.10/site-packages/pyproject_api/_backend.py True setuptools.build_meta
  default: OK (6.44=setup[5.95]+cmd[0.49] seconds)
  congratulations :) (6.60 seconds)
 ~/P/sample-project   main  
$ pytest                                                                                                                                                                      (python-env)  23-08-13 00:47
============================================================================================= test session starts =============================================================================================
platform darwin -- Python 3.10.12, pytest-7.4.0, pluggy-1.2.0 -- /Users/username/Programs/anaconda3/envs/python-env/bin/python
cachedir: .pytest_cache
rootdir: /Users/username/Projects/sample-project
configfile: setup.cfg
testpaths: tests
plugins: cov-4.1.0
collected 0 items / 1 error                                                                                                                                                                                   
/Users/username/Programs/anaconda3/envs/python-env/lib/python3.10/site-packages/coverage/inorout.py:507: CoverageWarning: Module sample_project was never imported. (module-not-imported)
  self.warn(f"Module {pkg} was never imported.", slug="module-not-imported")
/Users/username/Programs/anaconda3/envs/python-env/lib/python3.10/site-packages/coverage/control.py:883: CoverageWarning: No data was collected. (no-data-collected)
  self._warn("No data was collected.", slug="no-data-collected")
WARNING: Failed to generate report: No data to report.

/Users/username/Programs/anaconda3/envs/python-env/lib/python3.10/site-packages/pytest_cov/plugin.py:312: CovReportWarning: Failed to generate report: No data to report.

  warnings.warn(CovReportWarning(message))

=================================================================================================== ERRORS ====================================================================================================
___________________________________________________________________________________ ERROR collecting tests/test_skeleton.py ___________________________________________________________________________________
ImportError while importing test module '/Users/username/Projects/sample-project/tests/test_skeleton.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../Programs/anaconda3/envs/python-env/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_skeleton.py:3: in <module>
    from sample_project.skeleton import fib, main
E   ModuleNotFoundError: No module named 'sample_project'

--------- coverage: platform darwin, python 3.10.12-final-0 ----------

=========================================================================================== short test summary info ===========================================================================================
ERROR tests/test_skeleton.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================== 1 error in 0.04s ===============================================================================================
 2  ~/P/sample-project   main  
$                                                                                                                                                                             (python-env)  23-08-13 00:48

Versions and main components

  • PyScaffold Version: 4.5
  • Python Version: 3.10.12
  • Operating system: macOS Ventura 13.5 Update (22G74)
  • How did you install PyScaffold: pip
@abravalheri
Copy link
Collaborator

Hi @bicepjai thank you very much for the issue and sorry for the delay.

I think there might be a misunderstanding in your reproducer:

putup --very-verbose sample-project
cd sample-project
tox

tox will already run the tests in a way that makes your package available. You don't have to manually run pytest tests (and if you do, you are kind of expected to get the error you are seeing because pytest does not install any dependencies).

If you want to run the tests, you just need to run tox.

Does that work for you?

@abravalheri abravalheri added the waiting response/feedback Blocked while waiting information/feeback from the issue/PR/discussion author. label Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting response/feedback Blocked while waiting information/feeback from the issue/PR/discussion author.
Projects
None yet
Development

No branches or pull requests

2 participants