python中 __init__.py的例程
生活随笔
收集整理的這篇文章主要介紹了
python中 __init__.py的例程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
__init__.py一般是為空,用在一個python目錄中,標識該目錄是一個python的模塊包
先上來看一個例子:
1 .: 2 test1 test2 test_init.py 3 4 ./test1: 5 time.py 6 7 ./test2: 8 cpuinfo.py cpuinfo.pyc __init__.py __init__.pyctest_init.py里面的代碼如下:
1 from test2 import cpuinfo2 from test1 import time3time.py里面:
1 import time2 print time.strftime('%Y.%m.%d',time.localtime(time.time()))cpuinfo.py里面:
import platformos = platform.system() print osprint platform.platform() print platform.version()?
執行結果:
Linux Linux-4.15.0-041500-generic-x86_64-with-Ubuntu-16.04-xenial #201802011154 SMP Thu Feb 1 11:55:45 UTC 2018 Traceback (most recent call last):File "test_init.py", line 2, in <module>from test1 import time ImportError: No module named test1
假如把time.py改成:并在其平行的目錄下面加上__init__.py,即可得出:
#import time #print time.time() #print time.strftime('%Y.%m.%d',time.localtime(time.time()))print "hello world"下面的結果:
Linux Linux-4.15.0-041500-generic-x86_64-with-Ubuntu-16.04-xenial #201802011154 SMP Thu Feb 1 11:55:45 UTC 2018 hello world
?
轉載于:https://www.cnblogs.com/dylancao/p/9931662.html
總結
以上是生活随笔為你收集整理的python中 __init__.py的例程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云yum仓库配置
- 下一篇: websocket python爬虫_p