python导入文件-如何导入其他Python文件?
導(dǎo)入python文件的方法很多,都有各自的優(yōu)缺點。
不要倉促地選擇對您有用的第一個導(dǎo)入策略,否則,當(dāng)您發(fā)現(xiàn)代碼基不能滿足您的需要時,您將不得不重寫代碼基。
我將首先解釋最簡單的示例#1,然后我將轉(zhuǎn)向最專業(yè)和最健壯的示例#7。
示例1:使用python解釋器導(dǎo)入python模塊:把這個放在/home/el/foo/fox.py中:def what_does_the_fox_say():
print("vixens cry")
進入python解釋器:el@apollo:/home/el/foo$ pythonPython 2.7.3 (default, Sep 26 2013, 20:03:06) >>> import fox>>> fox.what_does_the_fox_say()vixens cry>>>
通過python解釋器導(dǎo)入Fox,調(diào)用python函數(shù)。what_does_the_fox_say()從fox.py。
示例2,使用execfile或(exec在Python 3中)在執(zhí)行另一個python文件的腳本中:把這個放在/home/el/foo 2/mylib.py中:def moobar():
print("hi")
把這個放在/home/el/foo 2/main.py中:execfile("/home/el/foo2/mylib.py")moobar()
運行文件:el@apollo:/home/el/foo$ python main.py
hi
功能moobar是從mylib.py導(dǎo)入并在main.py中提供的。
例3,使用.進口.。功能:把這個放在/home/el/foo 3/chekov.py中:def question():
print "where are the nuclear wessels?"
把這個放在/home/el/foo 3/main.py中:from chekov import question
question()
這樣運行:el@apollo:/home/el/foo3$ python main.py
where are the nuclear wessels?
如果在chekov.py中定義了其他函數(shù),則它們將不可用,除非import *
例4,導(dǎo)入riaa.py(如果它與導(dǎo)入的文件位置不同)把這個放在/home/el/foo 4/content/riaa.py中:def watchout():
print "computers are transforming into a noose and a yoke for humans"
把這個放在/home/el/foo 4/main.py中:import sys
import os
sys.path.append(os.path.abspath("/home/el/foo4/stuff"))from riaa import *watchout()
運行它:el@apollo:/home/el/foo4$ python main.py
computers are transforming into a noose and a yoke for humans
它從不同的目錄中導(dǎo)入外部文件中的所有內(nèi)容。
示例5,使用os.system("python yourfile.py")import os
os.system("python yourfile.py")
示例6,通過支持python startuphook導(dǎo)入您的文件:
將此代碼放入主目錄中~/.pythonrc.pyclass secretclass:
def secretmessage(cls, myarg):
return myarg + " is if.. up in the sky, the sky"
secretmessage = classmethod( secretmessage )
def skycake(cls):
return "cookie and sky pie people can"t go up and "
skycake = classmethod( skycake )
將此代碼放入main.py(可以在任何地方):import user
msg = "The only way skycake tates good" msg = user.secretclass.secretmessage(msg)msg += user.secretclass.skycake()print(msg + "
have the sky pie! SKYCAKE!")
運行它:$ python main.pyThe only way skycake tates good is if.. up in the sky, the skycookie and sky pie people can"t go up and have the sky pie!
SKYCAKE!
示例7,最健壯:使用裸導(dǎo)入命令導(dǎo)入python中的文件:創(chuàng)建一個新目錄
/home/el/foo5/
創(chuàng)建一個新目錄
/home/el/foo5/herp
創(chuàng)建一個名為__init__.py草皮下:el@apollo:/home/el/foo5/herp$ touch __init__.py
el@apollo:/home/el/foo5/herp$ ls
__init__.py
創(chuàng)建一個新目錄/home/el/foo 5/herp/derp
在脫衣舞下,再做一次__init__.py檔案:el@apollo:/home/el/foo5/herp/derp$ touch __init__.py
el@apollo:/home/el/foo5/herp/derp$ ls
__init__.py
在/home/el/foo 5/herp/derp下面創(chuàng)建一個名為yolo.py把這個放進去:def skycake():
print "SkyCake evolves to stay just beyond the cognitive reach of " +
"the bulk of men. SKYCAKE!!"
在真相的時刻,制作新的文件/home/el/foo5/main.py把這個放進去from herp.derp.yolo import skycake
skycake()
運行它:el@apollo:/home/el/foo5$ python main.pySkyCake evolves to stay just beyond the cognitive reach of the bulk
of men. SKYCAKE!!
空蕩蕩的__init__.py文件與python解釋器通信,開發(fā)人員希望這個目錄是一個重要的包。
總結(jié)
以上是生活随笔為你收集整理的python导入文件-如何导入其他Python文件?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1亿以内素数的个数_算法|找出给定范围的
- 下一篇: MSSQL用户映射,服务器角色