No known parent package - Solution 2

Let's create a package and use -m option

  • First, create a new directory named new_project
  • Move the project directory to new_project
  • Create a new empty __init__.py inside the root directory - this will make the directory to a package.

The project directory - solution 2
 new_project
 └── project
     ├── __init__.py
     ├── config.py
     └── package
         ├── __init__.py
         └── demo.py

Now, we invoke the demo.py with the -m option. As the following output demonstrate , the python -m option allows modules to be located using the Python module namespace for execution as scripts.

Invoking demo.py
 Y:/new_project>python -m project.package.demo
 __file__=Y:/new_project/project/package/demo.py | __name__=__main__                  | __package__=project.package
 __file__=Y:/new_project/project/config.py       | __name__=project.config            | __package__=project
 The value of config.count is 5

Running the command will also set package information (__package__ variable). Now, the python interpreter can resolve the relative import in project/demos/demo.py successfully (even thought __name__ is __main__).