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
Your
fb_api.py
is in the same directory as__main__.py
so nofacebook
folder exists for your application. Remove it from your import and it should workBecause
__main__.py
is in the same directory. So just importfb_api
. To useFacebook.fb_api
then move main.py to Facebook directory.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 withpython -m facebook
. The-m
flag tells Python you’re running module by its Python name, which in this case is thefacebook
package. A package being run that way has its__main__.py
file run as the main script, which is exactly what you want.Try local import