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

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

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).

Answer*

Draft saved
Draft discarded
Cancel
4
  • Hi, thanks for your reply. I tried both methods unfortunately it still throws an error: ImportError: cannot import name 'talk' Commented Apr 11, 2018 at 1:27
  • My bad. I didn't take a proper look at your directory tree. I have modified my answer. My small suggestion for you: when you build something like this main.py should be in the main directory, so all imports from subdirectory/subpackage will be relative to this script. At your solution, you need to go up over your directory tree and also other people will have a problem to find main.py. Commented Apr 11, 2018 at 7:34
  • thanks for your suggestion. i wanted this main.py to be the script to pull all the data (hence inside the data folder). there would be another app.py at the main directory for the actual app. maybe i should move main.py to the main directory and rename it pull_data.py Commented Apr 11, 2018 at 7:43
  • 1
    Separate data file and logic. data folder should be only for storing data. No Python files inside. Data parser should be inside lib and main.py should in the main directory. This way when you import parser from lib directory into main, path to data directory still be relative to main.py. It's easier to maintain. Commented Apr 11, 2018 at 7:46

lang-py

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