I've referred to several threads and articles including:
- Importing modules from parent folder
- Can't get Python to import from a different folder
- Import Script from a Parent Directory
- PEP 328 -- Imports: Multi-Line and Absolute/Relative
but can't get the desired result.
Say I have a directory called "helloworld":
helloworld
|--__init__.py
|--say_hello.py
|--another_hello
|--__init__.py
|--import_hello.py
This is say_hello.py:
def hello_world():
print("Hello World!")
if __name__ == "__main__":
hello_world()
This is import_hello.py:
from .. import say_hello
say_hello.hello_world()
I am hoping to import say_hello
module wherever I call python /path/to/import_hello.py
without using sys
module.
However, now when I do python /path/to/import_hello.py
, it will return ValueError: attempted relative import beyond top-level package
, and I have no idea why it isn't working.
Even this doesn't work:
from helloworld import say_hello
say_hello.hello_world()
It will give me ModuleNotFoundError: No module named 'helloworld'
.