Method of the Py file under the Python Reference (import) folder

Source: Internet
Author: User

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:

Copy the Code code as follows:
# Coding=utf-8
"b.py file under import dir directory"

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!

Transferred from: http://www.jb51.net/article/54323.htm

Method of the Py file under the Python Reference (import) folder

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.