0

I have problem importing one of my python fails into another python file. In my main.py fail I have:

from authentication import *
from models import *

They are in the same dir, all the files are python files and they are in same directory.

The error I get:

  File "/Users/user/PycharmProjects/e_commerce_api/./main.py", line 4, in <module>
    from authentication import *
  File "/Users/user/PycharmProjects/e_commerce_api/./authentication.py", line 5, in <module>
    from .models import User
ImportError: attempted relative import with no known parent package

I have tried to from .models import *, and from .authentication import * does not work too.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Gsemerdz
  • 1
  • 1
  • You did not provide a [mre] so we can't tell with certainty what the issue is. – mkrieger1 Mar 07 '23 at 21:24
  • @Brian61354270 ah.. I will need some time to follow it along – Gsemerdz Mar 07 '23 at 21:34
  • @mkrieger1 I tried not to paste my whole code in, should I ? – Gsemerdz Mar 07 '23 at 21:34
  • There is no package that contains either `models` or `authentication`; they are installed as top-level modules in a directory that's on your Python search path. – chepner Mar 07 '23 at 21:39
  • The weird thing is that, it used to work and suddenly these errors started popping out – Gsemerdz Mar 07 '23 at 21:39
  • @chepner How would you suggest fixing that, I just want to import the whole files(models, authentication) in my main file and use the functions in there... – Gsemerdz Mar 07 '23 at 21:42
  • 1
    If you are going to use relative imports, you need to package the files appropriately. It bears repeating: you don't import *files*, you import *modules*. You need to understand how the import system *uses* files to define modules before you start trying to use code that hasn't already been properly packaged and installed. – chepner Mar 07 '23 at 21:59
  • @chepner so in models, authentication.py files i need to pack them and then use the so called "relative import" ? – Gsemerdz Mar 07 '23 at 22:03

0 Answers0