change the message in ModuleNotFoundError
Project description
friendly_module_not_found_error
This is a Python package that provides a custom exception class for handling module not found errors in a friendly way.
Installation
To install the package, run the following command:
pip install friendly_module_not_found_error
Usage
Don't need to import the packages. The pth file is already in the site-packages folder. Any using for the module in your programs are undocumental behavior(UB).
You can use the code to test the effects of the package:
import ant
The message raised will change to : "No module named 'ant'. Did you mean 'ast'?" The suggestion may be change according to the package you have installed.
import multiprocessing.dumy
The message raised will change to : "module 'multiprocessing' has no child module 'dumy'. Did you mean 'dummy'?"
You can also run "testmodule" to test the effects of the python.
Effect and explain
The example:
import xxx.yyy.zzz
If "xxx" not exist, the message is:
"No module named 'xxx'"
If "xxx" exist but "yyy" not exist, the message is:
"module 'xxx' has no child module 'yyy'"
Then the message add like the text below:
The final name will be compared to all module at that path. If at the top, it first compared with stdlib and then compared with the path in sys.path. Or, if the module before is not a package and the now module not exist, the message will add "module '...' is not a package". For the non-package module, it won't support for this condition: module has a child module, and it has child module. For package, it will scan the attribute __path__ to get all possible child module to compare.
The change can clearly show the specific error in import and give the near name suggestion. For example, the original is "No module named 'xxx.yyy.zzz'", we cannot get message that which step is wrong, now we can see which step is wrong:
"No module named 'xxx'" means the top, "module 'xxx' has no child module 'yyy'" means the second, and ''module 'xxx.yyy' has no child module 'zzz'" means the third, and so on. And like NameError and AttributeError, it will suggest the possible name.
require
python3.7+
In friendly_module_not_found_error verison 0.4.2, it supports python3.7+.
License
This package is licensed under the MIT License. See the LICENSE file for more information.
issues
If you have any questions or suggestions, please open an issue on GitHub.
Contributing
Contributions are welcome! Please submit a pull request with your changes.
data
The test for "__main__.py" here:
| No. | number of entries | used time(/s) | average time(/s) | result |
|---|---|---|---|---|
| 1 | 5 | 0.064 | 0.013 | success |
| 2 | 5 | 0.072 | 0.014 | success |
| 3 | 5 | 0.052 | 0.010 | success |
| 4 | 5 | 0.056 | 0.011 | success |
| 5 | 5 | 0.057 | 0.011 | success |
| 6 | 6 | 0.081 | 0.014 | success |
| 7 | 6 | 0.062 | 0.010 | success |
| 8 | 6 | 0.091 | 0.015 | success |
| 9 | 6 | 0.064 | 0.011 | success |
| 10 | 6 | 0.064 | 0.011 | success |
The speed test here:
| tool | function | arg | number | average time(/ms) |
|---|---|---|---|---|
| timeit | find_all_packages | (no args) | 1000 | 8.567 |
| timeit | scan_dir | path/to/site-packages | 1000 | 4.845 |
Note
If a module that is not a package contains submodules, and those submodules also contain their own submodules, this nested module structure is not supported. When this situation occurs, you should reorganize the code using proper package structure. This approach violates Python's packaging best practices and should be avoided.
To make your custom import hook be supported, you need to define a magic method __find__ to return the list of all modules.
For example:
class MyImportHook:
def __find__(self, name: str=None) -> list[str]:
"""
Return a list of all modules that are available to the import hook without import them.
If the "name" is provided, the method should return a list of all submodules that under the module named "name".
parameter name: The name of the module to find submodules for. If None, return all top modules.
"""
return []
The __find__ method should return a list of all modules that are available to the import hook without import them.
If the "name" is provided, the method should return a list of all submodules that under the module named "name". Or it needs to return all top modules if the "name" is None.
Rejected suggestion
- Build a cache for site-packages when install: The code runs fast, so it can find all of the packages fast. Before that finding costs, the computer has been almost broken.
- Suggest for "pip install xxx": spell mistakes are often closely associated with homograph attacks. Suggesting for it will help it.
Credits
This package was created by Locked-chess-official and is maintained by Locked-chess-official
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file friendly_module_not_found_error-0.4.3.tar.gz.
File metadata
- Download URL: friendly_module_not_found_error-0.4.3.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebb779efcc9f4f38bc561ec92505741b96ec412ff05d56b587912f561ec4b963
|
|
| MD5 |
41d974a9e7b3a81525b657110fd08e97
|
|
| BLAKE2b-256 |
f9cb15ab7ef10c8c94710536703b33824a38339650f6425afa7900f216066b64
|
File details
Details for the file friendly_module_not_found_error-0.4.3-py3-none-any.whl.
File metadata
- Download URL: friendly_module_not_found_error-0.4.3-py3-none-any.whl
- Upload date:
- Size: 16.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
811c7cb9ef049e6f5a1a2ed3accf144084a05165f2b5b38b4cc3ce8a4f21d86f
|
|
| MD5 |
aeb31a3cfb74cf5aba35e02bdca8bf15
|
|
| BLAKE2b-256 |
c11b2107f0f9c6f850921beaa59e64b77d84dc27dc504f3ff0e649f73a692e90
|