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 inside the root directory - this will make the directory to a package.

The project directory - solution 2
 └── project
     └── package

Now, we invoke the 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.

 Y:/new_project>python -m project.package.demo
 __file__=Y:/new_project/project/package/ | __name__=__main__                  | __package__=project.package
 __file__=Y:/new_project/project/       | __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/ successfully (even thought __name__ is __main__).