I'm trying to my app.py file on azure. The files runs locally fine but when I deploy this app on azure with all libraries in requirements.txt. it gives me this error.

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

While flask_socketio already present in requirements.txt. How i can solve it?

I checked the requirements.txt and the package is present there, but it still causing an error that it is not found. How i can solve it.

1

There are 1 best solutions below

0
Sirra Sneha On

I tried deploying a Flask application with Flask-SocketIO without any issues

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

To avoid the above error make sure to install flask_socketio.

You can install it by using pip:

pip install flask-socketio

make sure to enable virtual environment for deployment purpose.

python -m venv venv
venv\Scripts\activate

Index.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)

requirements.txt:

Flask==3.0.2
Flask-SocketIO==5.3.6

Here's the local output:

enter image description here

For deployment,

Select the Azure logo on the taskbar in Visual Studio Code to open the Azure window.

Here are the steps:

1.Select your Azure subscription.

2.Then, select your created web app

3.Right-click on the web app

  1. Here you will see the Deploy to Web Application option.

enter image description here

enter image description here

Here's the output after deployment:

enter image description here