0

I have a folder xyz which contains two files ab.py and cd.py. Im trying to import cd.py in ab.py file.

Below is the folder structure:

/xyz
    ab.py
    cd.py

I have to import cd to ab

when i do

from . import cd

This gives me error as: Attempted relative import beyond top-level package

1 Answers1

0

If I understand correctly. xyz is a directory under your working directory.

In that case try to add the current directory to import path.

>>> import sys
>>> from pathlib import Path
>>> sys.path.append(Path.cwd())

And in ab.py use from xyz import cd

Additional references

Sibling package imports

Relative imports for the billionth time

abhilb
  • 5,639
  • 2
  • 20
  • 26