0

folder structure

 root/
     fisrt/
          x.py
          y.py
# x.py

from . import y

This will get an error , ImportError: attempted relative import with no known parent package

I have seached a lot of relavtive module import information.

There are other question in stackoverflow talk about import, but I still can not figure out.

net
  • 39
  • 7

1 Answers1

0

They are both in the same directory, so you don't need to use from; just use import y.

Say if you were to move y.py to a new directory labelled example, (see the following file structure example,) you would use from example import y.

x.py
example/
    y.py
KingsDev
  • 654
  • 5
  • 21
  • Using `import y` to import a module in your folder only makes sense if there are no packages involved. If either `root` or `first` in the example are supposed to be packages, then such an implicit relative import only works in Python 2 (which nobody should be using at this point). – Blckknght Aug 09 '22 at 03:32