You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python导入wikipedia模块失败的问题求助

Python导入wikipedia模块失败的问题求助

大家好,我最近在跑一个从维基百科下载文章的Python脚本,先后安装了wikipedia和wikipedia-api两个库,但运行时始终提示找不到wikipedia模块,折腾了好一阵还是没解决,想请各位帮忙分析下问题所在。

先给大家看看我的脚本代码:

#!/usr/bin/env python
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
This script downloads a few sample wikipedia articles that can be used for demo or quickstart purposes in conjunction with the solution accelerator.
"""

import argparse
import os

import wikipedia

us_states = [
    "Alaska",
    "California",
    "Washington (state)",
    "Washington_D.C.",
    "New York (state)",
]


def main():
    parser = argparse.ArgumentParser(description="Wikipedia Download Script")
    parser.add_argument(
        "directory",
        help="Directory to download sample wikipedia articles to.",
        default="testdata",
    )
    parser.add_argument(
        "--short-summary",
        help="Retrieve short summary article content.",
        action="store_true",
    )
    parser.add_argument(
        "--num-articles",
        help="Number of wikipedia articles to download. Default=5",
        default=5,
        choices=range(1, 6),
        type=int,
    )
    args = parser.parse_args()
    os.makedirs(args.directory, exist_ok=True)
    for state in us_states[0 : args.num_articles]:
        try:
            title = wikipedia.page(state).title.lower().replace(" ", "_")
            content = (
                wikipedia.page(state).summary
                if args.short_summary
                else wikipedia.page(state).content
            )
            content = content.strip()
            filename = os.path.join(args.directory, f"{title}_wiki_article.txt")
            with open(filename, "w", encoding="utf-8") as f:
                f.write(content)
            print(f"Saving wiki article '{title}' to {filename}")
        except Exception:
            print(f"Error fetching wiki article {title}")


if __name__ == "__main__":
    main()

以下是我排查过程中收集的环境信息:

  1. pip list输出(确认库已安装):
wheel              0.45.1
wikipedia          1.4.0
Wikipedia-API      0.8.1
  1. 运行脚本时的报错信息:
vscode ➜ /graphrag-accelerator/notebooks (main) $ python get-wiki-articles.py 
Traceback (most recent call last):
  File "/graphrag-accelerator/notebooks/get-wiki-articles.py", line 12, in <module>
    import wikipedia
ModuleNotFoundError: No module named 'wikipedia'
  1. pip show wikipedia的输出(查看库安装位置):
vscode ➜ /graphrag-accelerator/notebooks (main) $ pip show wikipedia
Name: wikipedia
Version: 1.4.0
Summary: Wikipedia API for Python
Home-page: https://github.com/goldsmith/Wikipedia
Author: Jonathan Goldsmith
Author-email: jhghank@gmail.com
License: MIT
Location: /home/vscode/.local/lib/python3.10/site-packages
Requires: beautifulsoup4, requests
Required-by: 
  1. 重建虚拟环境后的相关配置:
  • pyvenv.cfg内容:
home = /usr/local/bin
include-system-site-packages = false
version = 3.10.16
  • 终端中which python的输出(确认使用虚拟环境的Python):
$ which python
/graphrag-accelerator/.venv/bin/python
  • Python版本:
python --version
Python 3.10.16
  • 脚本中打印的sys.path:
['/graphrag-accelerator/notebooks', '/graphrag-accelerator/backend', '/graphrag-accelerator/notebooks/$PATH', '/usr/local/lib/python310.zip', '/usr/local/lib/python3.10', '/usr/local/lib/python3.10/lib-dynload', '/graphrag-accelerator/.venv/lib/python3.10/site-packages']
  1. Python环境的截图:
    Python环境详情

我已经尝试过重建虚拟环境、确认库安装状态,但还是无法解决导入问题,希望各位能给我一些排查思路或解决方案,谢谢大家!

备注:内容来源于stack exchange,提问作者Manu Chadha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 09:05:31