Python3.7.4导入facebook包出现Invalid Syntax错误求助
facebook in Python 3.7.4 The error you're hitting happens because the facebook package you installed is Python 2-only—it uses outdated syntax that’s no longer valid in Python 3. Here’s how to resolve this:
Root Cause
The line except urllib2.HTTPError, e: is classic Python 2 syntax. In Python 3:
- Exception handling uses
asinstead of a comma (e.g.,except urllib.error.HTTPError as e:) - The
urllib2module was split intourllib.requestandurllib.error, sourllib2doesn’t exist natively anymore.
The original facebook package on PyPI hasn’t been updated for Python 3 compatibility, which is why reinstalling it didn’t fix the issue.
Solution Steps
Uninstall the outdated package:
Run this command in your terminal to remove the Python 2-only version:pip uninstall facebookConfirm the uninstallation when prompted.
Install the Python 3-compatible Facebook SDK:
The official, maintained Facebook SDK for Python is available asfacebook-sdkon PyPI. Install it with:pip install facebook-sdkKeep your existing code:
Thefacebook-sdkpackage uses the same module name, so your original import code works without changes:import requests import facebook
This should eliminate the syntax error, as the facebook-sdk package is fully optimized for Python 3.
内容的提问来源于stack exchange,提问作者Usman Rafiq




