1.1 有2個模塊功能有些聯繫python
receiveMsg.py和sendMsg.py都在msg文件夾裏面。函數
1.二、使用import 文件.模塊的方式導入ui
在桌面建立demo.py文件,並把receiveMsg.py和sendMsg.py使用import文件.模塊的方式導入demo.py文件。url
receive.py模塊spa
def receiveMessage(): print("正在接受消息...")
send.py模塊設計
def sendMessage(): print("正在發送消息...")
demo.py模塊code
import msg.send import msg.receive msg.send.sendMessage() msg.receive.receiveMessage()
運行結果爲:blog
正在發送消息...
正在接受消息...
1.3使用from文件夾import模塊的方式導入ip
demo.py模塊rem
from msg import * send.sendMessage() receive.receiveMessage()
運行結果爲:
Traceback (most recent call last): File "C:\Users\Se7eN_HOU\Desktop\deno.py", line 2, in <module> send.sendMessage() NameError: name 'send' is not defined
這個時候咱們使用msg下面的模塊失敗,提示咱們沒有定義模塊
1.四、在msg文件夾下建立 _ _init_ _.py 文件
1.五、在_ _init_ _.py文件中寫入
__all__ = ["send","receive"]
1.6從新使用from文件夾import模塊的方式導入
from msg import * send.sendMessage() receive.receiveMessage()
運行結果爲:
正在發送消息...
正在接受消息...
總結:
__init__.py模塊
__all__ = ["send","receive"] print("你導入的msg包") def test(): print("這裏是msg包裏面的test")
demo.py模塊
import msg msg.test()
運行結果爲:
你導入的msg包
這裏是msg包裏面的test
假定咱們的包的例子有以下的目錄結構:
A/#包A
__init__.py a1.py sub_B/#包B
__init__.py b1.py b2.py sub_C/#包C
__init__.py c1.py c2.py sub_D/#包D
__init__.py d1.py d2.py
A是最頂層的包,sub_B等是它的子包,咱們能夠這樣導入子包:
import A.sub_B.b1
你也可以使用 from-import 實現不一樣需求的導入
第一種方法是隻導入頂層的子包,而後使用屬性點操做符向下引用子包樹:
from A import sub_B sub_b.b2
此外,咱們能夠還引用更多的子包:
from A.sub_B import b1
事實上,你能夠一直沿子包的樹狀結構導入
在咱們上邊的目錄結構中,咱們能夠發現不少的 __init__.py 文件。這些是初始化模塊,from-import 語句導入子包時須要用到它。 若是沒有用到,他們能夠是空文件。
1.myModule目錄結構體以下:
./ setup.py __init__.py test.py sub_A/
__init__.py a.py sub_B/
__init__.py b.py
2.編輯setup.py文件
py_modules需指明所需包含的py文件
from distutils.core import setup setup(name = "Se7eN_HOU",version = "1.0",description = "Se7eN_HOU's module",author = "Se7eN_HOU",py_modules = ["sub_A.a","sub_B.b"])
3.構建模塊
使用控制檯在setup.py文件的同目錄下執行python setup.py build
C:\Users\Se7eN_HOU\Desktop\myModule>python setup.py build running build running build_py copying sub_A\a.py -> build\lib\sub_A copying sub_B\b.py -> build\lib\sub_B C:\Users\Se7eN_HOU\Desktop\myModule>
構建後目錄結構:
./ setup.py __init__.py test.py sub_A/
__init__.py a.py sub_B/
__init__.py b.py build/ lib/ sub_A/
__init__.py a.py sub_B/
__init__.py b.py
4.生成發佈壓縮包
同目錄下執行python setup.py sdist
C:\Users\Se7eN_HOU\Desktop\myModule>python setup.py sdist running sdist running check warning: check: missing required meta-data: url warning: check: missing meta-data: if 'author' supplied, 'author_email' must be supplied too warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list) warning: sdist: standard file not found: should have one of README, README.txt, README.rst writing manifest file 'MANIFEST' creating Se7eN_HOU-1.0 creating Se7eN_HOU-1.0\sub_A creating Se7eN_HOU-1.0\sub_B making hard links in Se7eN_HOU-1.0... hard linking setup.py -> Se7eN_HOU-1.0 hard linking sub_A\__init__.py -> Se7eN_HOU-1.0\sub_A hard linking sub_A\a.py -> Se7eN_HOU-1.0\sub_A hard linking sub_B\__init__.py -> Se7eN_HOU-1.0\sub_B hard linking sub_B\b.py -> Se7eN_HOU-1.0\sub_B creating dist Creating tar archive removing 'Se7eN_HOU-1.0' (and everything under it) C:\Users\Se7eN_HOU\Desktop\myModule>
打包後,生成最終發佈壓縮包Se7eN_HOU-1.0tar.gz,目錄結構
./ setup.py __init__.py test.py sub_A/
__init__.py a.py sub_B/
__init__.py b.py build/ lib/ sub_A/
__init__.py a.py sub_B/
__init__.py b.py MANIFEST dist/ Se7eN_HOU-1.0.tar.gz
一、安裝的方式
注意:
二、模塊的引入
在程序中,使用from import 便可完成對安裝的模塊使用
from 模塊名 import 模塊名或者*
1. 什麼是循環導⼊
A.py
from B import b print("這是A模塊") def a(): print("hello a") b() a()
B.py
from A import a print("這是B模塊") def b(): print("Hello b") a() b()
運⾏python a.py
Traceback (most recent call last): File "C:\Users\Se7eN_HOU\Desktop\A.py", line 1, in <module>
from B import b File "C:\Users\Se7eN_HOU\Desktop\B.py", line 1, in <module>
from A import a File "C:\Users\Se7eN_HOU\Desktop\A.py", line 1, in <module>
from B import b ImportError: cannot import name 'b' from 'B' (C:\Users\Se7eN_HOU\Desktop\B.py)
像這樣A裏面引用了B,B裏面又引用了A,這樣就叫作循環引用
2. 怎樣避免循環導⼊