0

So I'm trying create a project and can't resolve this issue for some reason.

My directory:

Desktop
|- MyProject
    |- TableInfo
        |- Team.py
        |- User.py

What my User.py looks like:

import hashlib
import uuid

class createUser():
    userId = str(uuid.uuid4())
    def __init__(self, name, password):
        self.userName = name
        hashedPassword = hashlib.sha512(password.encode()+name.encode())
        self.userPassword = hashedPassword.hexdigest()

What my Team.py looks like:

from User import createUser
import uuid 

class Team():
    playerOne = None
    playerTwo = None
    teamId = str(uuid.uuid4())
        
    def addPlayerOne(self, p1):
        self.playerOne = p1
        
    def addPlayerTwo(self, p2):
        self.playerTwo = p2

and when i execute Team.py I get the error of

ModuleNotFoundError: No module named 'User'

I'm annoyed because it was working, then I went to bed, and when I retried this morning it's not working. Can anyone help me resolve this simple issue please?

  • The short version is that Python has no reason to look in the `TableInfo` folder to find `User.py`, unless you start the code from that directory (which tends to break *other* things once you start working with packages). To solve that, use *relative import*; since `Team.py` and `User.py` are in the same folder, you can use that fact no matter what the current working directory is (and depending on how you run the program, that could be something very unexpected). You should try the linked duplicates, and also look up a Python packaging tutorial. – Karl Knechtel Aug 02 '21 at 15:06
  • Wait isn't it just the fact that createUser is defined as "createUser()" with parentheses? – Ryan Fu Aug 02 '21 at 15:07
  • @RyanFu no, that's entirely legal in Python and means the same as the no-parentheses version. But even if that were the problem, it would report an error completely differently. – Karl Knechtel Aug 02 '21 at 15:08

0 Answers0