[Tutor] Python Error Message
Nirel Leitman
leitman375 at yahoo.com
Tue Jan 26 22:36:58 EST 2021
Thank you. And yes, degrees.py is also a folder name along with small. I was told that file names should have .py at the end of it so that Python can recognize it.
I went ahead and changed the folder back to just degrees before receiving this email. I will try again tomorrow with the forward slashing to see if that helps at all. Also, how to do I create a directory for the small folder?
Thank you again.
> On Jan 26, 2021, at 5:48 PM, Alan Gauld via Tutor <tutor at python.org> wrote:
>
> I'm forwarding to the list so others can comment....
>
>
> I've loaded and run your file and (once I mock up unit.py_)
> it works with no errors until it tries to load data.
> So I'm not seeing the error you are getting which suggests
> its an environment issue.
>
> However, looking back at your original post I noticed
> something odd that I missed first time:
>
> C:\Users\leitm>python C:\Users\leitm\Desktop\AI\degrees.py
> "C:\Users\leitm\Desktop\AI\degrees.py\small"
>
> The argument to degrees.py is a path string.
>
> But the last element of the path is a filename followed
> by \small - is that correct?
>
> Your code is looking for a directory!
>
> I don't think that would cause the missing main module
> error but you should probably fix it!
>
> The other issue there is that I'm not sure how windows
> python will handle that path string with the backslashes.
> You may need to escape them or use forward slashes(which
> Windows can process with no problems inside a string)
>
> Alan G.
>
>
>
>
>
>> On 27/01/2021 01:42, Alan Gauld via Tutor wrote:
>>
>>> On 26/01/2021 17:18, Nirel Leitman wrote:
>>> Here is the main code for degrees.py -- it is very long:
>>>
>>> import csv
>>> import sys
>>> from util import Node, StackFrontier, QueueFrontier
>>> # Maps names to a set of corresponding person_ids
>>> names = {}
>>> # Maps person_ids to a dictionary of: name, birth, movies (a set of movie_ids)
>>> people = {}
>>> # Maps movie_ids to a dictionary of: title, year, stars (a set of person_ids)
>>> movies = {}
>>> def load_data(directory):
>>> """
>>> Load data from CSV files into memory.
>>> """
>>> # Load people
>>> with open(f"{directory}/people.csv", encoding="utf-8") as f:
>>> reader = csv.DictReader(f)
>>> for row in reader:
>>> people[row["id"]] = {
>>> "name": row["name"],
>>> "birth": row["birth"],
>>> "movies": set()
>>> }
>>> if row["name"].lower() not in names:
>>> names[row["name"].lower()] = {row["id"]}
>>> else:
>>> names[row["name"].lower()].add(row["id"])
>>> # Load movies
>>> with open(f"{directory}/movies.csv", encoding="utf-8") as f:
>>> reader = csv.DictReader(f)
>>> for row in reader:
>>> movies[row["id"]] = {
>>> "title": row["title"],
>>> "year": row["year"],
>>> "stars": set()
>>> }
>>> # Load stars
>>> with open(f"{directory}/stars.csv", encoding="utf-8") as f:
>>> reader = csv.DictReader(f)
>>> for row in reader:
>>> try:
>>> people[row["person_id"]]["movies"].add(row["movie_id"])
>>> movies[row["movie_id"]]["stars"].add(row["person_id"])
>>> except KeyError:
>>> pass
>>> def main():
>>> if len(sys.argv) > 2:
>>> sys.exit("Usage: python degrees.py [directory]")
>>> directory = sys.argv[1] if len(sys.argv) == 2 else "large"
>>> # Load data from files into memory
>>> print("Loading data...")
>>> load_data(directory)
>>> print("Data loaded.")
>>> source = person_id_for_name(input("Name: "))
>>> if source is None:
>>> sys.exit("Person not found.")
>>> target = person_id_for_name(input("Name: "))
>>> if target is None:
>>> sys.exit("Person not found.")
>>> path = shortest_path(source, target)
>>> if path is None:
>>> print("Not connected.")
>>> else:
>>> degrees = len(path)
>>> print(f"{degrees} degrees of separation.")
>>> path = [(None, source)] + path
>>> for i in range(degrees):
>>> person1 = people[path[i][1]]["name"]
>>> person2 = people[path[i + 1][1]]["name"]
>>> movie = movies[path[i + 1][0]]["title"]
>>> print(f"{i + 1}: {person1} and {person2} starred in {movie}")
>>> def shortest_path(source, target):
>>> """
>>> Returns the shortest list of (movie_id, person_id) pairs
>>> that connect the source to the target.
>>> If no possible path, returns None.
>>> """
>>> # TODO
>>> raise NotImplementedError
>>> def person_id_for_name(name):
>>> """
>>> Returns the IMDB id for a person's name,
>>> resolving ambiguities as needed.
>>> """
>>> person_ids = list(names.get(name.lower(), set()))
>>> if len(person_ids) == 0:
>>> return None
>>> elif len(person_ids) > 1:
>>> print(f"Which '{name}'?")
>>> for person_id in person_ids:
>>> person = people[person_id]
>>> name = person["name"]
>>> birth = person["birth"]
>>> print(f"ID: {person_id}, Name: {name}, Birth: {birth}")
>>> try:
>>> person_id = input("Intended Person ID: ")
>>> if person_id in person_ids:
>>> return person_id
>>> except ValueError:
>>> pass
>>> return None
>>> else:
>>> return person_ids[0]
>>> def neighbors_for_person(person_id):
>>> """
>>> Returns (movie_id, person_id) pairs for people
>>> who starred with a given person.
>>> """
>>> movie_ids = people[person_id]["movies"]
>>> neighbors = set()
>>> for movie_id in movie_ids:
>>> for person_id in movies[movie_id]["stars"]:
>>> neighbors.add((movie_id, person_id))
>>> return neighbors
>>> if __name__ == "__main__":
>>> main()
>>>
>>> Please let me know how I can proceed from here. Thank you again for
>>> your assistance.
>>>
>>> - Nirel
>>>
>>> On Monday, January 25, 2021, 06:04:41 PM MST, Alan Gauld via Tutor
>>> <tutor at python.org> wrote:
>>>
>>>
>>> On 25/01/2021 20:24, Nirel Leitman via Tutor wrote:
>>>> I keep getting this error message:
>>>> C:\Users\leitm>python C:\Users\leitm\Desktop\AI\degrees.py
>>> "C:\Users\leitm\Desktop\AI\degrees.py\small">
>>> C:\Users\leitm\AppData\Local\Programs\Python\Python39\python.exe:
>>>
>>> The error says:
>>>> can't find '__main__' module in
>>> 'C:\\Users\\leitm\\Desktop\\AI\\degrees.py'
>>>
>>>> Why can't my computer find my folder?
>>>
>>> It evidently can, it is a main module that it says is missing not
>>> that it can't find the file. It's looking in the file for main
>>> and not finding it.
>>>
>>>> What do I need to change for my computer to read this?
>>>
>>> It depends on what is in the file.
>>> Can you show us the main code for degrees.py?
>>>
>>>
>>> --
>>> Alan G
>>> Author of the Learn to Program web site
>>> http://www.alan-g.me.uk/
>>> http://www.amazon.com/author/alan_gauld
>>> Follow my photo-blog on Flickr at:
>>> http://www.flickr.com/photos/alangauldphotos
>>>
>>>
>>>
>>> _______________________________________________
>>> Tutor maillist - Tutor at python.org <mailto:Tutor at python.org>
>>> To unsubscribe or change subscription options:
>>> https://mail.python.org/mailman/listinfo/tutor
>>
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list