为了更容易理解:
第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