0
votes

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
why do you need from . ? Wouldn't import cd suffice? - abhilb
No. It didnt work - Rajeev Srivastava
Which directory are you running python from, and with what command? - lxop

1 Answers

0
votes

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