socketio in python - django

Kindly help me in configuring the socketio in my django module. Am using windows7 OS
File wsgi.py
Sample Code - from socketio import SocketIOServer
Error - Unresolved import:SocketIOServer
Am new to python and Django Frameworks.!

Try this:
pip install socketIO-server

I think what you want is from socketio.server import SocketIOServer

Related

Can't import MigrateCommand from flask_migrate

I made a manage.py file for database migration in a Flask application and I get an error when trying to import MigrateCommand. I'm using PyCharm and I'm sure the package is installed.
Code:
import os
from flask_migrate import Migrate,MigrateCommand
from flask_script import Manager
from app import app, db
app.config.from_object(os.environ['APP_SETTINGS'])
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
Error:
Cannot find reference 'MigrateCommand' in '__init__.py'
Edit:
Like Matteo Pasini answered Flask-Script is no longer supported and the best solution is to use Flask CLI. For anyone wondering what's the equivalent of the manage.py file using Flask CLI I suggest reading this short guide that explains it well:
https://medium.datadriveninvestor.com/migrating-flask-script-to-flask-2-0-cli-4a5eee269139
The MigrateCommand seems to be no longer supported: https://github.com/miguelgrinberg/Flask-Migrate/issues/407
a possible workaround could be to install an older version of Flask-Migrate, such as:
pip install Flask-Migrate==2.6.0
or switch to the Flask CLI
Flask-Script is not supported anymore. Switch to the Flask CLI or downgrade Flask-Migrate to version 2.7.0, which is the last to support Flask-Script.
pip install flask-migrate==2.7.0

Error while deploying Django app on cpanel(shared hosting)

I m new to django. I created a web with dajngo,and successfully deployed it in the server
The python app has been successfully setup and virtual environment has been setup.
but while running the web it gives me "Server Error (500)" I don't know whats the problem.
I think error is in "wsgi.py" file but i'm unable to idenify it.
My wsgi file:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'karan_web.settings')
application = get_wsgi_application()
my "passenger_wsgi.py" file is:
import imp
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'karan_web/wsgi.py')
application = wsgi.application
can someone help me with it;
Sorry for the late answer, I had figured out the as to this just forgot to post it.
As I stated in my question, the actual problem was in passenger_wsgi.py, the django server starts with the wsgi.py and act as the gate way to the Django server.
So whenever a Django project is uploaded on the hosting server, It creates a passenger_wsgi.py file and one default wsgi.py, address of which is provided in passenger_wsgi.py by default.
So we just need to change that address and provide the address of our own wsgi.py in the project
In my case it was
import os
import sys
from karan_web import wsgi
application = wsgi.application
just edit in passenger_wsgi.py the following code.
from karan_web.wsgi import application
You need to check if your code syntax is correct and running properly. If its still doesn't work try to delete and recreate your database in cpanel and check if you have made all necessary migrations, don't forget to restart your python app. If after all these it still doesn't work check if all your files have the correct file permission(766).

Flask-mongoengine: Unable to import MongoEngine From flask-mongoengine

I must be missing something but I look around and couldn't find reference to this issue.
I have the very basic code, as seen in flask-mongoengine documentation.
test.py:
from flask import Flask
from flask_mongoengine import MongoEngine
When I run
python test.py
...
from flask_mongoengine import MongoEngine
ImportError: cannot import name 'MongoEngine'
Module in virtual environment contain (requirements.txt):
click==6.7
Flask==1.0.2
flask-mongoengine==0.9.5
Flask-WTF==0.14.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
mongoengine==0.15.3
pymongo==3.7.1
six==1.11.0
Werkzeug==0.14.1
WTForms==2.2.1
My interpreter is Python 3.6.5
Any help would be appreciated. Thanks.
Since your using a virtual environment did you try opening your editor from your virtual environment?
For example opening the vscode editor from command-line is "code". Go to your virtual environment via the terminal and activate then type "code" at your prompt.
terminal:~path/to/virtual-enviroment$ source bin/activate
(virtual-enviroment)terminal:~path/to/virtual-enviroment$ code
If that doesn't work I, myself, haven't used flask-mongoengine. I was nervous of any issues that would come from the abstraction of it and instead just used Mongoengine with Flask.
I'm assuming you're only using this library for connection management so if you can't solve your issue with flask-mongoengine but are still interested in using mongoengine this was my approach. ~
I would put this in a config file somewhere and import it where appropriate-
from flask import Flask
MONGODB_DB = 'DB_NAME'
MONGODB_HOST = '127.0.0.1' # or whatever your db address
MONGODB_PORT = 27017 # or whatever your port
app = Flask(__name__) # you can import app from config and it will keep its configurations
then I would connect and disconnect from the database within each HTTP request function like this-
from config import MONGO_DB, MONGODB_HOST, MONGODB_PORT
# to connect
db = connect(MONGODB_DB, host=MONGODB_HOST, port=MONGODB_PORT)
# to close connection before any returns
db.close()
Hope this helps.
I had this issue and managed to fix it by deactivating, reinstalling flask-mongoengine and reactivating the venv (all in the Terminal):
deactivate
pip install flask-mongoengine
# Not required but good to check it was properly installed
pip freeze
venv\Scripts\activate
flask run

Import error when running django server

I get the following error when I try to run python manage.py runserver_socketio
ImportError: cannot import name SocketIOServer
When I run python and help("modules") the socket module appears to be there. How can I determine what "SocketIOServer" needs to be changed to in from socket import SocketIOServer?
I am using Python 2.7.
ERROR : "ImportError: cannot import name SocketIOServer"
The error is in the command file
runserver_socketio.py
located in the folder
site-packages/django_socketio/management/commands/
In the line 13 it says:
from socketio import SocketIOServer
Then I fixed it to:
from socketio.server import SocketIOServer
Hope it helps.
Try to install django-socketio (gevent gives a basic system, but for django written a separate add-on) or check access rights

Django: Manage.py runserver no error, but mod_wsgi report cannot import name connection

When testing my django website by python manage.py runserver 0.0.0.0:8000, there is no error.
But if I deploy it to production with apache and mod_wsgi, it reports error cannot import name connection.
I find some other questions like django 1.4 database router - "cannot import name connection" suggesting adding from django.db import connections into settings.py.
But I found adding this import can prevent this error, but it also disables database router.
I think it may due to different running environment between manage.py runserver and WSGI.
This is how my wsgi.py looks like
import os
import sys
sys.path.insert(0,"/home/my/myweb")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mywebsite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
How can I fix this bug?
My python version is 2.7 and django version is 1.4.3
Edit 1:
My OS is CentOS 6.4, on this server:
If I run it using python manage.py runserver 0.0.0.0:8000, no error.
If I remove database router, it can work under WSGI.
But database router and WSGI cannot work together.
Any advice is appreciated.
Edit 2:
Thanks to #Graham Dumpleton, I figured it out by myself.
This is the wsgy.py that works for me.
import sys
sys.path.insert(0, '/home/my/myweb')
from mywebsite import settings
import django.core.management
django.core.management.setup_environ(settings)
utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')
command.validate()
import django.conf
import django.utils
django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Usually caused by using a mix of imports where some imports are via the site package and some don't. You can add:
sys.path.insert(0,"/home/my/myweb/mywebsite")
and that may help.
For a bit of a discussion of the problem see:
http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html