ModuleNotFoundError: No module named 'XYZ'
samreenmallick opened this issue · comments
Hello,
I am using penguin in my project and getting the following error.
ModuleNotFoundError: No module named 'webapp'.
My project structure is as follows
Folder structure:
- webapp (does not contain __init__.py)
-
- server (does not contain __init__.py)
-
-
- module1 (contains __init__.py)
-
-
-
- module2 (contains __init__.py)
....
- module2 (contains __init__.py)
-
-
-
- authUtilities (contains __init__.py)
-
The code in module1/__init__.py has an import statement as follows
from webapp.server.authUtilities import verify_token
Here's the penguin command I am executing.
pynguin --project-path XXX/webapp/server/module1 --output-path XXX/webapp/testCases --module-name __init__ -v
The error I get is
ModuleNotFoundError: No module named 'webapp'
Here's the entire trace
...../.venv/lib/python3.9/site-packages/pynguin/generator.py:141 in _load_sut
│ 138 │ try:
│ 139 │ │ # We need to set the current thread ident so the import trace is recorded.
│ 140 │ │ tracer.current_thread_identifier = threading.current_thread().ident
| ❱ 141 │ │ importlib.import_module(config.configuration.module_name)
│ 142 │ except ImportError as ex:
│ 143 │ │ # A module could not be imported because some dependencies
144 │ │ # are missing or it is malformed
..../opt/anaconda3/lib/python3.9/importlib/__init__.py:127 in import_module
│
│ 124 │ │ │ if character != '.':
│ 125 │ │ │ │ break
│ 126 │ │ │ level += 1
│ ❱ 127 │ return _bootstrap._gcd_import(name[level:], package, level)
│ 128
| 129
│ 130 _RELOADING = {}
│ :1030 in _gcd_import
│ :1007 in _find_and_load
│ :986 in _find_and_load_unlocked
│ :680 in _load_unlocked
...... /.venv/lib/python3.9/site-packages/pynguin/instrumentation/machinery.py:44 in exec_module
│ 41 │
│ 42 │ def exec_module(self, module):
│ 43 │ │ self._tracer.reset()
│ ❱ 44 │ │ super().exec_module(module)
│ 45 │ │ self._tracer.store_import_trace()
│ 46 │
│ 47 │ def get_code(self, fullname) -> CodeType:
│ <frozen importlib._bootstrap_external>:850 in exec_module
│ <frozen importlib._bootstrap>:228 in _call_with_frames_removed
XXX/webapp/server/module1/__init__.py:4 in
│ 1 import json
|
│ ❱ 4 from webapp.server.authUtilities import verify_token
I am executing penguin in a virtual environment however not inside a docker.
Is there a specific switch that I should be using?
Kindly excuse if this is a novice question, would appreciate any direction.
Hi,
Thank you for your interest in Pynguin. It's hard to tell what the exact reason is, but I've spotted one issue directly: you are using a Virtual Environment based on Python 3.9. Pynguin requires Python 3.10, it won't work with Python 3.9 (neither with older version, nor with Python 3.11); this is because Pynguin instruments Python's byte code, which changes between the different versions.
Could you try again with a virtual environment based on Python 3.10, please?
Hi Stephan,
Thank you for getting back. I tried using virtual environment based on Python 3.10, I'm still getting the same error.
I assume that the issue lies in how Python's module handling works. I was able to reproduce the issue on my machine using Pynguin 0.27.0. However, I also managed Pynguin to generate tests using the following command line (reusing your XXX placeholder from the initial post):
pynguin --project-path XXX --output-path XXX/webapp/testCases --module-name web app.server.module1 -v
Since you did not provide a minimal example project, I've filled the following files with the below content:
webapp/server/authUtilities/__init__.py
def verify_token(token: str) -> bool:
return token != ""
webapp/server/module1/__init__.py
from webapp.server.authUtilities import verify_token
def foo():
return verify_token("foo")