Python3 - how to make absolute imports correctly and make Pylint happy

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_projectas the current working directory.
  • Do not change PYTHONPATHat 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?

!

+4
1

- :

/path/README
/path/requirements.txt
/path/cleverappname/__main__.py
/path/cleverappname/__init__.py
/path/cleverappname/foo.py
/path/cleverappname/bar.py

, - class CleverName(object) __init__.py , class Foo(object) foo.py ( , ).

__main__:

from cleverappname import CleverName
from cleverappname.foo import Foo
from cleverappname.bar import Bar

, script ( , , ): python3 -m clevername

.

0

Source: https://habr.com/ru/post/1691226/


All Articles