Skip to main content
Files should be relative imported as per https://docs.python.org/3/reference/import.html#submodules
Source Link

There are 2 main reasons for __init__.py

  1. For convenience: the other users will not need to know your functions' exact location in your package hierarchy (documentation).

    your_package/
      __init__.py
      file1.py
      file2.py
        ...
      fileN.py
    
    # in __init__.py
    from .file1 import *
    from .file2 import *
    ...
    from .fileN import *
    
    # in file1.py
    def add():
        pass
    

    then others can call add() by

     from your_package import add
    

    without knowing file1file1's inside functions, like

     from your_package.file1 import add
    
  2. If you want something to be initialized; for example, logging (which should be put in the top level):

     import logging.config
     logging.config.dictConfig(Your_logging_config)
    

There are 2 main reasons for __init__.py

  1. For convenience: the other users will not need to know your functions' exact location in your package hierarchy (documentation).

    your_package/
      __init__.py
      file1.py
      file2.py
        ...
      fileN.py
    
    # in __init__.py
    from file1 import *
    from file2 import *
    ...
    from fileN import *
    
    # in file1.py
    def add():
        pass
    

    then others can call add() by

     from your_package import add
    

    without knowing file1, like

     from your_package.file1 import add
    
  2. If you want something to be initialized; for example, logging (which should be put in the top level):

     import logging.config
     logging.config.dictConfig(Your_logging_config)
    

There are 2 main reasons for __init__.py

  1. For convenience: the other users will not need to know your functions' exact location in your package hierarchy (documentation).

    your_package/
      __init__.py
      file1.py
      file2.py
        ...
      fileN.py
    
    # in __init__.py
    from .file1 import *
    from .file2 import *
    ...
    from .fileN import *
    
    # in file1.py
    def add():
        pass
    

    then others can call add() by

     from your_package import add
    

    without knowing file1's inside functions, like

     from your_package.file1 import add
    
  2. If you want something to be initialized; for example, logging (which should be put in the top level):

     import logging.config
     logging.config.dictConfig(Your_logging_config)
    

There are 2 main reasons for __init__.py

  1. For convenience: the other users will not need to know your functions' exact location in your package hierarchy (documentation).

    your_package/
      __init__.py
      file1.py
      file2.py
        ...
      fileN.py
    
    # in __init__.py
    from file1 import *
    from file2 import *
    ...
    from fileN import *
    
    # in file1.py
    def add():
        pass
    

    then others can call add() by

     from your_package import add
    

    without knowing file1, like

     from your_package.file1 import add
    
  2. If you want something to be initialized; for example, logging (which should be put in the top level):

     import logging.config
     logging.config.dictConfig(Your_logging_config)
    

There are 2 main reasons for __init__.py

  1. For convenience: the other users will not need to know your functions' exact location in your package hierarchy.

    your_package/
      __init__.py
      file1.py
      file2.py
        ...
      fileN.py
    
    # in __init__.py
    from file1 import *
    from file2 import *
    ...
    from fileN import *
    
    # in file1.py
    def add():
        pass
    

    then others can call add() by

     from your_package import add
    

    without knowing file1, like

     from your_package.file1 import add
    
  2. If you want something to be initialized; for example, logging (which should be put in the top level):

     import logging.config
     logging.config.dictConfig(Your_logging_config)
    

There are 2 main reasons for __init__.py

  1. For convenience: the other users will not need to know your functions' exact location in your package hierarchy (documentation).

    your_package/
      __init__.py
      file1.py
      file2.py
        ...
      fileN.py
    
    # in __init__.py
    from file1 import *
    from file2 import *
    ...
    from fileN import *
    
    # in file1.py
    def add():
        pass
    

    then others can call add() by

     from your_package import add
    

    without knowing file1, like

     from your_package.file1 import add
    
  2. If you want something to be initialized; for example, logging (which should be put in the top level):

     import logging.config
     logging.config.dictConfig(Your_logging_config)
    
Split up first code block to make clearer that it's three separate things (a directory listing and the contents of two files).
Source Link
George Hawkins
  • 38.2k
  • 7
  • 32
  • 45

There are 2 main reasons for __init__.py

  1. For convenience: the other users will not need to know your functions' exact location in your package hierarchy.

     your_package/
       __init__.py
       file1.py/
       file2.py/
         ...
       fileN.py
     
      
    # in __init__.py
     from file1 import *
     from file2 import *
     ...
     from fileN import *
     
      
    # in file1.py
     def add():
         pass
    

    then others can call add() by

     from your_package import add
    

    without knowing file1, like

     from your_package.file1 import add
    
  2. If you want something to be initialized; for example, logging (which should be put in the top level):

     import logging.config
     logging.config.dictConfig(Your_logging_config)
    

There are 2 main reasons for __init__.py

  1. For convenience: the other users will not need to know your functions' exact location in your package hierarchy.

     your_package/
       __init__.py
       file1.py/
       file2.py/
         ...
       fileN.py
     
     # in __init__.py
     from file1 import *
     from file2 import *
     ...
     from fileN import *
     
     # in file1.py
     def add():
         pass
    

    then others can call add() by

     from your_package import add
    

    without knowing file1, like

     from your_package.file1 import add
    
  2. If you want something to be initialized; for example, logging (which should be put in the top level):

     import logging.config
     logging.config.dictConfig(Your_logging_config)
    

There are 2 main reasons for __init__.py

  1. For convenience: the other users will not need to know your functions' exact location in your package hierarchy.

    your_package/
      __init__.py
      file1.py
      file2.py
        ...
      fileN.py
     
    # in __init__.py
    from file1 import *
    from file2 import *
    ...
    from fileN import *
     
    # in file1.py
    def add():
        pass
    

    then others can call add() by

     from your_package import add
    

    without knowing file1, like

     from your_package.file1 import add
    
  2. If you want something to be initialized; for example, logging (which should be put in the top level):

     import logging.config
     logging.config.dictConfig(Your_logging_config)
    
better explanation and grammer
Source Link
Ninjakannon
  • 3.8k
  • 7
  • 54
  • 80
Loading
Source Link
flycee
  • 12.5k
  • 4
  • 20
  • 14
Loading