如何在Python

问题描述 投票:0回答:1
wikipedia-api

,但似乎没有用。

#!/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()
输出
pip list

wheel              0.45.1
wikipedia          1.4.0
Wikipedia-API      0.8.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'
输出

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: [email protected]
License: MIT
Location: /home/vscode/.local/lib/python3.10/site-packages
Requires: beautifulsoup4, requests
Required-by: 

PythonEnv:

enter image description here您安装了两个安装的python的安装是python3.10

,另一个是
python wikipedia wikipedia-api
1个回答
0
投票
时,安装了wikipedia软件包时,安装了它为

python3.10

安装了,而您的
pyenv.cfg
表示正在使用
python3.11

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.