https://pulumi.com logo
#python
Title
# python
s

stocky-lion-56153

07/11/2020, 12:43 PM
If I have
Copy code
from . import s3
in
__main___.py
I get an error
Copy code
ImportError: attempted relative import with no known parent package
b

better-actor-92669

07/11/2020, 3:02 PM
Copy code
from s3 import ...
s

stocky-lion-56153

07/11/2020, 4:36 PM
Ah cool. That works, but now vscode can’t figure out what’s going on. Does pulumi cd into the
main:
dir when it runs then?
b

better-actor-92669

07/11/2020, 5:15 PM
No. According to the output of your tree command, you have the s3 file on the same level as your main file, so they are part of the same module. Usually, I just separate those things. For example, you can create a python package
storage
and put your s3 file there. In this case, your imports will be
from storage.s3 import smth
or
import storage.s3
s

stocky-lion-56153

07/11/2020, 5:30 PM
OK, got it now. I was expecting it to work like
python -m infra
but it runs like
python infra
. The former allows relative top-level imports but the latter does not.
Copy code
❯ tree modplay
modplay
├── __init__.py
├── __main__.py
├── bar
│   ├── __init__.py
│   └── baz.py
└── foo.py
Copy code
❯ cat modplay/__main__.py
from .bar import baz
from . import foo
foo.foo()
baz.baz()
Copy code
❯ python -m modplay
foo
baz

~/temp via 🐍 v3.8.3 on ☁️  eu-west-1
❯ python modplay
Traceback (most recent call last):
  File "/Users/adrian/.pyenv/versions/3.8.3/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Users/adrian/.pyenv/versions/3.8.3/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "modplay/__main__.py", line 1, in <module>
    from .bar import baz
ImportError: attempted relative import with no known parent package
Thanks for the suggestion 🙂 I’ll set things up like this going forward
2 Views