Skip to content

No Module Named when Referencing local packages (folders) #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
EmanuelU opened this issue May 14, 2023 · 4 comments · Fixed by #12
Closed

No Module Named when Referencing local packages (folders) #92

EmanuelU opened this issue May 14, 2023 · 4 comments · Fixed by #12

Comments

@EmanuelU
Copy link

EmanuelU commented May 14, 2023

Just running the main.py locally works.
Referencing a sub folder as a module

tree:

- test
- - __init__.py
- - base.py
- main.py

in main.py:

from test import base
def some_function():
base.testFunc()

if __name__ == "__main__":
some_function()

When deploying the function, or when running emulators locally with firebase emulators:start

ModuleNotFoundError: No module named 'test'

When running python main.py everything works as expected

@EmanuelU
Copy link
Author

Learned something else, it seems it works when referencing the folders within functions.
I can do imports such as "from x import y" directly in the function itself, but not at the top of main.py

@gijswobben
Copy link
Contributor

Adding this to the top of main.py works as a workaround, but it's not ideal:

import sys
from pathlib import Path

sys.path.insert(0, Path(__file__).parent.as_posix())

from test import base
...

@gear11
Copy link

gear11 commented May 24, 2023

Ran into the same issue. Thanks for posting the workaround!

@jelling
Copy link

jelling commented Jun 15, 2023

I'm still seeing the issue even when using the main branch. I'm not a Python expert but looking #12,
it adds this:

sys.path.insert(0, os.getcwd())

But @gijswobben's fix, which does work for me, adds this:

sys.path.insert(0, Path(__file__).parent.as_posix())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants