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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:02:31