0

I am having directory structure as

-Practice
 -Connect
    -connect.py -> having function abc
    -__init__.py
 -Pytest
    -__init__.py
    -file.py

file.1 contents

from ..connect.connect import abc
abc()

Getting error as

ValueError: attempted relative import beyond top-level package

I can use import from parent directory as

from connect.connect import abc
abc()

This is working. Need to know why relative import is not working

As people suggested i have added init.py in Practice directory and still getting same error

Sumit
  • 1,793
  • 4
  • 29
  • 50

2 Answers2

0

You need an __init__.py inside the top level Practice folder, so Python knows that it's supposed to be a package in its own right, as opposed to a random folder that happens to contain two packages.

Robin Zigmond
  • 16,251
  • 2
  • 19
  • 31
0

You need an __init__.py in your Practice directory too. Otherwise, Pytest and Connect are separate toplevel packages and not sibling subpackages of the Practice package. Hence the error you're getting.

d.j.yotta
  • 512
  • 2
  • 10