Django 2.0 - Python3.6 - CentOS 7 - MySQL connections - mysql

i try to install my website on a webserver which is in CentOS 7 with Django 2.0.6 and Python 3.6.
I installed : mysql-connector = 2.1.6,
mysql-connector-python = 8.0.11,
MySQL-python = 1.2.5,
mysqlclient = 1.3.12,
PyMySQL = 0.8.1
And when i run this command :
python3.6 manage.py migrate
I have this error :
File "MYPYTHONPATH/python3.6/site-packages/django/db/backends/mysql/base.py", line 36, in <module>
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 1.2.5
But in the base.py file :
try:
import MySQLdb as Database
except ImportError as err:
raise ImproperlyConfigured(
'Error loading MySQLdb module.\n'
'Did you install mysqlclient?'
) from err
from MySQLdb.constants import CLIENT, FIELD_TYPE # isort:skip
from MySQLdb.converters import conversions # isort:skip
# Some of these import MySQLdb, so import them after checking if it's installed.
from .client import DatabaseClient # isort:skip
from .creation import DatabaseCreation # isort:skip
from .features import DatabaseFeatures # isort:skip
from .introspection import DatabaseIntrospection # isort:skip
from .operations import DatabaseOperations # isort:skip
from .schema import DatabaseSchemaEditor # isort:skip
from .validation import DatabaseValidation # isort:skip
version = Database.version_info
if version < (1, 3, 3):
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
I dont understand what can i do for resolving this kind of problem !
I hope someone can help me on =)

Okay, i solved it with the answer of this thread.
No module named MySQLdb
pip install PyMySQL
and then add this two lines to your Project/Project/init.py
import pymysql
pymysql.install_as_MySQLdb()
Works on WIN and python 3.3+

Related

ImportError: cannot import name '_rfc_1738_quote'

I am trying to connect Jupyter Notebook to Snowflake but getting the below issue
`
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-12-3b4f5d455df9> in <module>
1 from sqlalchemy import create_engine
----> 2 from snowflake.sqlalchemy import URL
3 import sqlalchemy as db
4 import pandas as pd
5
~\AppData\Local\Continuum\anaconda3\lib\site-packages\snowflake\sqlalchemy\__init__.py in <module>
56 VARIANT,
57 )
---> 58 from .util import _url as URL
59 from .version import VERSION
60
~\AppData\Local\Continuum\anaconda3\lib\site-packages\snowflake\sqlalchemy\util.py in <module>
11 from urllib.parse import quote_plus
12
---> 13 from sqlalchemy.engine.url import _rfc_1738_quote
14
15
ImportError: cannot import name '_rfc_1738_quote'
`
And here: https://github.com/snowflakedb/snowflake-sqlalchemy/issues/350 it was supposed to upgrade to v1.4.3 version but getting the below error when trying to upgrade.
Used command: pip install snowflake-sqlalchemy==1.4.3
Error
`
ERROR: Could not find a version that satisfies the requirement snowflake-sqlalchemy==1.4.3 (from versions: 1.0.7, 1.0.8, 1.0.9, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.1.5, 1.1.6, 1.1.9, 1.1.10, 1.1.11, 1.1.12, 1.1.13, 1.1.14, 1.1.15, 1.1.16, 1.1.17, 1.1.18, 1.2.0, 1.2.1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.3.1, 1.3.2, 1.3.3, 1.3.4)
ERROR: No matching distribution found for snowflake-sqlalchemy==1.4.3
`
Can someone please help with the issue?
Tried to upgrade to the latest version which is not the latest.
The issue was resolved by upgrading python to 3.9 and then upgrading sqlalchemy to 1.4.3

AttributeError: module 'keras.api._v2.keras' has no attribute 'utils'

OS: MacOS Monterey (12.4)
Chip: Apple Silicon M1
Env: Conda 4.12.0
Python: 3.9.12
TensorFlow version: 2.9.0, installed through pip install tensorflow-macos
Error message:
Traceback (most recent call last):
File "/Users/okihita/PycharmProjects/TFFlowerClassification/main.py", line 20, in <module>
training_dataset = tf.keras.utils.image_dataset_from_directory(
AttributeError: module 'keras.api._v2.keras' has no attribute 'utils'
Here's my main.py code:
import pathlib
import tensorflow as tf
from PIL import Image
data_dir = pathlib.Path('aksara-batak/toba')
aksara = list(data_dir.glob('a/*'))
img_height = 30
img_width = 30
training_dataset = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width)
)
Did I import the wrong version of TensorFlow?

