I'm having huge problems trying to understand Python 3 import (I am using Python 3.5). This seems to be a million dollar question, and I know that it was answered everywhere. However, I am not able to really well understand how things should be done; Answers on the Internet vary greatly. So sorry in advance if this is an almost duplicate answer. I would really appreciate links to good reading material.
So, I have the following dummy project:
/my_project/main.py
/my_project/lib/__init__.py
/my project/lib/my_lib.py
If possible, I would like to:
- Be able to run my program as
python3 main.py
, having my_project
as the current working directory. - Do not change
PYTHONPATH
at any time. - Make pylint happy.
- Use proper Python3 import.
main.py
contains:
from .lib.my_lib import foo
if __name__ == '__main__':
foo()
foo
lib/my_lib.py
:
SystemError: Parent module '' not loaded, cannot perform relative import
, :
from lib.my_lib import foo
1) Python3, ? 2) : Unable to import 'lib.my_lib' (import-error)
, , my_project/__init__.py
:
from my_project.lib.my_lib import foo
pylint , : python3 main.py
:
ImportError: No module named 'my_project'
- : python3 -m my_project.main
.
, : , python3 main.py
?
!