Circular Import error when trying to use flask-socket library - python

I am trying to use the flask_socketio library, following the documentation: https://python-socketio.readthedocs.io/en/latest/intro.html#what-is-socket-io
I have a file called socketio.py, and tried using their sample code:
from flask import Flask, render_template
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
if __name__ == '__main__':
socketio.run(app)
I get this error when trying to run it through python3 ($ python3 socketio.py)
Traceback (most recent call last):
File "socketio.py", line 2, in <module>
from flask_socketio import SocketIO
File "/usr/local/lib/python3.8/dist-packages/flask_socketio/__init__.py", line 21, in <module>
import socketio
File "socketio.py", line 2, in <module>
from flask_socketio import SocketIO
ImportError: cannot import name 'SocketIO' from partially initialized module 'flask_socketio' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/flask_socketio/__init__.py)
When I tried using flask run to run it , I get this error
Error: While importing "socketio", an ImportError was raised:
Traceback (most recent call last):
File "python3.8/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "socketio.py", line 2, in <module>
from flask_socketio import SocketIO
File "/usr/local/lib/python3.8/dist-packages/flask_socketio/__init__.py", line 22, in <module>
from socketio.exceptions import ConnectionRefusedError # noqa: F401
ModuleNotFoundError: No module named 'socketio.exceptions'; 'socketio' is not a package
I have tried uninstalling socketio and reinstalling and flask-socketio as there seems to be issues if you have both installed, but it still continues to give the same error.

I can see you have named your program socketio.py. This is conflicting with socketio module used by flask_socketio. Rename your filename and try running.

Related

unittesting fails on ModuleNotFound in the file to be tested imports

I am writing a small unittest for my method move(). The application is running but when I do python -m unittest app.test.test_move_items, it fails with the following error:
ImportError: Failed to import test module: test_move
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "/Users/daniel/app/test/test_move.py", line 13, in <module>
from app.move_items import move
File "/Users/daniel/apps/app/move.py", line 29, in <module>
from helpers.calculator import determine_move_amount
ModuleNotFoundError: No module named 'helpers'
If i do python app/move.py, the output is correct.
The folder structure is as follows:
app
helpers
|-calculator.py
move_items.py
test
|-test_move_items.py
move_items.py
from helpers.calculator import determine_move_amount
def move(list_of_items, move_by):
pass
There is init.py file in app and the test directory as well.

ImportError: cannot import name 'util' from partially initialized module 'sqlalchemy' (most likely due to a circular import)

I am using the ChatterBot library to create a chatbot and I have come across this error. I tried to create a chatbot object in my program but I get this compile time error
Traceback (most recent call last):
File "d:\TwitterBot1\Bot1.py", line 123, in <module>
bot = ChatBot('Bot1')
File "D:\Python\lib\site-packages\chatterbot\chatterbot.py", line 34, in __init__
self.storage = utils.initialize_class(storage_adapter, **kwargs)
File "D:\Python\lib\site-packages\chatterbot\utils.py", line 54, in initialize_class
return Class(*args, **kwargs)
File "D:\Python\lib\site-packages\chatterbot\storage\sql_storage.py", line 22, in __init__
from sqlalchemy import create_engine
File "D:\Python\lib\site-packages\sqlalchemy\__init__.py", line 8, in <module>
from . import util as _util # noqa
ImportError: cannot import name 'util' from partially initialized module 'sqlalchemy' (most likely due to a circular import) (D:\Python\lib\site-packages\sqlalchemy\__init__.py)
I looked into the file that is in the error and the issue is with this import line
from . import util as _util # noqa
and when I try running the __init__.py file, I get this error
Traceback (most recent call last):
File "d:\Python\Lib\site-packages\sqlalchemy\__init__.py", line 8, in <module>
from . import util as _util # noqa
ImportError: attempted relative import with no known parent package
all of the other imports in the __init__.py file have some file directive of sorts after the "." following the "from" instruction. I am not sure if this file was missing some directive or what.
Can anyone point me in the direction of a fix?
I figured this out. Turns out that sqlalchemy 1.4(verson of this time) is not compatible with the current version of chatterbot so I deleted and reinstalled sqlalchemy with version 1.2. Then there was a time.clock error in one of the files in sqlalchemy so I replaced that with time.perf_counter() and everything works now

Target WSGI script can't be load as python module

