skip to Main Content

When I call python __main__.py

I receive error:

from facebook.fb_api import FacebookApi
ModuleNotFoundError: No module named ‘facebook’

  # __main__.py

from facebook.fb_api import FacebookApi   

if __name__ == "__main__":
    api = FacebookApi()
    api.start()

Project structure

facebook/
├── cache.py
├── configs.py
├── fb_api.py
├── __init__.py
├── __main__.py
├── parser
│   ├── cfg.py
│   ├── example.json
│   ├── __init__.py
│   ├── models.py
│   ├── run_parser.py
│   └── utils.py
├── __pycache__
│   └── fb_api.cpython-36.pyc
├── request_handler.py
└── services
    ├── case_service.py
    └── __init__.py

4

Answers


  1. Your fb_api.py is in the same directory as __main__.py so no facebook folder exists for your application. Remove it from your import and it should work

    Login or Signup to reply.
  2. Because __main__.py is in the same directory. So just import fb_api. To use Facebook.fb_api then move main.py to Facebook directory.

    Login or Signup to reply.
  3. You need to run your code from a higher level in your directory structure. Rather than running from within the facebook folder, run from its parent folder with python -m facebook. The -m flag tells Python you’re running module by its Python name, which in this case is the facebook package. A package being run that way has its __main__.py file run as the main script, which is exactly what you want.

    Login or Signup to reply.
  4. Try local import

      # __main__.py
    
    from .fb_api import FacebookApi   
    
    if __name__ == "__main__":
        api = FacebookApi()
        api.start()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search