Python package Creation (__init__.py)

Source: Internet
Author: User

Tag: INI code mod call LNL cannot module Python3 nbsp

How to make a package of your own:
First, you need to create a folder, use it as Top-level package, within this folder we can define the various subdocuments
folder with a. py file as individual sub-packages and modules
Note:Under each package folder, you need to have a __init__.pyFile, this file can be empty for example: we create a folder test as the top-level Package folder, add __init__.py in test top.py
[[email protected] pythoncode]$ ls test__init__.py  __pycache__  top.py[[email protected] test]$ cat top.py def Top ():    print ("top!")    Return
Next, we create a test.py file at the same directory level as test and import the test package into it
and reference the function top in the module top contained in the test package
[[email protected] pythoncode]$ cat test.py import TestTest.Top.top () Execute test.py This file, what will we see? is the output "top!"? [Email protected] pythoncode]$ Python3 test.py Traceback (most recent call last):  File ' test.py ', line 2, in <modu Le>    Test.Top.top () attributeerror:module ' Test ' has no attribute ' Top '
We see the above output, suggesting that the module top is not found in Test, what is this for?
The problem is on the __init__.py file, let me take a look at the __init__.py file
[email protected] test]$ cat __init__.py

This is a Empty File。 As we said earlier, each package file needs to contain a __init__.py file, only
There it is, this a folder can be recognized as a package, otherwise it is just a folder
And in the above error message we see that the error in the second line, proof Test This package was successfully imported, but
The second line went wrong, and I couldn't find the Top module in Test .。 But we've put the top.py file in test.
Why can't I find it in this folder? The reason is still on the __init__.py file. Although the Test is in
__init__.py file, but just so we can only make Test be recognized as a package, and cannot reference
the Internal module。 We need Import the Top module first in the __init__.py fileTo import the test package externally
When using these modules, let's modify the __init__.py file and then execute test.py to see the results:
[Email protected] test]$ vim __init__.py [[email protected] test]$ cat __init__.py from Test import top[[email protected] test]$ CD. /[[email protected] pythoncode]$ python3 test.py top!

As we have imagined, the output of "top!"

Python package Creation (__init__.py)

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.