1

I have following file structure:

proj
- __init__.py
- bar.py  # contains constant 'foo', used in consumers of proj

Now I have realised bar.py fits nicely in a subfolder. However I need to make this change without changing the code depending on proj. Specifically, from proj.bar import foo should still work. So I'm changing the file structure to:

proj
- __init__.py
- subfolder
- - bar.py

I have tried to put from .subfolder import bar into __init__.py. However, following error occurs:

import proj
print(proj.bar)  # <module 'proj.subfolder.bar' from '~/proj/subfolder/bar.py'>

from proj.bar import foo  # ModuleNotFoundError: No module named 'proj.bar'

What can I put in __init__.py of proj such that any consumer can still do from proj.bar import foo?

ikamen
  • 3,175
  • 1
  • 25
  • 47
  • Does this answer your question? [Import module from subfolder](https://stackoverflow.com/questions/8953844/import-module-from-subfolder) – msanford Nov 19 '21 at 14:20
  • No it doesn't - I don't have a subfolder with same name as the module. – ikamen Nov 19 '21 at 14:22
  • so you are saying there is no way to do this? what if I'm grouping multiple modules. – ikamen Nov 19 '21 at 14:41
  • Just to make this explicit: I want to group modules, not turn one module in a subpackage. But I hear you, this might not be possible. – ikamen Nov 20 '21 at 12:48

0 Answers0