1

I've checked other answers on SO as well, but they don't seem to be like mine.

So I'm playing around with imports, and I decided to put my module custom.py into a nested folder like this: /Users/alex/Desktop/Learn/mods/onemore/custom.py

My main script file is in the following location: /Users/alex/Desktop/Learn/main.py

So when I do import custom in main.py, I get the error ModuleNotFoundError: No module named 'custom'

This happens despite the fact that /Users/alex/Desktop/Learn/mods/onemore is in PYTHONPATH:

> echo $PYTHONPATH

> /Users/alex/Desktop/Learn/mods/onemore

and also the path is visible in sys.path (the second one):

> ['', '/Users/alex/Desktop/Learn/mods/onemore', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python310.zip', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages']

Why can't I still import the module?

2 Answers 2

1

You can import it with folder name Ex: from onemore import custom

Sign up to request clarification or add additional context in comments.

1 Comment

It definitely works, but I am trying to omit concrete path and folder names. I just want to go with import custom. Is there a way to do this?
1

You need to add an empty __init__.py file at /Users/alex/Desktop/Learn/mods/onemore to call in this manner (as a regular package).

else if it's in the python path, call it as a namespace package (no need for init):

from custom import <func-you-need>

The standard for imports is currently from PEP420.

1 Comment

Honestly, that didn't explain why Python didn't find my module in my folder. Also, placing __init__.py file at /Users/alex/Desktop/Learn/mods/onemore didn't fix the situation with import custom. It still can't see the module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.