Python의 sys.path는 어디에서 초기화됩니까?
Python의 sys.path는 어디에서 초기화됩니까?
UPD : Python은 PYTHONPATH를 참조하기 전에 몇 가지 경로를 추가합니다.
>>> import sys
>>> from pprint import pprint as p
>>> p(sys.path)
['',
'C:\\Python25\\lib\\site-packages\\setuptools-0.6c9-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\orbited-0.7.8-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\morbid-0.8.6.1-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\demjson-1.4-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\stomper-0.2.2-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\uuid-1.30-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\stompservice-0.1.0-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\cherrypy-3.0.1-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\pyorbited-0.2.2-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\flup-1.0.1-py2.5.egg',
'C:\\Python25\\lib\\site-packages\\wsgilog-0.1-py2.5.egg',
'c:\\testdir',
'C:\\Windows\\system32\\python25.zip',
'C:\\Python25\\DLLs',
'C:\\Python25\\lib',
'C:\\Python25\\lib\\plat-win',
'C:\\Python25\\lib\\lib-tk',
'C:\\Python25',
'C:\\Python25\\lib\\site-packages',
'C:\\Python25\\lib\\site-packages\\PIL',
'C:\\Python25\\lib\\site-packages\\win32',
'C:\\Python25\\lib\\site-packages\\win32\\lib',
'C:\\Python25\\lib\\site-packages\\Pythonwin']
내 PYTHONPATH는 다음과 같습니다.
PYTHONPATH=c:\testdir
PYTHONPATH 이전의 경로가 어디에서 왔는지 궁금합니다.
"환경 변수 PYTHONPATH 및 설치 종속 기본값에서 초기화 됨"
-http : //docs.python.org/library/sys.html#sys.path
파이썬은 정말 지능적으로 sys.path
. 설정 방법은 정말 복잡해질 수 있습니다 . 다음 가이드는 파이썬 수치는 무엇을로 사용할에서 때 발생의 평 조합원 파이썬 프로그래머를위한 물을 다운 다소-불완전 다소-잘못, 그러나 희망 유용한 가이드 초기 값 의 sys.path
, sys.executable
, sys.exec_prefix
, 및 sys.prefix
A의 일반 파이썬 설치.
첫째, 파이썬은 운영체제가 말하는 것을 기반으로 파일 시스템에서 실제 물리적 위치를 파악하기 위해 최선을 다합니다. OS가 "python"이 실행 중이라고 만 표시하면 $ PATH에서 자신을 찾습니다. 모든 심볼릭 링크를 해결합니다. 이 작업을 수행하면 찾은 실행 파일의 경로가 sys.executable
, no ifs, ands 또는 buts 의 값으로 사용됩니다 .
다음으로 sys.exec_prefix
및 의 초기 값을 결정합니다 sys.prefix
.
pyvenv.cfg
같은 디렉토리 sys.executable
또는 하나의 디렉토리에 호출 된 파일이 있으면 파이썬은 파일을 찾습니다. 다른 OS는이 파일로 다른 작업을 수행합니다.
One of the values in this config file that python looks for is the configuration option home = <DIRECTORY>
. Python will use this directory instead of the directory containing sys.executable
when it dynamically sets the initial value of sys.prefix
later. If the applocal = true
setting appears in the pyvenv.cfg
file on Windows, but not the home = <DIRECTORY>
setting, then sys.prefix
will be set to the directory containing sys.executable
.
Next, the PYTHONHOME
environment variable is examined. On Linux and Mac, sys.prefix
and sys.exec_prefix
are set to the PYTHONHOME
environment variable, if it exists, superseding any home = <DIRECTORY>
setting in pyvenv.cfg
. On Windows, sys.prefix
and sys.exec_prefix
is set to the PYTHONHOME
environment variable, if it exists, unless a home = <DIRECTORY>
setting is present in pyvenv.cfg
, which is used instead.
Otherwise, these sys.prefix
and sys.exec_prefix
are found by walking backwards from the location of sys.executable
, or the home
directory given by pyvenv.cfg
if any.
If the file lib/python<version>/dyn-load
is found in that directory or any of its parent directories, that directory is set to be to be sys.exec_prefix
on Linux or Mac. If the file lib/python<version>/os.py
is is found in the directory or any of its subdirectories, that directory is set to be sys.prefix
on Linux, Mac, and Windows, with sys.exec_prefix
set to the same value as sys.prefix
on Windows. This entire step is skipped on Windows if applocal = true
is set. Either the directory of sys.executable
is used or, if home
is set in pyvenv.cfg
, that is used instead for the initial value of sys.prefix
.
If it can't find these "landmark" files or sys.prefix
hasn't been found yet, then python sets sys.prefix
to a "fallback" value. Linux and Mac, for example, use pre-compiled defaults as the values of sys.prefix
and sys.exec_prefix
. Windows waits until sys.path
is fully figured out to set a fallback value for sys.prefix
.
Then, (what you've all been waiting for,) python determines the initial values that are to be contained in sys.path
.
- The directory of the script which python is executing is added to
sys.path
. On Windows, this is always the empty string, which tells python to use the full path where the script is located instead. - The contents of PYTHONPATH environment variable, if set, is added to
sys.path
, unless you're on Windows andapplocal
is set to true inpyvenv.cfg
. - The zip file path, which is
<prefix>/lib/python35.zip
on Linux/Mac andos.path.join(os.dirname(sys.executable), "python.zip")
on Windows, is added tosys.path
. - If on Windows and no
applocal = true
was set inpyvenv.cfg
, then the contents of the subkeys of the registry keyHK_CURRENT_USER\Software\Python\PythonCore\<DLLVersion>\PythonPath\
are added, if any. - If on Windows and no
applocal = true
was set inpyvenv.cfg
, andsys.prefix
could not be found, then the core contents of the of the registry keyHK_CURRENT_USER\Software\Python\PythonCore\<DLLVersion>\PythonPath\
is added, if it exists; - If on Windows and no
applocal = true
was set inpyvenv.cfg
, then the contents of the subkeys of the registry keyHK_LOCAL_MACHINE\Software\Python\PythonCore\<DLLVersion>\PythonPath\
are added, if any. - If on Windows and no
applocal = true
was set inpyvenv.cfg
, andsys.prefix
could not be found, then the core contents of the of the registry keyHK_CURRENT_USER\Software\Python\PythonCore\<DLLVersion>\PythonPath\
is added, if it exists; - If on Windows, and PYTHONPATH was not set, the prefix was not found, and no registry keys were present, then the relative compile-time value of PYTHONPATH is added; otherwise, this step is ignored.
- Paths in the compile-time macro PYTHONPATH are added relative to the dynamically-found
sys.prefix
. - On Mac and Linux, the value of
sys.exec_prefix
is added. On Windows, the directory which was used (or would have been used) to search dynamically forsys.prefix
is added.
At this stage on Windows, if no prefix was found, then python will try to determine it by searching all the directories in sys.path
for the landmark files, as it tried to do with the directory of sys.executable
previously, until it finds something. If it doesn't, sys.prefix
is left blank.
Finally, after all this, Python loads the site
module, which adds stuff yet further to sys.path
:
It starts by constructing up to four directories from a head and a tail part. For the head part, it uses
sys.prefix
andsys.exec_prefix
; empty heads are skipped. For the tail part, it uses the empty string and thenlib/site-packages
(on Windows) orlib/pythonX.Y/site-packages
and thenlib/site-python
(on Unix and Macintosh). For each of the distinct head-tail combinations, it sees if it refers to an existing directory, and if so, adds it to sys.path and also inspects the newly added path for configuration files.
참고URL : https://stackoverflow.com/questions/897792/where-is-pythons-sys-path-initialized-from
'developer tip' 카테고리의 다른 글
로드 된 모든 어셈블리를 어떻게 나열합니까? (0) | 2020.08.27 |
---|---|
자바 리플렉션-setAccessible (true)의 영향 (0) | 2020.08.27 |
사용자의 시간대를 무시하고 Date ()가 특정 시간대를 사용하도록하는 방법 (0) | 2020.08.27 |
개인 설정은 MSBuild 프로젝트 파일의 ProjectReference에서 무엇을합니까? (0) | 2020.08.27 |
jQuery : eq () 대 get () (0) | 2020.08.26 |