pynguin icon indicating copy to clipboard operation
pynguin copied to clipboard

Pynguin throws module not found error for internal packages

Open dineshkumarkb opened this issue 4 years ago • 2 comments

Hi There,

I am using Pynguin to generate unit tests for my project. My project has an internal package dependency. Let's call it datapackage. This is installed in a virtual environment. My Pycharm and REPL are able to detect datapackage and import them.

However, Pynguin throws module not found error.

Please find the stacktrace below.

$pynguin --algorithm WHOLE_SUITE --project_path src --output_path tests\unit --module_name client.py

[13:22:35] ERROR    Failed to load SUT: No module named 'datapackage'                                                                                         generator.py:137
                    ┌──────────────────────────────────────────────── Traceback (most recent call last) ─────────────────────────────────────────────────┐
                    │ c:\users\username\appdata\local\programs\python\python38\lib\site-packages\pynguin\generator.py:133 in _load_sut                  │
                    │                                                                                                                                    │
                    │   130 │   try:                                                                                                                     │
                    │   131 │   │   # We need to set the current thread ident so the import trace is recorded.                                           │
                    │   132 │   │   tracer.current_thread_ident = threading.currentThread().ident                                                        │
                    │ > 133 │   │   importlib.import_module(config.configuration.module_name)                                                            │
                    │   134 │   except ImportError as ex:                                                                                                │
                    │   135 │   │   # A module could not be imported because some dependencies                                                           │
                    │   136 │   │   # are missing or it is malformed                                                                                     │
                    │                                                                                                                                    │
                    │ c:\users\username\appdata\local\programs\python\python38\lib\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 = {}                                                                                                              │
                    │ <frozen importlib._bootstrap>:1014 in _gcd_import                                                                                  │
                    │ <frozen importlib._bootstrap>:991 in _find_and_load                                                                                │
                    │ <frozen importlib._bootstrap>:961 in _find_and_load_unlocked                                                                       │
                    │ <frozen importlib._bootstrap>:219 in _call_with_frames_removed                                                                     │
                    │ <frozen importlib._bootstrap>:1014 in _gcd_import                                                                                  │
                    │ <frozen importlib._bootstrap>:991 in _find_and_load                                                                                │
                    │ <frozen importlib._bootstrap>:975 in _find_and_load_unlocked                                                                       │
                    │ <frozen importlib._bootstrap>:671 in _load_unlocked                                                                                │
                    │ <frozen importlib._bootstrap_external>:783 in exec_module                                                                          │
                    │ <frozen importlib._bootstrap>:219 in _call_with_frames_removed                                                                     │
                    │                                                                                                                                    │
                    │ C:\<path to your module>:2 in <module>                       │
                    │                                                                                                                                    │
                    │     1 from datapackage.http.request_dispatcher import dispatch_retry_session                                                          │
                    │ >   2 from requests import HTTPError                                                                                               │
                    │     3                                                                                                                              │
                    │                                                          │
                    │                                                                                     │
                    └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
                    ModuleNotFoundError: No module named 'datapackage'

dineshkumarkb avatar May 07 '21 07:05 dineshkumarkb

Hello @dineshkumarkb, thanks for trying out Pynguin.

My best guess as to why Pynguin fails to find your private module is that you are probably executing Pynguin outside the virtual environment where your package is installed. Pynguin has only access to the packages of the environment in which it is started. Can you please double check this?

Maybe two more notes:

  • Judging from your stack-trace, you are executing Pynguin outside of a container, e.g., Docker. This is discouraged, because Pynguin will actually execute the module for which it should generate tests. This may also include other modules that are imported by the specified module. If any of these modules do 'bad' stuff, e.g., delete files, then nothing is stopping Pynguin from wiping your hard drive.
  • From the stack-trace I can also see that your module does some network related stuff. For now, Pynguin does not have any builtin support for mocking network related things, so any randomly generated network operations will simply be sent out to the respective web servers, which may or may not be what you want. We might add support for mocking network operations in a later version.

Best regards Florian

@stephanlukasczyk FYI.

Wooza avatar May 07 '21 11:05 Wooza

Thanks a lot for your swift response @Wooza .

I am running Pynguin inside a virtual environment as it was mentioned as the recommended way in the doc.

As you rightly said, I am not running Pynguin from a Docker. I will try that.

Thanks for the additional information on the network mock.

dineshkumarkb avatar May 07 '21 12:05 dineshkumarkb