Python: How to import files in a folder (implementation method ),
The file import function of Python is similar to the include function of PHP, but it should be more like the require in PHP, because the import in Python cannot be executed as long as the target does not exist. To include files in the directory, you only need to set the correct path in PHP. Python is different. Let's take a look at this example.
Directory structure:
A. py: import the B. py file under the dir directory. The a. py code is as follows:
# Coding = UTF-8 "B. py file under the import dir directory" import dir. bprint dir. B. name
An error is returned when a. py is executed.
The system prompts that the module name dir. B cannot be found. By searching for official documents, we found that a _ init _. py file must be declared under the directory to contain files, even if the file is empty. Of course, this file can also initialize some data.
Create the _ init _. py file under dir. The directory structure is as follows:
Execute a. py again. Everything is OK!
In this article, how to import the files in the folder (implementation method) in Python is all the content that I have shared with you. I hope you can give us a reference and support the help house.