The import include file feature of Python is similar to PHP's include, but more specifically, it should be more like PHP require, because the import of Python as long as the target does not exist in the error program can not be executed down. To include the files in the directory, PHP only need to give the path to OK. Python is different, let's take a look at the example below.
Directory structure:
a.py to import the b.py file under the dir directory. The a.py code is as follows:
| 123456 |
# Coding=utf-8 "import dir 目录下的 b.py 文件" import dir .b print dir .b.name |
Execute a.py Error
Tip The name of this module cannot be found dir.b. By finding the official documentation, you will need to declare a __init__.py file in the directory when you want to include the files in the directory, even if the file is empty. Of course, this file can also start some data.
Then, under Dir, create a new __init__.py file with the following directory structure:
Re-execute a.py, everything ok!
Python How to import files under a folder