2

My project tree is as simple as:

test
  ├── __init__.py (has line: "foo = True")
  ├── bar.py (has line: "bar = False")
  └── test.py (has line: "from . import foo; from .bar import bar")

But none of the imports in test.py work. When I run python test.py, it returns: attempted relative import with no known parent package

Did I miss anything here? I am using python 3.7.6

figbar
  • 714
  • 12
  • 35
shelper
  • 10,053
  • 8
  • 41
  • 67

4 Answers4

1

the issue is arising because you are trying to import foo from __init__.py file as from . import foo , but to import from __init__.py file you need to use a parent folder module, ie from test import foo as how it is structured to import from __init__.py.

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • so how can i use something like `from . import something`? according to this article https://realpython.com/absolute-vs-relative-python-imports/, `from . import something` should work if something is defined in __init__.py – shelper Jul 17 '20 at 19:51
  • you can import it as `from __init__ import foo`, why it is failing, maybe related to python version or article being out of date, this may [help](https://docs.python-guide.org/writing/structure/) – sahasrara62 Jul 17 '20 at 20:34
0

Your code is fine, but the test file needs to be imported as part of a package. So I created the following files and directories, exactly how you explained:

.
└── test
    ├── bar.py
    ├── __init__.py
    ├── __pycache__
    └── test.py

From the parent of test, I ran this:

>>> import test
>>> import test.test
>>> import test.bar
>>> 

It worked fine. But if you're doing relative imports, the code you're importing needs to be imported as part of a package, not executed as a standalone script.

Ken Kinder
  • 12,654
  • 6
  • 50
  • 70
  • could you explain "needs to be imported as part of a package"? I am not clear on this – shelper Jul 17 '20 at 19:48
  • The original code/script you run needs to be something that imports the package `test.py` is part of. In other words, the package must already be imported *outside of the test package* by the time `test.py` runs. – Ken Kinder Jul 17 '20 at 19:57
0

If you're going to import files from the same folder you are, just use their file names:

bar.py

bar = False

foo.py

foo = True

test.py

from foo import foo
from bar import bar

print(foo, bar)

Running python test.py returns:

True False
Telmo Trooper
  • 4,993
  • 1
  • 30
  • 35
0

Following the same/similar file structure, I can think of 2 ways to do imports:

  1. Use absolute import if all three files are in the same directory level, so test.py should look like:

    import foo
    import bar
    

    and python test.py should run fine from the test directory.

  2. Move foo.py and bar.py into a new directory, let's call it package, so the structure will be changed now to:

    test/
    ├── package
    │   ├── bar.py
    │   └── foo.py
    └── test.py
    

    And test.py will be:

    from package import bar
    from package import foo
    

    python test.py from the test directory will work.