mindcurrent / djanky-django

Daniel's Janky Attempt at a Django RESTful API Server
0 stars 1 forks source link

Hook Django up to (local) MySQL server #9

Open dpcunningham opened 4 years ago

dpcunningham commented 4 years ago

Perform the MySQL cofig/hookup per Database Tutorial, then:

$ python3 manage.py migrate
    import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'
The above exception was the direct cause of the following exception: 
[...exception stack...]
Did you install mysqlclient?

Ideas:

Solution: per StackExchange (cycles of pip3 un/re-installs, also involved apt-get install):

Error loading MySQLdb Module 'Did you install mysqlclient or MySQL-python?'

$ vi ./djankysite/__init__.py   # to add:
$ cat ./djankysite/__init__.py

import pymysql
pymysql.install_as_MySQLdb()

$ pip3 install pymysql
Collecting pymysql
  Downloading https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
    100% |████████████████████████████████| 51kB 603kB/s 
Installing collected packages: pymysql
Successfully installed pymysql-0.9.3

History (so far):

$history
 1358  pip3 install mysqlclient
 1359  pip3 install -U mysqlclient
 1360  pip3 uninstall mysqlclient
 1361  pip3 uninstall pymysql
 1362  pip3 install mysqlclient
 1363  sudo apt-get install python-setuptools
 1364  pip3 install mysqlclient
 1365  sudo apt-get install python3-setuptools
 1366  sudo apt update
 1367  pip3 install mysqlclient
 1368  pip3 uninstall mysqlclient
 1369  pip3 install wheel
 1370  pip3 install mysqlclient

$ pip3 uninstall mysqlclient

$ pip3 install wheel
Collecting wheel
  Using cached https://files.pythonhosted.org/packages/00/83/b4a77d044e78ad1a45610eb88f745be2fd2c6d658f9798a15e384b7d57c9/wheel-0.33.6-py2.py3-none-any.whl
Installing collected packages: wheel
Successfully installed wheel-0.33.6

$ pip3 install mysqlclient
Collecting mysqlclient
  Using cached https://files.pythonhosted.org/packages/4d/38/c5f8bac9c50f3042c8f05615f84206f77f03db79781db841898fde1bb284/mysqlclient-1.4.4.tar.gz
Building wheels for collected packages: mysqlclient
  Running setup.py bdist_wheel for mysqlclient ... done
  Stored in directory: /home/dpc/.cache/pip/wheels/a0/04/57/031b9b01df38999df7dc7f4ee998a98ecdbd2d781f73e3ffbf
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.4.4

$ pip3 install pymysql
Collecting pymysql
  Using cached https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl
Installing collected packages: pymysql
Successfully installed pymysql-0.9.3

Moving on...

I had a bitch of a time with a shitload of thrown exceptions when trying to run: python3 manage.py migrate

This was just a shit-show. Python versioning is such a PITA! :unamused:

I thrashed around for a long-ass time using various combinations of these links:

What finally ended up working was this one in particular:

Django - installing mysqlclient error: mysqlclient 1.3.13 or newer is required; you have 0.9.3

Except I just bumped down the version condition, instead of doing a pass (pseudo-comment) section on it). Here's the suggested solution that finally worked:

Commenter: This is how I fixed it

Go to your django/db/backends/mysql installation dir. Check your path in the error message.

I'm using pipenv so my path is:

    /home/username/.local/share/virtualenvs/project-env/lib/python3.7/site-packages/django/db/backends/mysql

Open file base.py and search for:

version = Database.version_info

Put a pass inside if and comment line:

    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.version)

Like this.

if version < (1, 3, 13):
   pass
   '''
   raise ImproperlyConfigured(
       'mysqlclient 1.3.13 or newer is required; you have %s.'
       % Database.__version__
   )
   '''

Save, close this file and open operations.py.

This is how I fixed it.

Go to your django/db/backends/mysql installation dir. Check your path in the error message.

I'm using pipenv so my path is:

/home/username/.local/share/virtualenvs/project-env/lib/python3.7/site-packages/django/db/backends/mysql

Open file base.py and search for:

version = Database.version_info

Put a pass inside if and comment line:

raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.version)

Like this.

if version < (1, 3, 13):
   pass
   '''
   raise ImproperlyConfigured(
       'mysqlclient 1.3.13 or newer is required; you have %s.'
       % Database.__version__
   )
   '''

NB: I just raised the version parameters to let v 0.9.3 pass, like so:

$ vi /home/dpc/.local/lib/python3.6/site-packages/django/db/backends/mysql/basr.py

...to make it:

if version < (0, 9, 3):
   raise ImproperlyConfigured(
       'mysqlclient 1.3.13 or newer is required; you have %s.'
       % Database.__version__
   )

Critical! Eliminate the lowest-level exception thrown by decode:

$ python3 manage.py migrate
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)

[....Skipping 8 million exception errors!...]

  File "/home/dpc/.local/lib/python3.6/site-packages/django/db/backends/mysql/features.py", line 82, in is_sql_auto_is_null_enabled
    cursor.execute('SELECT @@SQL_AUTO_IS_NULL')
  File "/home/dpc/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 103, in execute
    sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  File "/home/dpc/.local/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 146, in last_executed_query
    query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'

Save, close this file and open operations.py.

$ vi /home/dpc/.local/lib/python3.6/site-packages/django/db/backends/mysql/operations.py

Search for:

query = query.decode(errors='replace')

and change decode to encode

query = query.encode(errors='replace')

Now, try to run the server.

FINALLY!

dpc@LT3-Insp17-2017:~/dpc.data/local.FS/lfs.03-Projects.Active/project.MindCurrent/dev/python/djankysite$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

Re-cap of history since last capture:

$ history | tail -n20
 1384  python3 manage.py migrate
 1385  pip3 uninstall mysqlclient
 1386  pip3 uninstall pymysql
 1387  pip3 install pymysql
 1388  pip3 install mysqlclient
 1389  python3 manage.py migrate
 1390  vi /home/dpc/.local/lib/python3.6/site-packages/django/db/backends/mysql/base.py
 1391  python3 manage.py migrate
 1392  sudo apt-get install python3-dev
 1393  python3 manage.py migrate
 1394  vi ./djankysite/__init__.py 
 1395  python3 -m pip install PyMySQL
 1396  python3 manage.py migrate
 1397  pip3 install mysql-python
 1398  sudo apt-get install python3-mysqldb libmysqlclient-dev python-dev
 1399  python3 manage.py migrate
 1400  pip3 uninstall MySQL-python
 1401  vi /home/dpc/.local/lib/python3.6/site-packages/django/db/backends/mysql/operations.py 
 1402  python3 manage.py migrate
dpcunningham commented 4 years ago

Note to self: for future automation, everything I did with vi we can do with sed.