Python's Import subdirectory file

Source: Internet
Author: User

Questions:

Under the pre_tab.py file:

Print ("AA")  from Import login1login1 ()

From test.te import login1this sentence in the program introduces the Login1 object (method) in the te.py file in the test directory under the current directorybut it's been an error importerror didn't find test.te this module
Traceback (most recent):   " c:/users/administrator/pycharmprojects/laonanhai/shop_store/pre_tab.py "  in <module> from    import'test.te'

The reason for the lookup is that the __init__.py file was not created under the test directory (it was created by default, but I deleted it originally) 

The __init__.py files is required to make Python treat the directories as containing packages; This was done to prevent directories with a common name, such as string, from unintentionally hiding valid modules That's occur later on the module search path. In the simplest case, __init__.py can just is an empty file, but it can also execute initialization code for the Package or set the __all__ variable, described later.

Slag Google translation: A __init__.py file is required to enable Python to treat the directory as a containing package; This is done to prevent directories with common names (such as strings) from accidentally hiding valid modules that appear later on the module search path.  In the simplest case, __init__.py can be just an empty file, but it can also execute the package initialization code or set the __ALL__ variable, described later. Workaround: Build the __init__ (). py file in the test directory to supplement the views of others;as stated on the official website, this file allows this directory to be included in the current Python package,I understand it's becoming a module,import. This directory can be identified when the current directory is found, and then the import succeeds.

Python's Import subdirectory file

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.