我正在尝试让 python 的维基百科库在谷歌云中工作。当我尝试部署 Firebase 函数时,出现“没有名为‘wikipedia’的模块”错误。
[2024-11-20 15:32:31,620] ERROR in app: Exception on /__/functions.yaml [GET]
Traceback (most recent call last):
File "src/routes/functions/venv/lib/python3.11/site-packages/flask/app.py", line 1511, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src/routes/functions/venv/lib/python3.11/site-packages/flask/app.py", line 919, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src/routes/functions/venv/lib/python3.11/site-packages/flask/app.py", line 917, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "src/routes/functions/venv/lib/python3.11/site-packages/flask/app.py", line 902, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src/routes/functions/venv/lib/python3.11/site-packages/firebase_functions/private/serving.py", line 122, in get_functions_yaml
functions = get_functions()
^^^^^^^^^^^^^^^
File "src/routes/functions/venv/lib/python3.11/site-packages/firebase_functions/private/serving.py", line 40, in get_functions
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "src/routes/functions/main.py", line 5, in <module>
from cancer import get_cancer, get_wiki_cancer
File "src/routes/functions/cancer.py", line 2, in <module>
import wikipedia
ModuleNotFoundError: No module named 'wikipedia'
127.0.0.1 - - [20/Nov/2024 15:32:31] "GET /__/functions.yaml HTTP/1.1" 500 -
127.0.0.1 - - [20/Nov/2024 15:32:31] "GET /__/quitquitquit HTTP/1.1" 200 -
/bin/sh: line 1: 50587 Terminated: 15 python3.11 "/src/routes/functions/venv/lib/python3.11/site-packages/firebase_functions/private/serving.py"
Error: Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
当我在本地运行 Flask 服务器时它可以工作。这是我的代码:
import json
import wikipedia
def get_wiki_cancer(db):
print('Reading wikipedia')
#cancers = "TEST CANCERS PAGE"
cancers = wikipedia.page("List_of_cancer_types")
print(cancers.content)
return json.dumps(cancers.content)
这是我的主要 Flask 应用程序代码:
import firebase_admin
from firebase_admin import credentials, firestore
from flask import Flask, request
from firebase_functions import https_fn
from cancer import get_cancer, get_wiki_cancer
from flask_cors import CORS, cross_origin
# Initialize Firebase Admin SDK
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
app = Flask(__name__)
CORS(app, support_credentials=True)
@app.route('/cancers_wiki', methods=['GET'])
@cross_origin(supports_credentials=True)
def get_wiki_cancers():
print("wikipedia test")
cancers = "TESTING"
cancers_content = get_wiki_cancer(db)
return cancers_content
我还将维基百科添加到我的requirements.txt 文件中,并通过执行
pip3 install -r requirements.txt
安装依赖项
欢迎任何帮助。
我成功了。我在 src/routes/functions 目录中运行了以下内容
python3 -m venv venv
source venv/bin/activate && python3.11 -m pip install -r requirements.txt