Howdie do,
I'm attempting to use Python 3.6 with SQLAlchemy. I am able to connect to the database, but all reflection attempts are failing:
Traceback (most recent call last):
File "/Users/jw1050/Python/projects/label_automation/generate.py", line 14, in <module>
metadata.reflect(engine, only=['parcel', 'order', 'address', 'document'])
File "/Users/jw1050/.virtualenvs/psd_label_automatiion/lib/python3.6/site-packages/sqlalchemy/sql/schema.py", line 3874, in reflect
bind.engine.table_names(schema, connection=conn))
File "/Users/jw1050/.virtualenvs/psd_label_automatiion/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2128, in table_names
return self.dialect.get_table_names(conn, schema)
File "<string>", line 2, in get_table_names
File "/Users/jw1050/.virtualenvs/psd_label_automatiion/lib/python3.6/site-packages/sqlalchemy/engine/reflection.py", line 42, in cache
return fn(self, con, *args, **kw)
File "/Users/jw1050/.virtualenvs/psd_label_automatiion/lib/python3.6/site-packages/sqlalchemy/dialects/mysql/base.py", line 1756, in get_table_names
self.identifier_preparer.quote_identifier(current_schema))
File "/Users/jw1050/.virtualenvs/psd_label_automatiion/lib/python3.6/site-packages/sqlalchemy/sql/compiler.py", line 2888, in quote_identifier
self._escape_identifier(value) + \
File "/Users/jw1050/.virtualenvs/psd_label_automatiion/lib/python3.6/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 78, in _escape_identifier
value = value.replace(self.escape_quote, self.escape_to_quote)
TypeError: a bytes-like object is required, not 'str'
My connection information is below:
engine = create_engine('mysql+pymysql://:127.0.0.1:3306/(db_name)?charset=utf8&use_unicode=0')
session = scoped_session(sessionmaker(bind=engine))()
metadata = MetaData()
metadata.reflect(engine, only=['parcel', 'order', 'address', 'document'])
Base = automap_base(metadata=metadata)
Base.prepare()
Everything works fine in Python 2, but I do not want to use Python 2 here. Has anybody else run into this issue and able to resolve?
I've figured this out.
This was due to my connection string having use_unicode=0 in it.
According to the SQLAlchemy Docs this setting should never be used in Python3. In Python 2, it gives superior performance but not in Python 3
Hopefully this helps someone
Related
I installed chatterbot in my terminal earlier today using virtual studio code's terminal. I saw that both chatterbot and chatterbot_corpus worked in installation. Then, I made the following python document:
EDIT: Turns out I should define a chatbot variable first.
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.trainers import ListTrainer
conversation = [
"Hello",
"Hi there!",
"How are you doing?",
"I'm doing great.",
"That is good to hear",
"Thank you.",
"You're welcome."
]
bot = ChatBot('Maya')
trainer = ListTrainer(bot)
trainer.train(conversation)
This was my code
it says this however
bot = ChatBot('Maya')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\chatterbot\chatterbot.py", line 34, in __init__
self.storage = utils.initialize_class(storage_adapter, **kwargs)
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\chatterbot\utils.py", line 54, in initialize_class
return Class(*args, **kwargs)
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\chatterbot\storage\sql_storage.py", line 22, in __init__
from sqlalchemy import create_engine
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sqlalchemy\__init__.py", line 8, in <module>
from . import util as _util # noqa
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sqlalchemy\util\__init__.py", line 14, in <module>
from ._collections import coerce_generator_arg # noqa
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sqlalchemy\util\_collections.py", line 16, in <module>
from .compat import binary_types
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sqlalchemy\util\compat.py", line 264, in <module>
time_func = time.clock
AttributeError: module 'time' has no attribute 'clock'
Does anyone know how to fix this easily?
EDIT: I just updated python using pip install --upgrade ipython in terminal, but it didn't fix the issue
EDIT 2: Well now I tried updating a package using pip install sqlalchemy --upgrade
But now it gives
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\chatterbot\chatterbot.py", line 34, in __init__
self.storage = utils.initialize_class(storage_adapter, **kwargs)
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\chatterbot\utils.py", line 54, in initialize_class
return Class(*args, **kwargs)
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\chatterbot\storage\sql_storage.py", line 46, in __init__
if not self.engine.dialect.has_table(self.engine, 'Statement'):
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sqlalchemy\dialects\sqlite\base.py", line 2009, in has_table
self._ensure_has_table_connection(connection)
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sqlalchemy\engine\default.py", line 341, in _ensure_has_table_connection
raise exc.ArgumentError(
sqlalchemy.exc.ArgumentError: The argument passed to Dialect.has_table() should be a <class 'sqlalchemy.engine.base.Connection'>,
got <class 'sqlalchemy.engine.base.Engine'>. Additionally, the Dialect.has_table() method is for internal dialect use only; please use ``inspect(some_engine).has_table(<tablename>>)`` for public API use.
I am in the latest version though
PS C:\Users\Subha> Python --version
Python 3.9.6
EDIT 3: Now it comes up with
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\chatterbot\chatterbot.py", line 34, in __init__
self.storage = utils.initialize_class(storage_adapter, **kwargs)
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\chatterbot\utils.py", line 54, in initialize_class
return Class(*args, **kwargs)
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\chatterbot\storage\sql_storage.py", line 46, in __init__
# if not self.engine.dialect.has_table(self.engine, 'Statement'):
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sqlalchemy\dialects\sqlite\base.py", line 2009, in has_table
self._ensure_has_table_connection(connection)
File "C:\Users\Subha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sqlalchemy\engine\default.py", line 341, in _ensure_has_table_connection
raise exc.ArgumentError(
sqlalchemy.exc.ArgumentError: The argument passed to Dialect.has_table() should be a <class 'sqlalchemy.engine.base.Connection'>, got <class 'sqlalchemy.engine.base.Engine'>. Additionally, the Dialect.has_table() method is for internal dialect use only; please use ``inspect(some_engine).has_table(<tablename>>)`` for public API use.
it seems the python upgrade worked with regard to the initial error on time.clock. This new error you are seeing is quite different to the previous one. In this case you need to go into chatterbot/storage/sql_storage.py and comment out if not self.engine.dialect.has_table(self.engine, 'Statement'): and leave only self.create_database(). This means that instead of checking whether a database table has been created which is triggering the error, it will just create the DB every time, which i expect to work fine.
# if not self.engine.dialect.has_table(self.engine, 'Statement'):
self.create_database()
I wrote the following program to connect to my mongo database
from flask import Flask
from flask_mongoalchemy import MongoAlchemy
app = Flask(__name__)
app.config['MONGOALCHEMY_DATABASE']='MyDatabase'
app.config['MONGOALCHEMY_CONNECTION_STRING']='mongodb://<username>:<password>#mycluster-shard-00-00-cfkbg.mongodb.net:27017,mycluster-shard-00-01-cfkbg.mongodb.net:27017,mycluster-shard-00-02-cfkbg.mongodb.net:27017/test?ssl=true&replicaSet=MyCluster-shard-0&authSource=admin&retryWrites=true&w=majority'
db=MongoAlchemy(app)
class Example(db.Document):
name=db.StringField()
if __name__=='__main__':
obj=Example(name='Aman')
obj.save()
Error:
Traceback (most recent call last):
File "app.py", line 15, in <module>
obj.save()
File "/usr/local/lib/python3.6/dist-packages/flask_mongoalchemy/__init__.py", line 271, in save
self._session.insert(self, safe=safe)
File "/usr/local/lib/python3.6/dist-packages/mongoalchemy/session.py", line 172, in insert
self.add(item, safe=safe)
File "/usr/local/lib/python3.6/dist-packages/mongoalchemy/session.py", line 188, in add
return self.flush()
File "/usr/local/lib/python3.6/dist-packages/mongoalchemy/session.py", line 414, in flush
result = op.execute()
File "/usr/local/lib/python3.6/dist-packages/mongoalchemy/ops.py", line 97, in execute
return self.collection.save(self.data, safe=self.safe)
File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 1881, in save
write_concern = WriteConcern(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'safe'
Try to install an older version of pymongo (preferably 2.9.0).
According to https://github.com/MongoEngine/mongoengine/issues/1154:
last version of mongoengine is not compatible with pymongo 3+:
I am trying to get going with django, which so far I have found to be amazing other than repeated db issues.
My latest is following the Django by Example book and I have followed everything to the letter, yet when following some simple instructions to add some data via the python shell api I get the following:
>>> from django.contrib.auth.models import User
>>> from blog.models import Post
>>> user = User.objects.get(username='jamie')
>>> Post.objects.create(title='One More Post', slug='one-more-post', body='Post body', author='user')
Traceback (most recent call last):
File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/core/management/commands/shell.py", line 69, in handle
self.run_shell(shell=options['interface'])
File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/core/management/commands/shell.py", line 61, in run_shell
raise ImportError
ImportError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/db/models/query.py", line 346, in create
obj = self.model(**kwargs)
File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/db/models/base.py", line 468, in __init__
setattr(self, field.name, rel_obj)
File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/db/models/fields/related.py", line 629, in __set__
self.field.rel.to._meta.object_name,
ValueError: Cannot assign "'user'": "Post.author" must be a "User" instance.
This has happened while following multiple tutorials and I am stumped. I followed standard instructions and installed, pip, python and django via terminal. Also using a virtual env so not sure why this is happening.
Use this instead of your statement.
Use user variable instead of 'user' string.
Post.objects.create(title='One More Post', slug='one-more-post',
body='Post body', author=user)
I want to make an application for Sailfish OS using QML and Python. I want to have models in python and save data to sqlite using SQLAlchemy. Problem is I have no idea how can I get dependencies to my project and Sailfish OS does not have SQLAlchemy in repository. I tryed to install it using pip to lib directory in my project using
pip install --install-option="--install-purelib=pwd/lib" sqlalchemy
It installed SQLAlechemy as i expected, but when i tryed to run my project, it crashed on select with where condition - but the same code worked fine when I imported SQLAlchemy from my system.
I both installed SQLAlchemy are 1.0.8 version.
Select, which works with my SQLAlchemy installed without prefix
def getValue(self, key):
s = select([self.table.c.value]).where(self.table.c.key==key)
return self.con.execute(s).fetchone()
and error
Traceback (most recent call last):
File "", line 1, in
File "/home/tobik/Buhvico/Programovani/beardware/hgclient/qml/hgo/models.py", line 58, in getValue
s = select([self.table.c.value]).where(self.table.c.key==key)
File "", line 2, in where
File "/home/tobik/Buhvico/Programovani/beardware/hgclient/qml/hgo/lib/sqlalchemy/sql/base.py", line 45, in _generative
fn(self, *args[1:], **kw)
File "/home/tobik/Buhvico/Programovani/beardware/hgclient/qml/hgo/lib/sqlalchemy/sql/selectable.py", line 2897, in where
self.append_whereclause(whereclause)
File "/home/tobik/Buhvico/Programovani/beardware/hgclient/qml/hgo/lib/sqlalchemy/sql/selectable.py", line 3122, in append_whereclause
True_._ifnone(self.whereclause), whereclause)
File "/home/tobik/Buhvico/Programovani/beardware/hgclient/qml/hgo/lib/sqlalchemy/sql/elements.py", line 1929, in and
return cls.construct(operators.and, True_, False_, *clauses)
File "/home/tobik/Buhvico/Programovani/beardware/hgclient/qml/hgo/lib/sqlalchemy/sql/elements.py", line 1863, in _construct
util.coerce_generator_arg(clauses)
File "/home/tobik/Buhvico/Programovani/beardware/hgclient/qml/hgo/lib/sqlalchemy/sql/elements.py", line 1862, in
for clause in
File "/home/tobik/Buhvico/Programovani/beardware/hgclient/qml/hgo/lib/sqlalchemy/sql/elements.py", line 3766, in _expression_literal_as_text
return _literal_as_text(element, warn=True)
File "/home/tobik/Buhvico/Programovani/beardware/hgclient/qml/hgo/lib/sqlalchemy/sql/elements.py", line 3787, in _literal_as_text
"instead" % type(element)
hgo.lib.sqlalchemy.exc.ArgumentError: SQL expression object or string expected, got object of type instead
Thanks for ur suggestions :)
My web2py application returned me an error today, which is quite odd.
Traceback (most recent call last):
File "/var/www/web2py/gluon/restricted.py", line 212, in restricted
exec ccode in environment
File "/var/www/web2py/applications/1MedCloud/controllers/default.py", line 475, in <module>
File "/var/www/web2py/gluon/globals.py", line 194, in <lambda>
self._caller = lambda f: f()
File "/var/www/web2py/applications/1MedCloud/controllers/default.py", line 63, in patient_register
rows = db(db.patientaccount.email==email).select()
File "/var/www/web2py/gluon/dal.py", line 7837, in __getattr__
return ogetattr(self, key)
AttributeError: 'DAL' object has no attribute 'patientaccount'
I am using Mysql as the database, and the table 'patientaccount' does exist. There is no connection issue as I can create tables but not fetch them from the server.
I have been using the very same code to do the db thing, here is my code
db = DAL('mysql://###:$$$#^^^^^^:3306/account_info', pool_size=0)
rows = db(db.patientaccount.email==email).select()
I did not change any code in my default.py file, but accidentally deleted some files inside "database" folder in my application. But I doubt if that could result the error, since the module is fetching tables on the server rather than using local files.
Please help! Thanks in advance!
The DAL does not inspect the MySQL database to discover its tables and fields. You must define the data models explicitly. So, somewhere in your code, you must do:
db.define_table('patientaccount',
Field('email'),
...)
That will define the db.patientaccount table so the DAL knows it exists and what fields it includes.