preamble
While learning the blueprints for the Flask framework, I came across the package import that uses `from . Module import object`, and then tried to import directly will report an error, directly tell me can not find the module, found the problem with this record.
take
There is a flask framework project with the following directory structure.
There is a main package, pro_flask, and then two sub-packages, admin and web, and the same level of startup files as pro_flask.
I'm on the same level as admin and web in the__init__.py
file, import some resources from admin and web.
The code is as follows:
When importing resources in admin and web, it tells me thatModuleNotFoundError: No module named 'admin'
from flask import Flask # The following imported admin and web are the objects defined in __init__.py under the admin and web packages from admin import admin # After the project is started, an error is reported here from web import web # After the project is started, an error is reported here
The __init__.py file under the admin package
from flask import Blueprint admin = Blueprint( # Need to import this admin blueprint object in another module 'admin', __name__, template_folder='templates', static_folder='static' ) from . import views
The __init__.py file under the web package
from flask import Blueprint web = Blueprint( # Need to import this web blueprint object in another module 'web', __name__, template_folder='templates', static_folder='static' ) from . import views
Understanding the difference between import and from ...import
Notes from the big boys on the Internet.
import
import tkinter
(1) Reference packages
The import is introduced in the root directory of the package.__init__.py
The entire contents of the package, including its classes, public properties, public methods, methods, etc., are imported. (The essence of importing packages in this way is to execute the__init__.py
(Documentation)
(2) Citation module
(The essence of importing a module this way is to execute the module interpretation once and assign it to tkinter: module_name = "module_name.py all code")
===> import module_name -> module_name.py -> location of module_name.py -> (essentially a list)
from…import
(1) Reference packages
(from ... import ... is introduced in the root directory of the package)__init__.py
and the contents of a certain file) However, we know that importing packages is meaningless, the ultimate goal is to import the modules underneath the package. (This way of importing packages)
(2) Citation module
(The essence of the module is to break up the module_name.py file and put the part you want into the current file and execute it.)
analyze
Read the analysis of the big guys on the Internet as well as the solution and analyze it yourself
In general, python will load some default package directories and user-defined package directories into python's search module path set (, a list list), then when the user is importing packages, python will go to the search module path set to find out if the directory of the imported packages is in the search module path set. If it doesn't exist, then it throws an exception that the module was not found.
Code analysis: In the startup class, we print the set of module paths that python searches for
from pro_flask import app import sys print() if __name__ == '__main__': () ------------------------------------------------------- ['D:\\environment\\python-workspace\\flaskProject', 'D:\\environment\\python\\DLLs', 'D:\\environment\\python\\lib', 'D:\\environment\\python', 'D:\\environment\\python-workspace\\flaskProject\\venv', ........]
Notice this in the list'D:\\environment\\python-workspace\\flaskProject'
, which is the path where the project is located in the above screenshot.
Conclusion: That is to say, the directory where this project is located is loaded into python's search module path set, and all operations to import packages must start looking in the project's directory, or else something will go wrong
prescription
Option 1 (absolute path)
When importing modules, look in the root directory of the project
from flask import Flask from pro_flask.admin import admin # Just look in the root directory of your project from pro_flask.web import web
Option 2 (relative path, recommended)
When importing, use . to find the current file in the directory
from flask import Flask from . admin import admin # Start looking in the directory where the current file is located from . web import web
Similarly, if you start looking in the parent directory, then you can use the
from .. xxx import xxx
consultation
Difference between import and from ... import article
writings
summarize
To this point this article on Python from import package guide ModuleNotFoundError No module named can not find the module to solve the problem of the article is introduced to this, more related to Python from import package guide can not find the module content, please search for my previous posts or continue to browse the following related articles I hope that everyone! I hope you will support me more in the future!