2012-09-23 3 views
2

저는 몇 가지 콘솔 스크립트와 함께 제공되는 Python 패키지를 만들고 있습니다. 패키지 배치 방법 (아래 예 참조) 때문에 콘솔 스크립트는 가져 오는 패키지를 보지 못합니다. 파이썬 패키지의 내용에서 작업하는 동안 나는 "빈"의 콘솔 스크립트를 테스트 할 수 있도록Python 콘솔 스크립트를 테스트 중입니다.

- my_package 
    bin/some_script.py 
    my_package/ 
     __init__.py 
     a_module.py 

어떻게 패키지를 구성해야합니다

는 여기에 예제 레이아웃입니까?

답변

1

여기에 약간의 묘기가 있습니다. my_package에서 bin/some_script.py 수입은 개발 디렉토리에서 가져옵니다

python < bin/some_script.py 

.

여기서는 <이 입력 리디렉션으로 작동하는 bash와 같은 종류의 쉘을 사용한다고 가정합니다.

0
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'bin')) 

때문에, __init__.py

import os 
import sys 

sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'bin')) 

import some_script 

some_script.some_function() 
0

내부 저도 같은 문제를 겪고 마침내 나는 나의 스크립트의 처음에이 일을 결국했습니다 그래서 지금

try: 
    # this works after package has been installed using distutils for example 
    import my_package 
except ImportError: 
    # this should work during dev time with the directory layout you describe 
    rootpath = os.path.dirname(os.path.realpath(os.path.join(__file__, "../"))) 
    sys.path.insert(0, rootpath) 
    try: 
     import my_package 
    except ImportError: 
     print("*** my_package is not installed properly. Exiting.") 
     sys.exit() 

그리고 프로젝트 루트에서 콘솔 스크립트를 실행할 수 있습니다.

bin/some_script.py --help 

희망이 도움이됩니다!

관련 문제