您的位置:首页 > 编程语言 > Python开发

运行独立python 引用其他目录包

2016-02-23 18:09 681 查看

独立运行的py如何引用其他目录

. 1目录如下(同级目录):

- utils/test_a.py

- utils/test_main.py

解决方案(在sys模块中添加引用模块path):

test_a.py如下:

# -*- coding:utf-8 -*-
# __author__ = 'jakey'

def a():
print 'a'


test_admin.py如下:

# -*- coding:utf -8 -*-
# __author__ = 'jakey'

from os import sys, path
from test_a import a
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))

if __name__ == '__main__':
a()


. 2目录如下(层级目录):

- utils/admin/test_a.py

- utils/admin/init.py

- utils/test_admin.py

- test_a.py如下:

# -*- coding:utf-8 -*-
# __author__ = 'jakey'

def a():
print 'a'


test_admin.py如下:

# -*- coding:utf -8 -*-
# __author__ = 'jakey'

from admin.testing import a

if __name__ == '__main__':
a()


博客来源:  http://blog.csdn.net/juanjuel[/code] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python path