0

I am trying to test this demo program from lynda using Python 3. I am using Pycharm as my IDE. I already added and installed the request package, but when I run the program, it runs cleanly and shows a message "Process finished with exit code 0", but does not show any output from print statement. Where am I going wrong ?

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()))
dexter87
  • 85
  • 1
  • 8

3 Answers3

0

Not sure if you left this out on purpose, but this script isn't actually executing any code beyond the imports and function definition. Assuming you didn't leave it out on purpose, you would need the following at the end of your file.

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

The check on __name__ equaling "__main__" is just so your code is only executing when the file is explicitly run. To always run your printResults(data) function when the file is accessed (like, say, if its imported into another module) you could just call it at the bottom of your file like so:

data = "" # your data
printResults(data)
Mattew Whitt
  • 2,194
  • 1
  • 15
  • 19
  • Ohh no I had main... Just forgot to paste it in code. I did not restarted the IDE after installing the module. I just realized and tried it now with "Run as Admin". Strangely seems to work now. – dexter87 Jul 25 '16 at 14:12
0

I had to restart the IDE after installing the module. I just realized and tried it now with "Run as Admin". Strangely seems to work now.But not sure if it was a temp error, since even without restart, it was able to detect the module and its methods.

dexter87
  • 85
  • 1
  • 8
0

Your comments re: having to restart your IDE makes me think that pycharm might not automatically detect newly installed python packages. This SO answer seems to offer a solution.

SO answer

Community
  • 1
  • 1
Mattew Whitt
  • 2,194
  • 1
  • 15
  • 19
  • I installed the new package exactly through the above process. By going into the settings and then the interpreter. Also the reason I think that pycharm did detect it is because there was no red line under the module name and I was also able to call the functions included in the module when writing the code. Which makes me think it was a temp error. Since the red line was not there and module was usable I did not think of restarting it. Restart worked, so am a bit confused. – dexter87 Jul 26 '16 at 00:24