2011-05-08 9 views
39

subprocess으로 파이썬 스크립트에서 파이썬 스크립트를 실행하고 싶습니다. 각각에 대해 동일한 인터프리터를 사용하고 싶습니다.파이썬 스크립트 내부에서 현재 파이썬 인터프리터 경로를 얻는 방법은 무엇입니까?

나는 VIRTUALENV 사용하고, 그래서 나는 같은 것을 할 싶습니다

subprocess.Popen('%s script.py' % python_bin) 

가 어떻게 python_bin을받을 수 있나요을?

virtualenv 외부에 /usr/bin/python이어야하며 virtualenv에는 /path/to/env/bin/python이어야합니다.

답변

57

인터프리터의 이름은 그냥 확인하고의 변수 sys.executable

+9

이 포함 된 통역 신뢰할 수 없습니다 , 그것을 위해'os .__ file__'을 사용하십시오 (http://stackoverflow.com/a/8187027/14420의 호의) –

+1

사실 그것은'os .__ file__' 위의 하나의 디렉토리입니다 – denfromufa

2

에 저장됩니다

>>> import sys 
>>> sys.executable 
'/usr/bin/python' 
>>> 
7

내가하여 발견

>>> import sys   
>>> help(sys) 
... 

DATA 
    __stderr__ = <open file '<stderr>', mode 'w' at 0x110029140> 
    __stdin__ = <open file '<stdin>', mode 'r' at 0x110029030> 
    __stdout__ = <open file '<stdout>', mode 'w' at 0x1100290b8> 
    api_version = 1013 
    argv = [''] 
    builtin_module_names = ('__builtin__', '__main__', '_ast', '_codecs', ... 
    byteorder = 'big' 
    copyright = 'Copyright (c) 2001-2009 Python Software Foundati...ematis... 
    dont_write_bytecode = False 
    exc_value = TypeError('arg is a built-in module',) 
    exec_prefix = '/usr/bin/../../opt/freeware' 
    executable = '/usr/bin/python_64' 
관련 문제