从flask_socketio导入SocketIO,发出2024-03-26T19:36:49.794996719Z:[错误] ModuleNotFoundError:没有名为“flask_socketio”的模块

问题描述 投票:0回答:1

我正在尝试在 azure 上使用我的 app.py 文件。这些文件在本地运行良好,但是当我在 azure 上部署此应用程序并使用requirements.txt 中的所有库时。它给了我这个错误。

from flask_socketio import SocketIO, emit 2024-03-26T19:36:49.794996719Z: [ERROR]  ModuleNotFoundError: No module named 'flask_socketio

虽然flask_socketio已经存在于requirements.txt中。 我该如何解决?

我检查了requirements.txt并且该包存在于那里,但它仍然导致找不到它的错误。我该如何解决它。

python azure flask flask-socketio
1个回答
0
投票

我尝试使用 Flask-SocketIO 部署 Flask 应用程序,没有任何问题

从flask_socketio导入SocketIO,发出 2024-03-26T19:36:49.794996719Z:[错误] ModuleNotFoundError:否 名为“flask_socketio”的模块

为了避免上述错误,请确保安装flask_socketio。

您可以使用pip安装它:

pip install flask-socketio

确保启用虚拟环境以进行部署。

python -m venv venv
venv\Scripts\activate

索引.html:

<!DOCTYPE  html>
<html  lang="en">
<head>
<meta  charset="UTF-8">
<meta  name="viewport"  content="width=device-width, initial-scale=1.0">
<title>Flask-SocketIO</title>
</head>
<body>
<h1>Simple Flask-SocketIO Example</h1>
<div  id="messages"></div>
<input  type="text"  id="messageInput"  placeholder="Enter message">
<button  id="sendMessage">Send Message</button>
<script  src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.2.0/socket.io.js"></script>
<script>
var  socket = io.connect();
socket.connect('http://127.0.0.1:5000/');
socket.on('message', function(data) {
var  messages = document.getElementById('messages');
messages.innerHTML += '<p>' + data + '</p>';
});
document.getElementById('sendMessage').addEventListener('click', function() {
var  messageInput = document.getElementById('messageInput');
var  message = messageInput.value;
socket.emit('message', message);
messageInput.value = '';
});
</script>
</body>
</html>

app.py:

from  flask  import  Flask, render_template
from  flask_socketio  import  SocketIO, emit
app  =  Flask(__name__)
socketio  =  SocketIO(app)
@app.route('/')
def  index():
return  render_template('index.html')
@socketio.on('message')
def  handle_message(message):
print('Received message:', message)
socketio.emit('message', message)
if  __name__  ==  '__main__':
socketio.run(app)

需求.txt:

Flask==3.0.2
Flask-SocketIO==5.3.6

这是本地输出:

enter image description here

对于部署,

在 Visual Studio Code 中选择任务栏上的 Azure 徽标以打开 Azure 窗口。

步骤如下:

1.选择您的 Azure 订阅。

2.然后,选择您创建的网络应用程序

3.右键单击网络应用程序

  1. 在这里您将看到部署到 Web 应用程序选项。

enter image description here

enter image description here

这是部署后的输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.