I have the following project structure:
server/
server.py
__init__.py
sockets/
module.py
__init__.py
I set PYTHONPATH
to one directory above server (for example /home/user/server
contains server, PYTHONPATH
is set /home/user
).
The main file is server.py
; it imports modules:
import sockets
from sockets.module import Module
When I run python3 $PYTHONPATH/server/server.py
directly it works perfectly.
However when I call python3 -m server.server.py
it fails, despite the fact that it is explicitly recommended to avoid Python path hell, but it fails to find the the module, with an ugly message:
/usr/bin/python3: Error while finding spec for 'server.server.py' (<class 'ImportError'>: No module named 'sockets')
Why does the module import fail to import submodules?
How to properly setup sub-packages?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…