Why do I get NameError: name ... is not defined in python module?

2024/7/8 8:31:23

filename:recom.py

# Returns a distance-based similarity score for person1 and person2
def sim_distance(prefs,person1,person2):
# Get the list of shared_itemssi={}for item in prefs[person1]:if item in prefs[person2]:si[item]=1# if they have no ratings in common, return 0if len(si)==0: return 0# Add up the squares of all the differencessum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2)for item in prefs[person1] if item in prefs[person2]])return 1/(1+sum_of_squares)

Am getting the error ,when i try to do reload(recom)

Traceback (most recent call last):File "", line 1, in NameError: name 'recom' is not defined

Answer

I use python 3.4.3, and I just encountered the same problem. The below solution solved it for me.

When you use reload() you should also use from imp import reload before you use it.

As to getting the Euclidean Distance Score, you can get your answer like:

 from recom import criticsfrom recom import sim_distancesim_distance(critics,'Lisa Rose','Gene Seymour')

The result is: 0.29429805508554946

https://en.xdnf.cn/q/119805.html

Related Q&A

How to form boxes from nearly touching lines

I want to detect corners from a image with boxes, although i created the chessboard edge lines with the EDlines algorithm. Now, I have some problems to join them to create perfect boxes. Could you help…

How to change app.py variable with HTML button?

How do I add or subtract 1 from the variable num ( in flask route) with an HTML button? So when I click the button it change the var to 1 and refresh the page to show the new value @app.route(/) def n…

How to check if time is in the range between two days?

I found some nice examples to check, if a time is in a specific range, like this one:now_time = datetime.datetime.now().time() start = datetime.time(17, 30) end = datetime.time(4, 00) if start <=…

Removing Duplicate Domain URLs From the Text File Using Bash

Text file https://www.google.com/1/ https://www.google.com/2/ https://www.google.com https://www.bing.com https://www.bing.com/2/ https://www.bing.com/3/Expected Output: https://www.google.com/1/ https…

How can I create a race circuit using Cubic Spline?

My problem is im using Cubic Spline but i get this error trying to graph a race circuit raise ValueError("x must be strictly increasing sequence.") ValueError: x must be strictly increasing s…

Why cant python find my module?

Im getting this error every time I type python manage.py runserver in the root server of my Django app. ImportError: No module named utilsI just added a new app to my project called utils by running py…

Seperating the numbers from strings to do the maths and return the string with the results [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management

I want to Protect amy API by using OAuth 2.0 with Azure Active Directory and API Management. I have added my API in API management and Im following this article https://learn.microsoft.com/en-in/azure…

How can filter by list in django

I am trying to filter a queryset by a list I am getting unicode data into format of 1,4,5,6 bycategory = request.GET.getlist(category) print type(category)data = Leads.objects.filter(item_required__id…

Python: get the return code of ant sub-process in windows

I use python to call ant, I want to get the return code of the ant for detect ant error.for example, in cmd.exe, C:\Documents and Settings\Administrator>ant sfsf Buildfile: build.xml does not exist!…