爲了更容易理解:
第1步:讓我們進入一個目錄,其中都將被列入
$ cd /var/tmp
第2步:現在讓我們做一個class1的。其中有一個類名Class1的一些代碼
$ cat > class1.py <<\EOF
class Class1:
OKBLUE = '\033[94m'
ENDC = '\033[0m'
OK = OKBLUE + "[OK]: " + ENDC
EOF
步驟3 PY文件:現在可以使具有類名稱的Class2一些代碼
$ cat > class2.py <<\EOF
class Class2:
OKBLUE = '\033[94m'
ENDC = '\033[0m'
OK = OKBLUE + "[OK]: " + ENDC
EOF$ touch main.py
步驟4 class2.py文件:現在讓使一個main.py將被執行一次使用的Class1和等級2從2個不同的文件
$ cat main.py
"""this is how we are actually calling class1.py and from that file loading Class1"""
from class1 import Class1
"""this is how we are actually calling class2.py and from that file loading Class2"""
from class2 import Class2
print Class1.OK
print Class2.OK
步驟5:蘋果咬
$ python main.py
will now use class1 and class2
參見回答了[Python的:從一個子目錄中導入文件(http://stackoverflow.com/questions/1260792/python-import-a-file-from-a -subdirectory)。 – Trilarion 2014-03-19 09:55:48
http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#import – 2014-12-17 12:51:14
[我在這裏回答了類似的問題](http://stackoverflow.com/a/39152647/1911412 ) – kchoi 2016-08-25 23:33:09