Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

edited tags
Link
yl_low
  • 1.3k
  • 2
  • 18
  • 28
Source Link
yl_low
  • 1.3k
  • 2
  • 18
  • 28

Python submodule imports

I tried reading other similar questions but the answers didn't work for me, like Python submodule imports using __init__.py and module has no attribute

I have a folder structure like so:

python_scripts
├── lib
│ ├── __init__.py # import lib.talk
│ └── talk.py # def sayhello(x): print(x)
│ 
├── src
│ ├── __init__.py # import lib.talk
│ └── data
│ ├── __init__.py # import lib.talk
│ └── main.py # from lib.talk import sayhello
│ sayhello('hi')
│
└── __init__.py # import lib.talk

This throws an error:

Traceback (most recent call last):
 File "main.py", line 1, in <module>
 from lib.talk import sayhello
ModuleNotFoundError: No module named 'lib.talk'

The strange thing is if I simply 'import lib' in main.py there is no error. How should I solve this?

I am using Windows and I would highly prefer to avoid using the sys.path method because we don't want to hardcode the path in (this may be used by other teams in the future).

lang-py

AltStyle によって変換されたページ (->オリジナル) /