flask db migrate is throwing "AttributeError: can't set attribute" [duplicate] - python-3.x

This question already has answers here:
AttributeError: can't set attribute when connecting to sqlite database with flask-sqlalchemy
(6 answers)
Closed 1 year ago.
I'm following the flask mega tutorial, I'm currently trying to run the "flask db migrate" command but I get some errors and I'm not sure if I'm to stupid to see it but its not referring to any of my files really.
Traceback (most recent call last):
File "/home/moe/cryptowatch/venv/bin/flask", line 8, in <module>
sys.exit(main())
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/flask/cli.py", line 967, in main
cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/flask/cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/flask/cli.py", line 426, in decorator
return __ctx.invoke(f, *args, **kwargs)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/flask_migrate/cli.py", line 91, in migrate
_migrate(directory, message, sql, head, splice, branch_label, version_path,
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/flask_migrate/__init__.py", line 96, in wrapped
f(*args, **kwargs)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/flask_migrate/__init__.py", line 210, in migrate
command.revision(config, message, autogenerate=True, sql=sql,
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/alembic/command.py", line 212, in revision
script_directory.run_env()
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/alembic/script/base.py", line 490, in run_env
util.load_python_file(self.dir, "env.py")
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 97, in load_python_file
module = load_module_py(module_id, path)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/alembic/util/compat.py", line 182, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "migrations/env.py", line 25, in <module>
str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 943, in engine
return self.get_engine()
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 962, in get_engine
return connector.get_engine()
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 555, in get_engine
options = self.get_options(sa_url, echo)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 570, in get_options
self._sa.apply_driver_hacks(self._app, sa_url, options)
File "/home/moe/cryptowatch/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 914, in apply_driver_hacks
sa_url.database = os.path.join(app.root_path, sa_url.database)
AttributeError: can't set attribute

See this thread for more info. If you have version 1.4.0 of sqlalchemy uninstall it and install the previous version.
If your code is correct then that should solve the problem.

Related

AttributeError: 'PathDistribution' object has no attribute 'name'

I am trying to run a simple workflow using celery and using this documentation. I am using chain to sequentially run the tasks, with following workflow
Extract a file, tokenize it and load JSON dumps of sentence tokens of a doc to another (new)file. Iterate the workflow over list of files in a folder
Following is my code:-
folder structure
celery-pipeline/
├── celeryapp.py
├── celeryconfig.py
├── data/
├── output/
└── tasks.py
celeryapp.py
from celery import Celery
app = Celery()
app.config_from_object('celeryconfig')
celeryconfig.py
imports = ('tasks',)
broker_url = 'redis://localhost:6379/0'
result_backend = 'db+postgresql://celery_user:celery_user#127.0.0.1:5432/celery_db'
task_ignore_result = False
task_track_started = True
task_default_queue = 'default'
task_default_rate_limit = '20/s'
task_time_limit = 7200
worker_pool_restarts = True
tasks.py
import os
import json
import spacy
import logging
from datetime import datetime, timedelta
from celeryapp import app
sp = spacy.load('en_core_web_sm')
#app.task(bind=True)
def extract(self, filename):
file_path = os.path.join(os.getcwd(), 'data', filename)
doc = open(file_path).read()
print('Extract called')
return doc
#app.task(bind=True)
def transform_tokenize_doc(self, doc:str):
sentences = []
for sent in sp(doc).sents:
sentences.append(str(sent).strip())
return sentences
#app.task(bind=True)
def load(self, filename, *args):
with open(os.path.join(os.getcwd(), 'output', filename), 'a+') as file:
file.write(json.dumps(args, indent=4))
if __name__ == '__main__':
tasks = []
for filename in os.listdir(os.path.join(os.getcwd(), 'data'))[:10]:
print(f'filename is {filename}')
etl = (extract.s(filename) | transform_tokenize_doc.s() | load.s(filename)).apply_async()
tasks.append(etl)
for task in tasks:
task.get()
On running celery -A tasks worker --loglevel=info inside root folder - celery-pipeline/, I am getting following error:-
Traceback (most recent call last):
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/kombu/utils/objects.py", line 41, in __get__
return obj.__dict__[self.__name__]
KeyError: 'control'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ubuntu/Documents/projects/celery-venv/bin/celery", line 8, in <module>
sys.exit(main())
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/__main__.py", line 15, in main
sys.exit(_main())
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/bin/celery.py", line 213, in main
return celery(auto_envvar_prefix="CELERY")
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/bin/base.py", line 132, in caller
return f(ctx, *args, **kwargs)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/bin/worker.py", line 326, in worker
**kwargs)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/worker/worker.py", line 99, in __init__
self.setup_instance(**self.prepare_args(**kwargs))
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/worker/worker.py", line 139, in setup_instance
self.blueprint.apply(self, **kwargs)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/bootsteps.py", line 211, in apply
step.include(parent)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/bootsteps.py", line 379, in include
inc, ret = self._should_include(parent)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/bootsteps.py", line 335, in _should_include
return True, self.create(parent)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/worker/components.py", line 238, in create
prefetch_multiplier=w.prefetch_multiplier,
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/bootsteps.py", line 331, in instantiate
return instantiate(name, *args, **kwargs)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/utils/imports.py", line 44, in instantiate
return symbol_by_name(name)(*args, **kwargs)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 212, in __init__
self.blueprint.apply(self, **dict(worker_options or {}, **kwargs))
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/bootsteps.py", line 205, in apply
step = S(parent, **kwargs)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/worker/consumer/control.py", line 25, in __init__
self.box = (pidbox.gPidbox if self.is_green else pidbox.Pidbox)(c)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/worker/pidbox.py", line 28, in __init__
self.node = c.app.control.mailbox.Node(
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/kombu/utils/objects.py", line 43, in __get__
value = obj.__dict__[self.__name__] = self.__get(obj)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/app/base.py", line 1230, in control
return instantiate(self.control_cls, app=self)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/utils/imports.py", line 44, in instantiate
return symbol_by_name(name)(*args, **kwargs)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/kombu/utils/imports.py", line 56, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/celery/app/control.py", line 9, in <module>
from kombu.matcher import match
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/kombu/matcher.py", line 132, in <module>
for ep, args in entrypoints('kombu.matchers'):
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/kombu/utils/compat.py", line 93, in entrypoints
for ep in importlib_metadata.entry_points().get(namespace, [])
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/importlib_metadata/__init__.py", line 865, in entry_points
return SelectableGroups.load(eps).select(**params)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/importlib_metadata/__init__.py", line 340, in load
ordered = sorted(eps, key=by_group)
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/importlib_metadata/__init__.py", line 863, in <genexpr>
dist.entry_points for dist in unique(distributions())
File "/home/ubuntu/Documents/projects/celery-venv/lib/python3.6/site-packages/importlib_metadata/_itertools.py", line 16, in unique_everseen
k = key(element)
AttributeError: 'PathDistribution' object has no attribute 'name'
I tried to search the error on stackoverflow but could find not much insight about the error. Would appreciate some hint on it
The problem is probably related to importlib-metadata. Try adding a requirement to your venv to restrict it to an earlier version. In a similar case, importlib-metadata<3.4.0 worked for me.
The next release of spacy (v3.0.6) should fix this problem (at least if it's only related to spacy) by removing importlib-metadata as a requirement.
I came across the same error in spacy v3.0.6 too. importlib-metadata==3.4.0 worked for me.
The error was with importlib-metadata==4.0.0
This exact same issue happened to me with spacy v3.4.1. importlib-metadata==3.4.0 while creating the virtual environment fixed the issue for me as well. Interestingly, my importlib-metadata defaulted to 2.0.0 beforehand, even though my python is version 3.9.10.

Airflow scheduler works normally, fails with -D

I have set up Airflow on an AWS EC2 server with Ubuntu 18.04, Python 3.6.9. The DB backend is a 5.7.26 MySQL, I am using a LocalExecutor. The setup is nothing more than:
apt-get install python3 python3-pip python3-venv libmysqlclient-dev
I install Airflow with pip3 install apache-airflow[mysql]==1.10.9, init DB connection, start the webserver and it works both normally and with -D. The scheduler, however, works only when run in the foreground. Trying to run it as a daemon fails with the following trace:
Traceback (most recent call last):
File "/usr/local/bin/airflow", line 37, in <module> args.func(args)
File "/usr/local/lib/python3.6/dist-packages/airflow/utils/cli.py", line 75, in wrapper return f(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/airflow/bin/cli.py", line 1032, in schedulerjob.run()
File "/usr/local/lib/python3.6/dist-packages/airflow/jobs/base_job.py", line 215, in run session.commit()
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/session.py", line 1036, in commit self.transaction.commit()
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/session.py", line 503, in commitself._prepare_impl()
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/session.py", line 482, in _prepare_impl self.session.flush()
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/session.py", line 2496, in flushself._flush(objects)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/session.py", line 2637, in _flush transaction.rollback(_capture_exception=True)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__exc_value, with_traceback=exc_tb,
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/util/compat.py", line 178, in raise_raise exception
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/session.py", line 2597, in _flush flush_context.execute()
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute rec.execute(self)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute uow,
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/persistence.py", line 213, in save_obj) in _organize_states_for_save(base_mapper, states, uowtransaction):
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/persistence.py", line 374, in _organize_states_for_save base_mapper, uowtransaction, states
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/persistence.py", line 1602, in _connections_for_states connection = uowtransaction.transaction.connection(base_mapper)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/session.py", line 313, in connection return self._connection_for_bind(bind, execution_options)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/session.py", line 420, in _connection_for_bind conn = self._parent._connection_for_bind(bind, execution_options)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/session.py", line 432, in _connection_for_bind conn = bind._contextual_connect()
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py", line 2251, in _contextual_connect self._wrap_pool_connect(self.pool.connect, None),
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py", line 2285, in _wrap_pool_connect return fn()
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/pool/base.py", line 363, in connect return _ConnectionFairy._checkout(self)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/pool/base.py", line 804, in _checkout result = pool._dialect.do_ping(fairy.connection)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 138, in do_pingdbapi_connection.ping(False)
File "/usr/local/lib/python3.6/dist-packages/pymysql/connections.py", line 546, in ping self._execute_command(COMMAND.COM_PING, "")
File "/usr/local/lib/python3.6/dist-packages/pymysql/connections.py", line 771, in _execute_command self._write_bytes(packet)
File "/usr/local/lib/python3.6/dist-packages/pymysql/connections.py", line 711, in _write_bytesself._sock.settimeout(self._write_timeout)
OSError: [Errno 9] Bad file descriptor
Exception ignored in: <function _ConnectionRecord.checkout.<locals>.<lambda> at 0x7f2eb025e268>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/pool/base.py", line 503, in <lambda>
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/pool/base.py", line 702, in _finalize_fairy
File "/usr/lib/python3.6/logging/__init__.py", line 1337, in error
File "/usr/lib/python3.6/logging/__init__.py", line 1444, in _log
File "/usr/lib/python3.6/logging/__init__.py", line 1454, in handle
File "/usr/lib/python3.6/logging/__init__.py", line 1516, in callHandlers
File "/usr/lib/python3.6/logging/__init__.py", line 865, in handle
File "/usr/lib/python3.6/logging/__init__.py", line 1071, in emit
File "/usr/lib/python3.6/logging/__init__.py", line 1061, in _open
NameError: name 'open' is not defined
I am trying to set up a server for semi-production purposes, so this is actually a blocker for me. I would be grateful for any advice.
EDIT
I tried using mysql-connector-python==8.0.18 and the scheduler did run as a daemon, but an attempt to open a dag failed with the following trace:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.6/dist-packages/flask_admin/base.py", line 69, in inner
return self._run_view(f, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/flask_admin/base.py", line 368, in _run_view
return fn(self, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/flask_login/utils.py", line 258, in decorated_view
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/airflow/www/utils.py", line 384, in view_func
return f(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/airflow/www/utils.py", line 290, in wrapper
return f(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/airflow/utils/db.py", line 74, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/airflow/www/views.py", line 1559, in tree
start_date=min_date, end_date=base_date, session=session)
File "/usr/local/lib/python3.6/dist-packages/airflow/utils/db.py", line 70, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/airflow/models/dag.py", line 837, in get_task_instances
tis = tis.order_by(TaskInstance.execution_date).all()
File "/home/ubuntu/.local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3233, in all
return list(self)
File "/home/ubuntu/.local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3389, in __iter__
return self._execute_and_instances(context)
File "/home/ubuntu/.local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3414, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/home/ubuntu/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 982, in execute
return meth(self, multiparams, params)
File "/home/ubuntu/.local/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 293, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/ubuntu/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1101, in _execute_clauseelement
distilled_params,
File "/home/ubuntu/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1250, in _execute_context
e, statement, parameters, cursor, context
File "/home/ubuntu/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1478, in _handle_dbapi_exception
util.reraise(*exc_info)
File "/home/ubuntu/.local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
raise value
File "/home/ubuntu/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
cursor, statement, parameters, context
File "/home/ubuntu/.local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 588, in do_execute
cursor.execute(statement, parameters)
File "/home/ubuntu/.local/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 272, in execute
self._handle_result(result)
File "/home/ubuntu/.local/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 163, in _handle_result
self._handle_resultset()
File "/home/ubuntu/.local/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 651, in _handle_resultset
self._rows = self._cnx.get_rows()[0]
File "/home/ubuntu/.local/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 301, in get_rows
else self._cmysql.fetch_row()
SystemError: <built-in method fetch_row of _mysql_connector.MySQL object at 0x3cdaa10> returned a result with an error set
EDIT 2
What finally did work was using mysqlclient by specifying the SQLAlchemy connection string to begin with mysql+mysqldb. I am not giving this as an answer, because the initial problem persists.

PyTest throws [WinError 87] parameter not found

I am currently working on test automation using Python. I was trying to use Allure which, first, requires Pytest. After installing these two module, I proceeded to call:
pytest --alluredir $OutputPath
But got the [WinError 87] parameter not found message. In order to confirm that Allure was not causing the problem I then tried to only invoke Pytest through the Python interpreter from the command line with:
python -m pytest
But still got the same error. Looking online, this error has been happening for a while but I cannot seem to find an answer.
I'm using these versions:
Python-> 3.7.4
Pytest-> 5.2.1
IDE-----> Visual Studio code
OS-----> Windows 7
Full Traceback:
Traceback (most recent call last):
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\c.hernandezperez\AppData\Local\Continuum\anaconda3\Scripts\pytest.exe\__main__.py", line 9, in <module>
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\_pytest\config.py", line 54, in main
config = _prepareconfig(args, plugins)
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\_pytest\config.py", line 167, in _prepareconfig
pluginmanager=pluginmanager, args=args
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\__init__.py", line 617, in __call__
return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\__init__.py", line 222, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\__init__.py", line 216, in <lambda>
firstresult=hook.spec_opts.get('firstresult'),
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\callers.py", line 196, in _multicall
gen.send(outcome)
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\_pytest\helpconfig.py", line 89, in pytest_cmdline_parse
config = outcome.get_result()
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\callers.py", line 76, in get_result
raise ex[1].with_traceback(ex[2])
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\callers.py", line 180, in _multicall
res = hook_impl.function(*args)
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\_pytest\config.py", line 981, in pytest_cmdline_parse
self.parse(args)
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\_pytest\config.py", line 1146, in parse
self._preparse(args, addopts=addopts)
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\_pytest\config.py", line 1108, in _preparse
early_config=self, args=args, parser=self._parser
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\__init__.py", line 617, in __call__
return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\__init__.py", line 222, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\__init__.py", line 216, in <lambda>
firstresult=hook.spec_opts.get('firstresult'),
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\callers.py", line 201, in _multicall
return outcome.get_result()
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\callers.py", line 76, in get_result
raise ex[1].with_traceback(ex[2])
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\pluggy\callers.py", line 175, in _multicall
next(gen) # first yield
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\_pytest\capture.py", line 46, in pytest_load_initial_conftests
_py36_windowsconsoleio_workaround(sys.stdout)
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\_pytest\capture.py", line 711, in _py36_windowsconsoleio_workaround
sys.__stdin__ = sys.stdin = _reopen_stdio(sys.stdin, "rb")
File "c:\users\c.hernandezperez\appdata\local\continuum\anaconda3\lib\site-packages\_pytest\capture.py", line 704, in _reopen_stdio
open(os.dup(f.fileno()), mode, buffering),
OSError: [WinError 87] El parámetro no es correcto
Apparently Python 3.7.4 does not work with Pytest as stated on this GitHub question. This question is thus considered answered.

Flask/Keras webservice ModuleNotFoundError: No module named 'tensorflow_core.keras'

i'm building a simple webservice to classify an image.
Separated my keras model classifies correctly and the flask service is running.
But when i try to use the keras model in my flask app ...
from flask import Flask
from myproject.keras_model_wrapper import get_result
APP = Flask(__name__)
#APP.route('/')
def keras_result():
return get_result()
if __name__ == 'main':
APP.run()
... an import error occurs.
* Environment: development
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Traceback (most recent call last):
File "<python_path>\python\python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "<python_path>\python\python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "<project_path>\.venv\Scripts\flask.exe\__main__.py", line 9, in <module>
File "<project_path>\.venv\lib\site-packages\flask\cli.py", line 966, in main
cli.main(prog_name="python -m flask" if as_module else None)
File "<project_path>\.venv\lib\site-packages\flask\cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "<project_path>\.venv\lib\site-packages\click\core.py", line 717, in main
rv = self.invoke(ctx)
File "<project_path>\.venv\lib\site-packages\click\core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "<project_path>\.venv\lib\site-packages\click\core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "<project_path>\.venv\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "<project_path>\.venv\lib\site-packages\click\decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "<project_path>\.venv\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "<project_path>\.venv\lib\site-packages\flask\cli.py", line 860, in run_command
extra_files=extra_files,
File "<project_path>\.venv\lib\site-packages\werkzeug\serving.py", line 1008, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "<project_path>\.venv\lib\site-packages\werkzeug\_reloader.py", line 337, in run_with_reloader
reloader.run()
File "<project_path>\.venv\lib\site-packages\werkzeug\_reloader.py", line 202, in run
for filename in chain(_iter_module_files(), self.extra_files):
File "<project_path>\.venv\lib\site-packages\werkzeug\_reloader.py", line 24, in _iter_module_files
filename = getattr(module, "__file__", None)
File "<project_path>\.venv\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
module = self._load()
File "<project_path>\.venv\lib\site-packages\tensorflow\__init__.py", line 44, in _load
module = _importlib.import_module(self.__name__)
File "<python_path>\python\python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'tensorflow_core.keras'
I don't know if it's important that the import system searches out of my .venv
and TBH I have no idea, what exactly I should search for solving it on my own.
Every hint is welcome
I don't consider this to be a solution but Setting flask_debug=1(off) will allow you to continue working but you will need to restart the server after every change.
The issue is being tracked in TensorFlow Repo at the link below.
ModuleNotFoundError: No module named 'tensorflow_core.keras' in Flask
A Workaround that Works perfectly for Flask==1.1.1 and tensorflow==2.1.0
In Flask you should put "import tensorflow as tf" in the "__init__.py" file.
In Django you should put "import tensorflow as tf" in the "manage.py" file.

Alembic migrations when some tables exists

I have some migrations in alembic, and try to run alembic upgrade head to up-to-date my DB for revision, but in some cases I have tables, which already exists, so I have an error:
alembic upgrade head
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
INFO [alembic.runtime.migration] Running upgrade -> 3137cf88d8c6, create tables
Traceback (most recent call last):
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
psycopg2.ProgrammingError: relation "City" already exists
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/toweya/.virtualenvs/numus3/bin/alembic", line 11, in <module>
sys.exit(main())
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/config.py", line 479, in main
CommandLine(prog=prog).main(argv=argv)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/config.py", line 473, in main
self.run_cmd(cfg, options)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/config.py", line 456, in run_cmd
**dict((k, getattr(options, k, None)) for k in kwarg)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/command.py", line 254, in upgrade
script.run_env()
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/script/base.py", line 416, in run_env
util.load_python_file(self.dir, 'env.py')
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file
module = load_module_py(module_id, path)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/util/compat.py", line 64, in load_module_py
module_id, path).load_module(module_id)
File "<frozen importlib._bootstrap_external>", line 388, in _check_name_wrapper
File "<frozen importlib._bootstrap_external>", line 809, in load_module
File "<frozen importlib._bootstrap_external>", line 668, in load_module
File "<frozen importlib._bootstrap>", line 268, in _load_module_shim
File "<frozen importlib._bootstrap>", line 693, in _load
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "numus/env.py", line 68, in <module>
run_migrations_online()
File "numus/env.py", line 63, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/runtime/environment.py", line 817, in run_migrations
self.get_context().run_migrations(**kw)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/runtime/migration.py", line 323, in run_migrations
step.migration_fn(**kw)
File "/home/toweya/PycharmProjects/NumusPortal/portal/db_manage/numus/versions/3137cf88d8c6_create_tables.py", line 30, in upgrade
sa.Column('name', sa.String(64), nullable=False),
File "<string>", line 8, in create_table
File "<string>", line 3, in create_table
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/operations/ops.py", line 1106, in create_table
return operations.invoke(op)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/operations/base.py", line 318, in invoke
return fn(self, operation)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/operations/toimpl.py", line 101, in create_table
operations.impl.create_table(table)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/ddl/impl.py", line 194, in create_table
self._exec(schema.CreateTable(table))
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/ddl/impl.py", line 118, in _exec
return conn.execute(construct, *multiparams, **params)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 945, in execute
return meth(self, multiparams, params)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/sql/ddl.py", line 68, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1002, in _execute_ddl
compiled
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
context)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1393, in _handle_dbapi_exception
exc_info
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "City" already exists
[SQL: '\nCREATE TABLE "City" (\n\tid SERIAL NOT NULL, \n\tgeoid INTEGER NOT NULL, \n\tname VARCHAR(64) NOT NULL, \n\tPRIMARY KEY (id), \n\tUNIQUE (geoid)\n)\n\n']
How can I ignore existing tables?
That means something like: if table exist - continue but not abort
You need to go to your migration/version folder or alembic :
/path/to/prooject/alembic/versions/3137cf88d8c6.py
and edit the last migration script by removing the script for creating the table in the upgrade function and remove the reverse task in downgrade function in the same script.
As said in flask migrate :
The migration script needs to be reviewed and edited
And I quote this from alembic:
We review and modify these by hand as needed, then proceed normally.
Edit :
You can also do that automatically by using the include_object as suggested by this answer

Resources