TesseractError: (2, 'Usage: pytesseract [-l lang] input_file') error

I am getting the error TesseractError: (2, 'Usage: pytesseract [-l lang] input_file'). Using ! sudo apt install but still getting the error in colab.
Its a JPG I am trying to read.
-----------------------------code -------------------------------------
! apt install tesseract-ocr
! apt install libtesseract-dev
! sudo apt install tesseract-ocr
! pip install Pillow
! pip install pytesseract
import pytesseract
import shutil
import os
import cv2
import random
from google.colab import files
from io import BytesIO
from PIL import Image,ImageFilter
!pip install pdf2image
!apt-get install -y poppler-utils
from pdf2image import convert_from_path
#from google.colab import drive
!sudo apt install tesseract-ocr
!pip install pytesseract
!pip install tesseract
pytesseract.pytesseract.tesseract_cmd = r'/usr/local/bin/pytesseract'
image_path_in_colab = 'Pillow block drawing.jpg'
img = cv2.imread('Pillow block drawing.jpg')
custom_config = r'-l eng --psm 6'
pytesseract.image_to_string(img, config=custom_config)
extractedInformation = pytesseract.image_to_string(Image.open(image_path_in_colab))
print(extractedInformation)
install:
!sudo apt install tesseract-ocr
Change:
pytesseract.pytesseract.tesseract_cmd = r'/usr/local/bin/pytesseract'
to:
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'
you need to set the tesseract location as env variable . Like this
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'
The following line is wrong
pytesseract.pytesseract.tesseract_cmd =
r'/usr/local/bin/pytesseract'

aiohttp and gunicorn: logger doesn't work

I'm trying to debug my aiopg connection settings in production environment using aiohttp logger and gunicorn.
I'm trying to log my database credentials:
models.py:
async def init_pg(app):
logger = logging.getLogger('aiohttp.access')
logger.error("POSTGRES_USER = %s" % app['settings'].POSTGRES_USER)
logger.error("POSTGRES_DATABASE = %s" % app['settings'].POSTGRES_DATABASE)
logger.error("POSTGRES_HOST = %s" % app['settings'].POSTGRES_HOST)
logger.error("POSTGRES_PASSWORD = %s" % app['settings'].POSTGRES_PASSWORD)
app['engine'] = await create_engine(
user=app['settings'].POSTGRES_USER,
database=app['settings'].POSTGRES_DATABASE,
host=app['settings'].POSTGRES_HOST,
password=app['settings'].POSTGRES_PASSWORD
)
This doesn't add any output to /var/log/gunicorn/error_log, although it is expected to.
Here is how I start gunicorn:
/usr/local/bin/gunicorn producer.main:app --daemon --bind 0.0.0.0:8002 --worker-class aiohttp.worker.GunicornWebWorker --access-logfile /var/log/gunicorn/access_log --error-logfile /var/log/gunicorn/error_log --env ENVIRONMENT=PRODUCTION --timeout 120
Here is how I create aiohttp app:
main.py:
import logging
import aiohttp_jinja2
import jinja2
from aiojobs.aiohttp import setup as setup_aiojobs
from aiohttp_swagger import setup_swagger
from aiohttp import web, web_middlewares
from . import settings
from .models import init_pg
from .urls import setup_routes
"""
Run either of the following commands from the parent of current directory:
adev runserver producer --livereload
python3 -m producer.main
"""
def create_app():
logging.basicConfig(level=logging.DEBUG)
app = web.Application(middlewares=[
web_middlewares.normalize_path_middleware(append_slash=True),
], client_max_size=2048**2)
app.update(name='producer', settings=settings)
# setup Jinja2 template renderer
aiohttp_jinja2.setup(app, loader=jinja2.PackageLoader('producer', 'templates'))
# create db connection on startup, shutdown on exit
app.on_startup.append(init_pg)
# app.on_cleanup.append(close_pg)
# setup views and routes
setup_routes(app)
# setup middlewares
# setup_middlewares(app)
# setup aiojobs scheduler
setup_aiojobs(app)
# setup swagger documentation
setup_swagger(app, swagger_url="api/doc")
return app
if __name__ == '__main__':
app = create_app()
web.run_app(app, host=app['settings'].HOST, port=app['settings'].PORT)

error ('datetime.datetime' object has no attribute 'split') in django 1.11.4

