You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python3.7.4导入facebook包出现Invalid Syntax错误求助

Fixing SyntaxError when importing 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 as instead of a comma (e.g., except urllib.error.HTTPError as e:)
  • The urllib2 module was split into urllib.request and urllib.error, so urllib2 doesn’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

  1. Uninstall the outdated package:
    Run this command in your terminal to remove the Python 2-only version:

    pip uninstall facebook
    

    Confirm the uninstallation when prompted.

  2. Install the Python 3-compatible Facebook SDK:
    The official, maintained Facebook SDK for Python is available as facebook-sdk on PyPI. Install it with:

    pip install facebook-sdk
    
  3. Keep your existing code:
    The facebook-sdk package 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

火山引擎 最新活动