NREL / htem-api-examples

Examples of usage of the HTEM DB API
http://htem.nrel.gov
Other
7 stars 5 forks source link

Deprecated urllib function causing AttributeError #22

Open Chaozhuang22 opened 6 months ago

Chaozhuang22 commented 6 months ago

Description

While working through the first example notebook ('1_Basic_Queries.ipynb'), specifically at the execution of the second code block, an AttributeError is encountered, under a python 3 environment.

Steps to Reproduce

  1. Open the first example notebook.

  2. Execute the second code block:

    for lib in Library.search_by_ids([7387,10295,7494,7269]):
        print(lib.properties()[['id','pdac','num','elements']])
  3. Observe the AttributeError indicating the absence of the 'urlopen' attribute in the urllib module.

Expected Behavior

The code block should execute without errors, fetching and displaying the properties of the libraries identified by the given IDs.

Actual Behavior

An AttributeError is thrown, specifically stating:

AttributeError: module 'urllib' has no attribute 'urlopen'

This is traced back to the following line in the library.py file:

response = urllib.urlopen(url)

Possible Solution

The error encountered results from attempting to use urllib.urlopen, which is not available in Python 3. In Python 2.x, urllib.urlopen was used to open a network object denoted by a URL for reading. However, this function has been moved to urllib.request.urlopen in Python 3, as discussed in this StackOverflow post. Thus, to successfully run this code block and subsequent ones that requires sample.py, one need to replace all urllib by urllib.request in the library.py and sample.py files.