2016-07-25 7 views
0

lynda에서이 데모 프로그램을 Python 3을 사용하여 테스트하려고합니다. Pycharm을 IDE로 사용하고 있습니다. 이미 요청 패키지를 추가하고 설치했지만 프로그램을 실행하면 정상적으로 실행되고 "프로세스가 종료 코드 0으로 완료되었습니다."라는 메시지가 표시되지만 print 문의 출력이 표시되지 않습니다. 내가 어디로 잘못 가고 있니?모듈 urllib.request가 데이터를 가져 오지 않습니다

import urllib.request # instead of urllib2 like in Python 2.7 
import json 


def printResults(data): 
    # Use the json module to load the string data into a dictionary 
    theJSON = json.loads(data) 

    # now we can access the contents of the JSON like any other Python object 
    if "title" in theJSON["metadata"]: 
     print(theJSON["metadata"]["title"]) 

    # output the number of events, plus the magnitude and each event name 
    count = theJSON["metadata"]["count"]; 
    print(str(count) + " events recorded") 

    # for each event, print the place where it occurred 
    for i in theJSON["features"]: 
     print(i["properties"]["place"]) 

    # print the events that only have a magnitude greater than 4 
    for i in theJSON["features"]: 
     if i["properties"]["mag"] >= 4.0: 
      print("%2.1f" % i["properties"]["mag"], i["properties"]["place"]) 

    # print only the events where at least 1 person reported feeling something 
    print("Events that were felt:") 
    for i in theJSON["features"]: 
     feltReports = i["properties"]["felt"] 
     if feltReports != None: 
      if feltReports > 0: 
       print("%2.1f" % i["properties"]["mag"], i["properties"]["place"], " reported " + str(feltReports) + " times") 

    # Open the URL and read the data 
    urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson" 
    webUrl = urllib.request.urlopen(urlData) 
    print(webUrl.getcode()) 
    if webUrl.getcode() == 200: 
     data = webUrl.read() 
     data = data.decode("utf-8") # in Python 3.x we need to explicitly decode the response to a string 
    # print out our customized results 
     printResults(data) 
    else: 
     print("Received an error from server, cannot retrieve results " + str(webUrl.getcode())) 

답변

0

의도적으로 제외했지만이 스크립트는 가져 오기 및 함수 정의를 넘어서는 코드를 실제로 실행하지 않습니다. 의도적으로 제외하지 않는다고 가정하면 파일 끝에 다음이 필요합니다. "__main__"를 같게 __name__

if __name__ == '__main__': 
    data = "" # your data 
    printResults(data) 

체크 파일이 명시 적으로 실행될 때 너무 코드는 실행이다. 파일에 액세스 할 때 항상 printResults(data) 기능을 실행하려면 (경우, 말, 같은 자사의 다른 모듈에 수입) 당신이 너무 같은 파일의 맨 아래에 그것을 호출 할 수 있습니다

data = "" # your data 
printResults(data) 
+0

오히려 나는 코드를 붙여 넣기를 잊어 버렸다. 모듈을 설치 한 후 IDE를 다시 시작하지 않았습니다. 지금 막 "관리자 권한으로 실행"을 통해 깨닫고 시도했습니다. 이상하게도 지금 일하는 것 같습니다. – dexter87

0

내가 IDE를 다시 시작했다 모듈을 설치 한 후. 지금 막 "관리자 권한으로 실행"을 통해 깨닫고 시도했습니다. 이상하게도 지금은 작동하는 것처럼 보입니다.하지만 재시작하지 않아도 모듈과 그 방법을 감지 할 수 있었기 때문에 일시적인 오류인지 확실하지 않았습니다.

0

당신의 의견은 IDE를 다시 시작해야 pycharm이 새로 설치된 python 패키지를 자동으로 감지하지 못할 수도 있다고 생각합니다. 이 그래서 대답은 해결책을 제시하는 것 같습니다.

SO answer

+0

위의 과정을 통해 정확히 새 패키지를 설치했습니다. 설정에 들어가서 통역관을 보겠습니다. 또한 pycharm이 그것을 감지했다고 생각하는 이유는 모듈 이름 아래에 빨간 선이 없기 때문이며 코드를 작성할 때 모듈에 포함 된 함수를 호출 할 수 있기 때문입니다. 그래서 임시 오류라고 생각하게 만듭니다. 레드 라인이 없기 때문에 모듈을 사용할 수 있었기 때문에 다시 시작하지 않을 것이라고 생각했습니다. 다시 시작되었으므로 조금 혼란 스럽습니다. – dexter87