Python3 module in the __init__.py of the need to pay attention to the place

Source: Internet
Author: User

The online information about the role of __init__.py is everywhere, and I'm not going to nag about it here.

If you need, please crossing to search.

I've been having this interesting thing since I first started using Python3.

Gossip less, the following is to be introduced in the Python3 in __init__.py some of the slightly different places

The directory structure is as follows:

├──mdl│    __init__ . py│   ├──bar.py│   └──foo.py└──test.py

The contents of the file, from top to bottom, sequentially

mdl/__init__.py:

# Encoding:utf-8  from Import Foo    #python2, 3 are applicable fromimport Bar    #python2, 3 # From mdl.foo import foo    #python2, 3 applies #frommdl.bar import Bar    #python2, 3 applies  #fromfoo import foo    #python3不适用 #frombar import Bar    # Python3 Not available


mdl/bar.py:

class Bar ():     def __init__ (self):         Pass


mdl/foo.py

class Foo ():     def __init__ (self):         Pass

test.py

 from Import Foo  from Import  == Bar ()

You can see the sticker code, you may know what I want to say is what the meaning of it ...

Yes, that's right.

Before we used Python2 (at least after python2.6), we usually put the __init__.py empty (PS: Empty, and python3 can also be common) or directly (as shown)

 from Import Foo  from Import Bar

You can then use the Python test.py normally and happily without any error messages.

But at Python3 (at least after python3.4), we do this.

 from Import Foo  from Import Bar

Running Python test.py found an error, embarrassing ...

' Foo '

I'm not going to tell you anything else, I think everybody's got it.

Recommend that you use this method later (common usage)

 from Import Foo  from Import Bar

Appendix

1, after Python2 to run the program (that is, Python test.py), found

__init____init__. pyc│├──bar.py│├──bar.pyc│├──foo.py│└──foo.pyc└──test.py

(yes, we are very common. PYc appeared)

2, after Python3 to run the program, found

__init____pycache____init__. cpython-34. Pyc││├──bar.cpython- Pyc││└──foo.cpython-34. pyc│├──bar.py│└──foo.py└──test.py

(See the difference ...) )

Python3 module in the __init__.py of the need to pay attention to the place

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.