1

I am trying to use this python program for vehicle routing using OR tools to create a 'data matrix.' The example program is posted at https://developers.google.com/optimization/routing/vrp It is the full program at the very bottom of the page. The program is supposed to take data from GoogleDistance Matrix API and print out a data matrix to use for vehicle routing/ optimization problems, using the adresses entered. But I am getting errors instead. The program seems to run error free if I take out the last lines ( but then, of course the desired data is not printed) I have working API key, and OR tools installed, so that is not the problem.

I get these errors when I run the complete program

Traceback (most recent call last):
  File "C:\Users\shane\exampleprogram.py", line 94, in <module>
    main()
  File "C:\Users\shane\exampleprogram.py", line 91, in main
    distance_matrix = create_distance_matrix(data)
  File "C:\Users\shane\exampleprogram.py", line 46, in create_distance_matrix
    response = send_request(origin_addresses, dest_addresses, API_key)
  File "C:\Users\shane\exampleprogram.py", line 71, in send_request
    jsonResult = urllib.urlopen(request).read()
AttributeError: module 'urllib' has no attribute 'urlopen'

Is there something simple I can modify to make this program work as intended on my computer/ what is going on here? Please note I've run the program exactly as posted on Google's site with the exception of inserting my own API key (which itself works, but am not posting). For instance line 94 is the very bottom of the program that reads "main()" Here's a picture of my code if it also useful. codeimage Any useful suggestions would be appreciate. Thanks.

  • 3
    I suspect this is a python version difference, see: [AttributeError: 'module' object has no attribute 'urlopen'](https://stackoverflow.com/questions/3969726/attributeerror-module-object-has-no-attribute-urlopen) – tobyodavies Jan 04 '20 at 06:34
  • Thanks, I'll try to run it on a different version to see if it helps – adventureman Jan 04 '20 at 07:08
  • Don't know why this worked, but I changed the line in the original program that read jsonResult = urllib.urlopen(request).read() to jsonResult = urllib.request.urlopen(request).read() and it seems to work now. – adventureman Jan 04 '20 at 08:18
  • it worked because you are using python 3, and that is the new location for the urlopen function. – tobyodavies Jan 18 '20 at 12:45

1 Answers1

0

Following worked for me.

  • import urllib.request instead of import urllib
  • In the error occuring line (line 71 in your case) use jsonResult = urllib.request.urlopen(request).read()
Ishan Hettiarachchi
  • 1,426
  • 2
  • 19
  • 31