I am learning django version 1.11.4 through tutorial on the official documentation. I am using python 3.6.5 and mysql8 for database. I also use mysql.connector.django to connect to mysql database. I tried to do the first Django app, part 2.
This is the link of the example I used
everything works fine except when I run the this command:
Question.objects.all()
I got the following error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 226, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 62, in __iter__
for row in compiler.results_iter(results):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 839, in results_iter
for rows in results:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1284, in cursor_iter
sentinel):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1283, in <lambda>
for rows in iter((lambda: cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)),
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/utils.py", line 101, in inner
return func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 510, in fetchmany
rows.extend(self._cnx.get_rows(size))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 275, in get_rows
row[i])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/conversion.py", line 205, in to_python
return self._cache_field_types[vtype[1]](value, vtype)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/django/base.py", line 119, in _DATETIME_to_python
dt = MySQLConverter._DATETIME_to_python(self, value)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/conversion.py", line 506, in _DATETIME_to_python
(date_, time_) = value.split(b' ')
AttributeError: 'datetime.datetime' object has no attribute 'split'
the code used in the models file :
import datetime
from django.db import models
from django.utils import timezone
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def was_published_recently(self):
return self.pub_date >= timezone.now() -
datetime.timedelta(days=1)
def __str__(self):
return self.question_text
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
the database setting:
DATABASES = {
'default': {
'NAME': 'mysite',
'ENGINE': 'mysql.connector.django',
'USER': 'root',
'PASSWORD': '********',
'OPTIONS': {
'autocommit': True,
},
}
}
any clue or help to fix this error will be appreciated.
I struggled for few hours to set up my Django project with python3 using MySQL DB on MacOS.
I was not able to install either mysqlclient and MySQL-Python by pip3 in a virtual environment created with virtualenv
error stacktrace was: something wrong due to configparser in python3
Jans-MacBook-Pro:~ jan$ /Library/Frameworks/Python.framework/Versions/3.3/bin/pip-3.3 install MySQL-python
Downloading/unpacking MySQL-python
Running setup.py egg_info for package MySQL-python
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python/setup.py", line 14, in <module>
from setup_posix import get_config
File "./setup_posix.py", line 2, in <module>
from ConfigParser import SafeConfigParser
ImportError: No module named 'ConfigParser'
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python/setup.py", line 14, in <module>
from setup_posix import get_config
File "./setup_posix.py", line 2, in <module>
from ConfigParser import SafeConfigParser
ImportError: No module named 'ConfigParser'
----------------------------------------
Command python setup.py egg_info failed with error code 1 in /var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python
Storing complete log in /Users/jan/.pip/pip.log
Jans-MacBook-Pro:~ jan$
Now the SOLUTION which worked for me was
1) installing mysql with brew again
brew install mysql
2) upgrading mysql with brew to latest version (if required)
brew upgrade mysql
3) installing mysqlclient now with pip3 (installing globally without virtualenv)
pip3 install mysqlclient
4) now access virtualenv and instal the mysqlclient in it, it will install fine without any error for configparser
As can be seen in the bug report posted by #Alasdair, the solution is:
Set use_pure=True in DATABASES['default']['OPTIONS'].
I too went through this pain yesterday or 2 days ago and was able to get it running with the mysqlclient-1.3.12. I'm going from memory here so bear with me, I tried a lot of things but eventually I got it to work.
I installed mysql8 and mysql8connector from the mysql web site as you did but got no love. After much searching and many trial and errors I found an answer somewhere which I can't find my way back to but I ended up doing:
brew install mysql
pip install mysqlclient
I know that you already have mysql installed but brew install mysql seems to add client libraries that are used to compile the mysqlclient connector. Then my database in my settings file looks like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'polls',
'USER': 'myuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': {
'charset': 'utf8mb4',
'autocommit': True,
},
}
}
Note the ENGINE is different. I can replicate your exact error if I change my ENGINE to mysql.connector.django.
However, using django.db.backends.mysql I still get the following warning:
lib/python3.6/site-packages/django/db/backends/mysql/base.py:71: Warning: (3719, "'utf8' is currently an alias for the character set UTF8MB3, which will be replaced by UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous."
which I can't figure out, but as it is only a warning the Django tutorial seems to be working fine.
Let me know if this helps or if you have other questions and I'll do my best to help.
This is mysql-connector-python data conversion issue. You can get around this by setting the database options parameter with use_pure=true. This issue is not fixed in the current version 8.0.11,8.0.12, and 8.0.13.
"OPTIONS":{
"use_pure":True
}
This is just a temporary solution. Refer here for more information: https://bugs.mysql.com/bug.php?id=90541