2017-10-05 8 views
0

저는 pip를 사용하여 파이썬 스크립트를 사용하여 모듈을 설치하려고합니다. 예를 들어,이 스크립트를 Runntingpython의 pip 설치가 이상하게 스크립트에서 작동하지만 python 프롬프트에서 정상적으로 작동하는 이유는 무엇입니까?

#/usr/bin/python2.7 
# I'm the file called `script.py` 

import sys, importlib, pip 
print(sys.version); print(sys.path) # For debugging 
try: 
    importlib.import_module('docopt') 
except ImportError: 
    pip.main(['install', '-U', 'docopt']) 
finally: 
    globals()[pack] = importlib.import_module('docopt') 

: 여기에 script.py의 내용입니다 python2.7 script.py를 사용하는 것은 나에게 제공합니다 관련 인터넷 연결/SSL 또는 뭔가 수송과

그래서
$ python2.7 script.py 
2.7.9 (default, Oct 3 2016, 17:42:24) 
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] 
['/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/site-packages'] 
No module named docopt 
Downloading/unpacking docopt 
    Cannot fetch index base URL https://pypi.python.org/simple/ 
    Could not find any downloads that satisfy the requirement docopt 
Cleaning up... 
No distributions at all found for docopt 
Storing debug log for failure in /root/.pip/pip.log 
Traceback (most recent call last): 
    [...] 
    File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
ImportError: No module named docopt 

문제. 그러나, t 그는 같은 명령 (& 그들을 붙여 복사) 내가 손으로 그들을 입력 파이썬 대화 형 통역, 완벽하게 잘 작동 : 다시

$ python2.7 
Python 2.7.9 (default, Oct 3 2016, 17:42:24) 
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import pip, sys 
>>> print(sys.path) 
['/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/site-packages'] 
>>> pip.main(['install', '-U', 'docopt']) 
Downloading/unpacking docopt 
    Downloading docopt-0.6.2.tar.gz 
    Running setup.py (path:/tmp/pip_build_root/docopt/setup.py) egg_info for package docopt 

Installing collected packages: docopt 
    Running setup.py install for docopt 

Successfully installed docopt 
Cleaning up... 
0 

, 밖으로 나를 weirds 것은, 그것이 대화 형으로 작동합니다. 즉, 쉘에 python2.7을 실행 한 다음 매우 동일한 코드를 직접 입력하십시오. 그러나 스크립트 파일을 실행하면 python2.7 script.py이 표시됩니다. 그래서 한 컴퓨터에서 동일한 컴퓨터에서 같은 통역사는 인터넷에 접속하지 못합니다.

문제 해결 아이디어가 부족합니다. 위의 결과를 얻기 위해 동일한 기계와 사용자를 사용했습니다. 파이썬 시작 파일이 없으므로 명령이 마술처럼 대화식으로 작동하지 않습니다. sys.path의 내용은 두 경우 모두 같습니다. 나는 대리인이 아니야. 스크립트에서 누락 된 부분은 무엇입니까?

답변

0

https://pypi.python.org/simple/

어떤 이유로 파이썬 인터프리터가 인터넷에 액세스 할 수 없음을 나타냅니다 인덱스 기본 URL을 가져올 수 없습니다. 그것에는 많은 이유가있을 수 있습니다. HTTP 프록시를 사용합니까?

+0

아니요, 프록시를 사용하지 않습니다. 이상한 점은 인터프리터에서 작동한다는 것입니다. 즉 쉘에서'python2.7'을 실행 한 다음 직접 같은 코드를 입력하는 것입니다. 그러나, 내가'python2.7 script.py'를 할 때 아닙니다. 그래서 한 컴퓨터에서 동일한 컴퓨터에서 같은 통역사는 인터넷에 접속하지 못합니다. – JCGB

+0

완전히 눈먼 추측 : SELinux를 사용하도록 설정 했습니까? – Guillaume

+0

결코 들어 본 적이 없으므로. '$ sestatus bash : sestatus : command not found' – JCGB