Home > Enterprise >  The file is in the same directory but cannot be imported
The file is in the same directory but cannot be imported

Time:06-19

The python version I am using is 3.8.2 I searched a lot and most of the solutions are to use sys.path.append() But it didn't solve the problem for me, if I use from . import players it will say ImportError: attempted relative import with no known parent package

if i use import players it will say ModuleNotFoundError: No module named 'players'

The code I used to fix this:

  1. sys.path.append(".")
  2. sys.path.append(os.getcwd() "\\players.py")
  3. sys.path.append(os.getcwd())

still can't fix this,It's worth mentioning that at some point sys.path.append(os.getcwd() "\\players.py") can run

CodePudding user response:

Make sure that the name of the file is actually players.py. And not for example players.py, players .py (note the spaces). Also check for all other "invisible" Unicode characters that would not necessarily show up.

And make sure that there is no directory players as well.

CodePudding user response:

I deleted the file completely so I couldn't test it, but I ran into this problem again, it's an import order problem, I tried to import attributes.py first and then content.py, there was a problem Check, can't find attributes.py , the problem is solved when their import order is swapped, I can't understand why, but if you encounter such problems please try once (other files are not imported)

  • Related