본문 바로가기
Programming/python

절대 경로로 import 하기

by 유주원 2015. 7. 22.

절대 경로에 있는 파이썬 class를 import하려면 어떻게 해야 할까?


/home/test/define.py 란 python 코드가 아래와 같이 있다고 가정하자.


class TestClass:

    def testFunction(self, flag):

         print "test Function"


/home/test2/main.py 에서 /home/test/define.py에 있는 class를 import 하려면 어떻게 해야 할까?


import sys를 이용해서 쉽게 해결할 수 있다.


import sys

sys.path.append('/home/test')


from define import TestClass


test = TestClass()

test.testFunction('Hello World')