python-3.xvue.jsflasksocket.ioubuntu-22.04

Error executing a github code last modified on January 10, 2023


I am making a private messenger using Flask. This Github code is of interest. Executing flask run on command line, Ubuntu 22.04, as instructed on the "README.md" on Github page, the computer returns the following error:

    ImportError: cannot import name 'escape' from 'jinja2' (/home/home/.local/lib/python3.10/site-packages/jinja2/__init__.py)

The "requirements.txt" file installed using "pip3 install -r requirements.txt" is as follows:

Flask_SQLAlchemy==2.4.1
Flask_JWT_Extended==3.24.1
Flask_SocketIO==4.2.1
Flask_RESTful==0.3.8
Flask_Migrate==2.5.3
Flask==1.1.1
Flask_Cors==3.0.8
Werkzeug==0.15.4
SQLAlchemy==1.3.5

Kindly help.

I followed the instructions given here. It did not work.

=================================================================

The above part was solved on 3rd November, 2023, using the answer below by AKX.

==================================================================

Following the instructions on README.md, both the flask run and npm run serve now run fine. The database part is confusing on README.md. My attempt and error are as follows:

$ python3
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from database import init_db
>>> init_db()
Traceback (most recent call last):
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2262, in _wrap_pool_connect
    return fn()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 363, in connect
    return _ConnectionFairy._checkout(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
    rec = pool._do_get()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/impl.py", line 138, in _do_get
    with util.safe_reraise():
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 154, in reraise
    raise value
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
    return self._create_connection()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
    return _ConnectionRecord(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
    self.__connect(first_connect_check=True)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
    connection = pool._invoke_creator(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 451, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/home/home/.local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  password authentication failed for user "postgres"
connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  password authentication failed for user "postgres"


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/home/Documents/doctor_jha/flask/FlaskChat/api/database.py", line 15, in init_db
    Base.metadata.create_all(bind=engine)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/sql/schema.py", line 4286, in create_all
    bind._run_visitor(
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2032, in _run_visitor
    with self._optional_conn_ctx_manager(connection) as conn:
  File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2024, in _optional_conn_ctx_manager
    with self._contextual_connect() as conn:
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2226, in _contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2265, in _wrap_pool_connect
    Connection._handle_dbapi_exception_noconnection(
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1536, in _handle_dbapi_exception_noconnection
    util.raise_from_cause(sqlalchemy_exception, exc_info)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 399, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
    raise value.with_traceback(tb)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2262, in _wrap_pool_connect
    return fn()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 363, in connect
    return _ConnectionFairy._checkout(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
    rec = pool._do_get()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/impl.py", line 138, in _do_get
    with util.safe_reraise():
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 154, in reraise
    raise value
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
    return self._create_connection()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
    return _ConnectionRecord(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
    self.__connect(first_connect_check=True)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
    connection = pool._invoke_creator(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 451, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/home/home/.local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  password authentication failed for user "postgres"
connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  password authentication failed for user "postgres"

(Background on this error at: http://sqlalche.me/e/e3q8)
>>>

I followed the instructions here and here, but didn't work.

Kindly help.


Solution

  • That repository does not properly lock transitive dependencies, so you've ended up installing a [too-recent version of Jinja2 that doesn't export escape](https://jinja.palletsprojects.com/en/3.1.x/changes/#version-3-1-0, https://github.com/pallets/jinja/pull/1544).

    Install an older version of Jinja, and a compatible version of its dependency MarkupSafe too:

    pip install "jinja2<3.0" "markupsafe<2.0"