특정 problem으로 해결할 수 있었기 때문에 저는 오늘날 대부분의 사람들이 site.py()의 작동 방식을 파악했습니다. 내가 이해하지 못하는 점이있다.python : lib/site-packages/site.py와 lib/site.py 사이의 상호 작용
필자가 아는 한, 파이썬이로드 될 때, 먼저 lib/python2.7/site-packages/site.py
이 실행됩니다. PYTHONPATH
이상이고 lib/python2.7/site.py
을 검색하여 가져옵니다. 이 파일은 addsitedir
메서드를 가지고 있으며, sys.path
에 대한 경로를 추가 할뿐만 아니라 거기에 존재하는 *.pth
개의 파일도 처리합니다. 이 시점에서 main()
은 lib/python2.7/site.py
이고, addsitedir
은 사이트 패키지 및 사용자 사이트 패키지에서 실행됩니다.
이제 이상한 부분이 있습니다. 이제 우리는 lib/python2.7/site-packages/site.py
으로 돌아가서 pythonpath의 각 경로를 살펴보고 addsitedir
을 실행합니다. 나는 이상한 것을 두 가지 이유로 발견한다.
addsitedir
은lib/python2.7/site-packages
에서 두 번 실행되었다.- 이 (아무것도 두 번 sys.path에 추가 할 수 없습니다) 그 자체로 그렇게 나쁘지 않다, 그러나
lib/python2.7/site.py
는 야심 찬 사용자가usercustomize
모듈을 구현하여sys.path
을 조작 할 수 있도록하는 메커니즘을 것 같다 (헤이, 그것은에서도입니다 docs). 분명히 그런 메커니즘을 구현할 때 사용자가 마지막에 도착했는지 확인하여sys.path
에 추가 된 모든 것을 제어 할 수 있습니다. 그러나 이것은 여기에 해당되지 않습니다. (나는 좌절감을 느꼈습니다.) 대부분lib/python2.7/site-packages
에 대한 두 번째 호출은usercustomize
에서 완료된 모든 것을 무시합니다.
나는 끔찍한 것을 알고 있지만, 수신 한 경로를 인쇄하여 addsitedir
에 print 문을 추가 했으므로 어떤 일이 벌어 지는지 보여줄 수있었습니다. 다음은 처리 된 경로입니다.
/home/user/.local/lib/python2.7/site-packages #lib/python2.7/site.py
/home/user/py/lib/python2.7/site-packages #lib/python2.7/site.py
#This is where your usercustomize runs
#Followin calls are from lib/python2.7/site-packages/site.py
/home/user/py/lib/python2.7/site-packages/numpy-1.9.0-py2.7-linux-x86_64.egg
/home/user/Develop/Python/myproject
/home/user/lmfit-0.7.2
/home/user/py/lib/python2.7/site-packages #NOTE: this runs a second time
그래서 내가 뭘 물어 보는거야? :)
A. 이유는 사이트 패키지에 대한 두 번째 호출이 필요한 이유입니다.
B. usercustomize
은 실제로 구현으로 인한 것일까 요? 이것을 고려하면 어떻게 을 구현하면 sys.path (이론적으로)에서 경로를 제거 할 수 있습니까?
요청 디버그 출력 :
:genie39:~ ;-) python2.7 -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /home/user/py/lib/python2.7/site-packages/site.pyc matches /home/user/py/lib/python2.7/site-packages/site.py
import site # precompiled from /home/user/py/lib/python2.7/site-packages/site.pyc
# /home/user/py/lib/python2.7/os.pyc matches /home/user/py/lib/python2.7/os.py
import os # precompiled from /home/user/py/lib/python2.7/os.pyc
import errno # builtin
import posix # builtin
# /home/user/py/lib/python2.7/posixpath.pyc matches /home/user/py/lib/python2.7/posixpath.py
import posixpath # precompiled from /home/user/py/lib/python2.7/posixpath.pyc
# /home/user/py/lib/python2.7/stat.pyc matches /home/user/py/lib/python2.7/stat.py
import stat # precompiled from /home/user/py/lib/python2.7/stat.pyc
# /home/user/py/lib/python2.7/genericpath.pyc matches /home/user/py/lib/python2.7/genericpath.py
import genericpath # precompiled from /home/user/py/lib/python2.7/genericpath.pyc
# /home/user/py/lib/python2.7/warnings.pyc matches /home/user/py/lib/python2.7/warnings.py
import warnings # precompiled from /home/user/py/lib/python2.7/warnings.pyc
# /home/user/py/lib/python2.7/linecache.pyc matches /home/user/py/lib/python2.7/linecache.py
import linecache # precompiled from /home/user/py/lib/python2.7/linecache.pyc
# /home/user/py/lib/python2.7/types.pyc matches /home/user/py/lib/python2.7/types.py
import types # precompiled from /home/user/py/lib/python2.7/types.pyc
# /home/user/py/lib/python2.7/UserDict.pyc matches /home/user/py/lib/python2.7/UserDict.py
import UserDict # precompiled from /home/user/py/lib/python2.7/UserDict.pyc
# /home/user/py/lib/python2.7/_abcoll.pyc matches /home/user/py/lib/python2.7/_abcoll.py
import _abcoll # precompiled from /home/user/py/lib/python2.7/_abcoll.pyc
# /home/user/py/lib/python2.7/abc.pyc matches /home/user/py/lib/python2.7/abc.py
import abC# precompiled from /home/user/py/lib/python2.7/abc.pyc
# /home/user/py/lib/python2.7/_weakrefset.pyc matches /home/user/py/lib/python2.7/_weakrefset.py
import _weakrefset # precompiled from /home/user/py/lib/python2.7/_weakrefset.pyc
import _weakref # builtin
# /home/user/py/lib/python2.7/copy_reg.pyc matches /home/user/py/lib/python2.7/copy_reg.py
import copy_reg # precompiled from /home/user/py/lib/python2.7/copy_reg.pyc
import imp # builtin
# /home/user/py/lib/python2.7/site.pyc matches /home/user/py/lib/python2.7/site.py
import site # precompiled from /home/user/py/lib/python2.7/site.pyc
# /home/user/py/lib/python2.7/traceback.pyc matches /home/user/py/lib/python2.7/traceback.py
import traceback # precompiled from /home/user/py/lib/python2.7/traceback.pyc
# /home/user/py/lib/python2.7/sysconfig.pyc matches /home/user/py/lib/python2.7/sysconfig.py
import sysconfig # precompiled from /home/user/py/lib/python2.7/sysconfig.pyc
# /home/user/py/lib/python2.7/re.pyc matches /home/user/py/lib/python2.7/re.py
import re # precompiled from /home/user/py/lib/python2.7/re.pyc
# /home/user/py/lib/python2.7/sre_compile.pyc matches /home/user/py/lib/python2.7/sre_compile.py
import sre_compile # precompiled from /home/user/py/lib/python2.7/sre_compile.pyc
import _sre # builtin
# /home/user/py/lib/python2.7/sre_parse.pyc matches /home/user/py/lib/python2.7/sre_parse.py
import sre_parse # precompiled from /home/user/py/lib/python2.7/sre_parse.pyc
# /home/user/py/lib/python2.7/sre_constants.pyc matches /home/user/py/lib/python2.7/sre_constants.py
import sre_constants # precompiled from /home/user/py/lib/python2.7/sre_constants.pyc
# /home/user/py/lib/python2.7/_sysconfigdata.pyc matches /home/user/py/lib/python2.7/_sysconfigdata.py
import _sysconfigdata # precompiled from /home/user/py/lib/python2.7/_sysconfigdata.pyc
# zipimport: found 604 names in /home/user/py/lib/python2.7/site-packages/pytz-2014.7-py2.7.egg
# zipimport: found 20 names in /home/user/py/lib/python2.7/site-packages/hashlib-20081119-py2.7-linux-x86_64.egg
# zipimport: found 40 names in /home/user/py/lib/python2.7/site-packages/pysqlite-2.6.3-py2.7-linux-x86_64.egg
# zipimport: found 7 names in /home/user/py/lib/python2.7/site-packages/mock-1.0.1-py2.7.egg
import encodings # directory /home/user/py/lib/python2.7/encodings
# /home/user/py/lib/python2.7/encodings/__init__.pyc matches /home/user/py/lib/python2.7/encodings/__init__.py
import encodings # precompiled from /home/user/py/lib/python2.7/encodings/__init__.pyc
# /home/user/py/lib/python2.7/codecs.pyc matches /home/user/py/lib/python2.7/codecs.py
import codecs # precompiled from /home/user/py/lib/python2.7/codecs.pyc
import _codecs # builtin
# /home/user/py/lib/python2.7/encodings/aliases.pyc matches /home/user/py/lib/python2.7/encodings/aliases.py
import encodings.aliases # precompiled from /home/user/py/lib/python2.7/encodings/aliases.pyc
# /home/user/py/lib/python2.7/encodings/utf_8.pyc matches /home/user/py/lib/python2.7/encodings/utf_8.py
import encodings.utf_8 # precompiled from /home/user/py/lib/python2.7/encodings/utf_8.pyc
Python 2.7.8 (default, Sep 7 2014, 12:14:33)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
dlopen("/home/user/py/lib/python2.7/site-packages/readline-6.2.4.1-py2.7-linux-x86_64.egg/readline.so", 2);
import readline # dynamically loaded from /home/user/py/lib/python2.7/site-packages/readline-6.2.4.1-py2.7-linux-x86_64.egg/readline.so
>>>
출력 python -vv
의는 lib/python2.7/site-packages/site.py
파일이 정상적으로로드되지 입니다 here
이 답변은 명확하고 매우 유용합니다. 대부분의 경우,'site-packages/site.py'는 ** setuptools **를 설치 한 것이 었습니다. 커스텀 파이썬 설치에 설치하는 * 옛 방식 * [필수 설정 PYTHONPATH] (http://askbot.org/ko/question/2492/how-to-install-python-easy_install-python-distutils-in-root - 또는 - 비 - 루트 계정 /). 그러나 easy_install [더 이상 필요하지 않습니다] (https://pythonhosted.org/setuptools/easy_install.html#custom-installation-locations). "Python 2.6에서 PEP-370에 의해 도입 된 User 체계에 의해 이러한 [오래된] 방법이 효과적으로 사용되지 않게되었습니다." – cdunn2001