1. 소스 디렉토리 구조
src
│ module_main.py
│ test.py
│ test.pyc
│
└─services
call.py
call.pyc
2. module_main.py
#-*- coding:utf-8 -*-
import os, sys
if __name__ == "__main__":
# 1. 현재 실행 경로에 같이 있는 py 파일의 함수를 가져오는 경우
print 'Example 1.'
name = 'test' # 임포트 py 파일의 확장자를 제외한 파일명
f = getattr(__import__(name), 'func_a') # 파일안의 함수명을 알아야 한다.
f('aaaa') # 변수에 선언된 함수 실행
# 2. 현재 실행 경로에 있는 py 파일을 임포트 하는 경우
print 'Example 2.'
module = __import__(name) # 해당 py 파일을 임포트 하여 변수에 선언 한다.
module.func_a('module_main.py call')
# 3. 다른 경로에 있는 파일을 임포트 하는 경우
print 'Example 3.'
filename = "D:\python_workspace\services\call.py"
directory, module_name = os.path.split(filename)
module_name = os.path.splitext(module_name)[0]
path = list(sys.path)
sys.path.insert(0, directory)
try:
module = __import__(module_name)
finally:
sys.path[:] = path #
module.testcall('module_main.py call') # 해당 py 파일안의 함수를 실행한다.
3. test.py
#-*- coding:utf-8 -*-
print 'test.py call'
def func_a(a):
print 'agrs :' + a
4. call.py
print 'call.py call'
def testcall(name):
print name
'Python' 카테고리의 다른 글
[Python] 명령줄로 실행 결과값 받기 (0) | 2019.01.15 |
---|---|
[Django] eClipse에서 Django 프로젝트 (0) | 2015.11.28 |
[Python] Windows에서 파이썬 개발시 초기 설정 (0) | 2015.11.07 |
[Python] 메일 인증 에러 (0) | 2015.09.01 |
[Python] 빌드 (0) | 2015.09.01 |