I have created a cookiecutter template. The folder of the template is named as {{cookicutter.project_slug}}
. Inside this folder, I have the script make_repository_structure.py
that creates the necessary structure dependent on the repository application type (i.e., a Python package, data science project, etc.). The template repository has the following structure
.
|- {{cookicutter.project_slug}}/
| |- make_repository_structure.py
|
|- tests/
|- test_make_repository_structure.py
What I am trying to do is to design a test that verify if this repository structure is correctly implemented. I want to use Pytest to do this, and I wrote the file test_make_repository_structure.py
that starts with
import sys
from pathlib import Path
import pytest
PROJECT_ROOT = Path(__file__).parents[1].resolve()
sys.path.append(str(PROJECT_ROOT))
mrs = __import__(r"{{cookiecutter.project_slug}}")
def test_error_on_invalid_repository_type():
with pytest.raises(ValueError):
mrs.make_repository_structure.main("invalid_repository_type")
When I run this script with Pytest, I get the following error:
pytest
============================================================================ test session starts =============================================================================
platform linux -- Python 3.11.1, pytest-7.2.0, pluggy-1.0.0
rootdir: /workspaces/python.template, configfile: pyproject.toml, testpaths: tests
collected 0 items / 1 error
=================================================================================== ERRORS ===================================================================================
__________________________________________________________ ERROR collecting tests/test_make_repository_structure.py __________________________________________________________
ImportError while importing test module '/workspaces/python.template/tests/test_make_repository_structure.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/home/python.template/.pyenv/versions/3.11.1/lib/python3.11/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/test_make_repository_structure.py:10: in <module>
mrs = __import__(r"{{cookiecutter.project_slug}}")
E ModuleNotFoundError: No module named '{{cookiecutter'
========================================================================== short test summary info ===========================================================================
ERROR tests/test_make_repository_structure.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================== 1 error in 0.08s ==============================================================================
From the error, it looks that the import having an issue with the .
character. I am aware that the naming convention for imports is not the best, but I ran out of ideas on how to solve this. Does someone have an idea to tackle this issue?
Many thanks,
Ps.: I am running Python 3.11.
CodePudding user response:
Try using the importlib.import_module
function to import the the module:
import importlib
mrs = importlib.import_module("{{cookiecutter.project_slug}}")
This should fix the error ModuleNotFound
CodePudding user response:
you can try use:
import importlib
mrs = importlib.import_module("{{cookiecutter.project_slug}}")
instead of:
mrs = __import__(r"{{cookiecutter.project_slug}}")
Hope this helps.