0

So I am building a small flask-based search tool deployed on heroku to check what rankings universities can be found in. For this I am using fuzzywuzzy to go through lists of lists and returning the relevant rank.

@app.route('/results', methods=["POST"])
def results():
    uniname = request.form["Name"]

    with open("QS-2018-ASIA-clean.csv", encoding="ISO-8859-1") as f:
        reader = csv.reader(f)
        rankdata = [r for r in reader]

    with open("THE_Ranking_Asia.csv", encoding="ISO-8859-1") as g:
        reader1 = csv.reader(g)
        rankdata1 = [r for r in reader1]

    hit = process.extractOne(str(uniname), rankdata, scorer=fuzz.token_set_ratio)
    hit1 = process.extractOne(str(uniname), rankdata1, scorer=fuzz.token_set_ratio)

return render_template('results.html', result1=str(hit[0]), result=str(hit[0][0]))

This throws an error which in my heroku app logs looks like this:

 -File "/app/.heroku/python/lib/python3.6/site- packages/fuzzywuzzy/string_processing.py",
 line 19, in replace_non_letters_non_numbers_with_whitespace
 -return regex.sub(" ",a_string) 
 -TypeError: expected string or bytes-like object

This might be because the process.extractOne hit is a list? But the problem does not occur when I try this method in PyCharm. There he nicely prints out what I'm looking for (in this case with uniname = "Tokyo"):

print(hit) # (['14', 'THE UNIVERSITY OF TOKYO '], 100)
print(hit[0]) # ['14', 'THE UNIVERSITY OF TOKYO ']
print(hit[0][0]) # 14

Anyone have any idea why he throws the error in heroku flask app but not in PyCharm?

Based on a comment I tried checking fuzzywuzzy versions locally and in heroku. Doing this in heroku's python console throws the following error:

enter image description here

Thanks a lot!

Uralan
  • 79
  • 1
  • 9
  • Try `print(fuzzywuzzy.__version__)` both locally and on heroku. – Alex Hall Feb 25 '19 at 20:16
  • Thanks @AlexHall I'll give this a try asap! – Uralan Feb 26 '19 at 08:19
  • So, I dont seem to be able to run any version commands, hence I tried pip freeze, this showed the latest (0.17.0) version locally. In heroku's python shell (console) I was unable to figure out a way to check version or do a new pip install... (pip install did not work) any ideas? – Uralan Feb 26 '19 at 20:53
  • Literally `import fuzzywuzzy; print(fuzzywuzzy.__version__)` in python. – Alex Hall Feb 26 '19 at 20:55
  • @AlexHall that's what I tried. I uploaded the attempt and error above. Screenshot is from Heroku's python console. – Uralan Feb 27 '19 at 09:00
  • Well that suggests that you have an old version installed, or something completely wrong. The fact that you can't do a new pip install is the most worrying bit, you're going to need to do that kind of thing all the time. What happens when you try to run various pip commands? – Alex Hall Feb 27 '19 at 09:04
  • So although I was still unable to verify the version in heroku's console, I checked my Requirements.txt file and found out the version was `fuzzywuzzy==0.3.0` which is indeed the older version! I changed it and it works! Thanks a bunch @AlexHall! – Uralan Feb 27 '19 at 19:34

1 Answers1

0

The problem was indeed the version discrepancy for fuzzywuzzy between local and heroku (credit to @AlexHall). Rather than checking, installing or upgrading in Heroku's python console - which did not work - I checked the Requirements.txt file and found out the version mentioned there was:

fuzzywuzzy==0.3.0

Changed this to the latest

fuzzywuzzy==0.17.0

and ran

git add .
git commit -m "req update"
git push heroku master

once more. Works now!

Uralan
  • 79
  • 1
  • 9