A method for a py file under the Python Reference (import) folder _python

Source: Internet
Author: User

The import of Python contains file features similar to those in PHP, but more specifically, it should be more like the require in PHP, because the error program cannot be executed as long as the target does not exist in Python. 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 this example.

Directory structure:

a.py to import the b.py file in the dir directory. The a.py code is as follows:

Copy Code code as follows:

# Coding=utf-8
"B.py files under the Import dir directory"

Import Dir.b

Print Dir.b.name

Perform a.py error

The hint could not find the name of this module DIR.B. By looking for an official document, you'll need to declare a __init__.py file in the directory, even if the file is empty, to include the file under the directory. Of course, this file can also be a bit of initial data.

So under Dir, create a new __init__.py file with the following directory structure:

Re-execute a.py, everything ok!

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.