Python: Difference between revisions

Jump to navigation Jump to search
139 bytes added ,  2 months ago
m
Rephrasing
No edit summary
m (Rephrasing)
Line 667: Line 667:


=== ModuleNotFoundError: No module named 'X' === <!--T:115-->
=== ModuleNotFoundError: No module named 'X' === <!--T:115-->
When importing a Python module, it may not be found. Numerous reasons may explain this error but common cases are :
When trying to import a Python module, it may not be found. Some common causes are:
* the package is not installed or not visible by the python interpreter;
* the package is not installed or is not visible to the python interpreter;
* not imported as it is named;
* the name of the module to import is not the same as the name of the package that provides it;
* a broken virtual environment.
* a broken virtual environment.


<!--T:116-->
<!--T:116-->
In order to avoid these common cases, avoid:
To avoid such problems, do not:
* modifying <code>PYTHONPATH</code> environment variable;
* modify the <code>PYTHONPATH</code> environment variable;
* modifying <code>PATH</code> environment variable;
* modify the <code>PATH</code> environment variable;
* loading a module with a virtual env. activated (which in turn modifies the priors)
* load a module while a virtual environment is activated (activate your virtual environment only after loading all the required modules)


<!--T:117-->
<!--T:117-->
To solve this, ensure that the above element did not interfere, then:
When you encounter this problem, first make sure you followed the above advice. Then:
* that the package is installed and named correctly (upper or lower case and underscores matters); run <code>pip list</code>;
* make sure that the package is installed; run <code>pip list</code>;
* that it is imported at the correct level (when importing from its source directory).
* double-check the module name (upper or lower case and underscores matter);
* make sure that the module is imported at the correct level (when importing from its source directory).


<!--T:118-->
<!--T:118-->
In doubt, start from zero in a clean new environment.
In doubt, start over with a new, empty virtual environment.
</translate>
</translate>
cc_staff
127

edits

Navigation menu