ModuleNotFoundError: No module named xxx

Background: I encapsulate a package and contain multiple sub packages. Each sub package has multiple different moodles. When I refer to moodles again, it is OK to run in pychar. However, when I put this folder in Linux, there is an error: modulenotfounderror: no module named ‘package’  。
The structure is as follows:

package:

    ├─ sub_package0

        │ ├─ __init__.py

        │ ├─ utils.py

        │ └─ www.py

    ├─ sub_package1

        │ ├─ __init__.py

        │ ├─ utils1.py

        │ └─ www1.py

    ....

    ├─ __init__.py

    ├─ abc.py

    └─ xyz.py

After online Baidu, mainly tried two methods:

(1) One is to add environment variable pythonpath in. Bashrc of Linux environment http://blog.sina.com.cn/s/blog_ 9b1b494a0102vrl3.html

(2) In each moudule, add a path through the sys module https://www.cnblogs.com/dreamyu/p/7889959.html

After trying the first method, we found that the above error was still reported, while the second method was too cumbersome because it had to be added to each module. In order to save trouble, put the package directly into anaconda3’s installation path anaconda3/lib/python3.6/site-packages, and the problem is solved. The site packages library is actually the storage location of the package and module installed by PIP install.

 
Supplementary knowledge

Module: module. A. Py file can be called a module. Using module can avoid the conflict between function name and variable name. Functions and variables with the same name can be stored in different modules, but it should also be noted that the names of built-in functions should not conflict with each other.

Package: package. Different modules are organized by directory, similar to folder. Modules are organized by package to avoid conflicts. After the package is introduced, as long as the top-level package name does not conflict with others, all modules will not conflict with others. Please note that there is a under each package directory__ init__. Py file, this file must exist, otherwise, python will regard this directory as a normal directory instead of a package__ init__. Py can be an empty file or have Python code, because can be an empty file__ init__. Py itself is a module, and its module name is its package name.

 

Read More: