0

Here's my directory structure:

fire/
    __init__.py
    bls/
        __init__.py
        bls.py      <-- I am here
    iris/
        __init__.py
        iris.py

I use this structure to have a separate git repository for bls and iris.

I used the this example from the documentation which says that from subpackage1/moduleX.py this is a valid import: from ..subpackage2.moduleZ import eggs

However, when I try:

bls.py
from ..iris.iris import *

I get this error: Attempted relative import beyond top-level package.

I've tried various other import statements as well with no success.

What am I doing wrong? Should I restructure my package? If so, how?

chrisxfire
  • 101
  • 8
  • What's your current directory? How are you running the driver script? – Mad Physicist Aug 17 '21 at 16:35
  • Most likely you are in fire/bls, trying to run `python bls.py`. Instead, go up two folders, and run `python -m fire.bls.bls` – Mad Physicist Aug 17 '21 at 16:36
  • If that works, this is a duplicate: https://stackoverflow.com/q/50745094/2988730 – Mad Physicist Aug 17 '21 at 16:36
  • I'm inside bls.py, so fire/bls/bls.py. The import statement from inside bls.py fails. bls.py will be the driver app. – chrisxfire Aug 17 '21 at 16:36
  • What is your current working directory? I'm that sense, you can't be in a file – Mad Physicist Aug 17 '21 at 16:38
  • As you noted, fire/bls/ is my CWD. When I go to freeze package bls.py, it won't work because the import fails, right? So, do I need to move bls.py elsewhere and change the import for that work? – chrisxfire Aug 17 '21 at 16:40
  • You have to respect your own package structure. Your module is not `bls.bls`. It's `fire.bls.bls`. – Mad Physicist Aug 17 '21 at 16:50
  • You need to show the command you're running to have a [mcve] – Mad Physicist Aug 17 '21 at 16:52
  • This has been covered a zillion times on stack overflow. You can't run submodules directly as scripts when using relative imports. – wim Aug 17 '21 at 16:53
  • @MadPhysicist this is why I included the example from the official Python documentation. I realize my module is fire.bls.bls. But, from the documentation, using `from ..subpackage2.moduleZ import` should work when making this import call from `subpackage1/moduleX.py'.` This is, seemingly, identical to what I am trying to do, which does not work. That's what I'm trying to understand. – chrisxfire Aug 18 '21 at 04:17
  • 1
    Your current directory determines the top level of your package structure. You need to run from two folders up and use -m – Mad Physicist Aug 18 '21 at 04:58
  • thank you, much appreciated! – chrisxfire Aug 18 '21 at 15:11

0 Answers0