Flask rest API throwing 500 error with 'Target WSGI script can't load as cannot be loaded as Python module' with below logs and in application .wsgi file all looks good.
from flask import Flask, request, g
File "/usr/local/lib/python2.7/site-packages/flask/__init__.py", line 17, in <module>
from werkzeug.exceptions import abort
File "/usr/local/lib/python2.7/site-packages/werkzeug/__init__.py", line 152, in <module>
__import__('werkzeug.exceptions')
File "/usr/local/lib/python2.7/site-packages/werkzeug/exceptions.py", line 71, in <module>
from werkzeug.wrappers import Response
File "/usr/local/lib/python2.7/site-packages/werkzeug/wrappers.py", line 26, in <module>
from werkzeug.http import HTTP_STATUS_CODES, \\
File "/usr/local/lib/python2.7/site-packages/werkzeug/http.py", line 28, in <module>
from urllib.request import parse_http_list as _parse_list_header
ImportError: No module named request
When I debug through werkzeug/http.py file I came across the below code and in python command line from urllib2 import parse_http_list is working fine but in code, it is going to exception block.
from urllib2 import parse_http_list as _parse_list_header
except ImportError: # pragma: no cover
from urllib.request import parse_http_list as _parse_list_header
The module urllib.request is for Python 3. You are using python 2.
From Werkzeug 2.0.0 changelogs:
Drop support for Python 2 and 3.5
Either downgrade Werkzeug or upgrade to latest python 3 release.

ImportError: No module named FlaskApp

I am puzzled by no module named app importerror. When I use name FlaskApp, everything works, when I change name to xyzApp, it does not and I am getting ImportError.
Here is the error code:
Target WSGI script '/var/www/xyz/xyzApp.wsgi' cannot be loaded as Python module.
Exception occurred processing WSGI script '/var/www/xyz/xyzApp.wsgi'.
Traceback (most recent call last):
File "/var/www/xyz/xyzApp.wsgi", line 11, in <module>
from xyzApp import app as application
ImportError: No module named 'xyzApp'
Target WSGI script '/var/www/xyz/xyzApp.wsgi' cannot be loaded as Python module.
Exception occurred processing WSGI script '/var/www/xyz/xyzApp.wsgi'.
Traceback (most recent call last):
File "/var/www/xyz/xyzApp.wsgi", line 11, in <module>
from xyzApp import app as application
ImportError: No module named 'xyzApp'
The Structure of the Flask applications looks as follows:
/var/www/xyz/
|--xyzApp.wsgi
|--/instance/config.py
|--/xyzApp
|--/static
|--/templates
|--__init__.py
When I do application with similar structure but with FlaskApp folders, it works. Why is that?
The structure that works is as follows:
/var/www/FlaskApp/
|--flaskapp.wsgi
|--/FlaskApp
|--/static
|--/templates
|--__init__.py
The content of flaskapp.wsgi file is the same except for xyzApp:
#! /usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp")
from FlaskApp import app as application

Running Flask dev server in Python 3.6 raises ImportError for SocketServer and ForkingMixIn

I am trying to run a basic Flask app using Python 3.6. However, I get an ImportError: cannot import name 'ForkingMixIn'. I don't get this error when running with Python 2.7 or 3.5. How can I run Flask with Python 3.6?
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello, World!"
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\werkzeug\serving.py", line 65, in <module>
from SocketServer import ThreadingMixIn, ForkingMixIn
ImportError: No module named 'SocketServer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\fsk.py", line 9, in <module>
app.run()
File "C:\Python36\lib\site-packages\flask\app.py", line 828, in run
from werkzeug.serving import run_simple
File "C:\Python36\lib\site-packages\werkzeug\serving.py", line 68, in <module>
from socketserver import ThreadingMixIn, ForkingMixIn
ImportError: cannot import name 'ForkingMixIn'
This is fixed as of Werkzeug 0.11.15. Make sure you have installed the latest version of Werkzeug. pip install -U werkzeug.
This is a known issue that was reported to Werkzeug in anticipation of Python 3.6. Until that or another patch is merged and released, Werkzeug's dev server will not run on Python 3.6.
Check if OS can fork before importing ForkingMixIn since Python 3.6 will no longer define that when it is not available on the operating system (python/cpython#aadff9b) and ImportError: cannot import name 'ForkingMixIn' will occur.
In the mean time, you can run your app with an external WSGI server such as Gunicorn.
pip install gunicorn
gunicorn my_app:app
You can wrap your app in the debug middleware if you need the in-page debugger (as long as you only run Gunicorn with one worker).

Categories