Reputation: 189
I have a django - mySQL migration problem. I can not figure out what is the problem, please if you have any idea let me know.
I try to change the basic sqlite3 database to mySQL. I started a new django project. I work on mac, I use virtual env and my python version 3.9.0. I have installed pip and homebrew. I installed mysql with homebrew. I made a sample database in mysql, and also checked in the workbench. I started mysql.
I installed mysqlclient (2.0.3) via pip. I opened my project and change the DATABASE = {...} in settings.py to:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'sample',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'thisisreallymypassword',
}
}
My problem is when I run the python manage.py -migrate command I always get NameError: name '_mysql' is not defined. I went through all the topics, but can't figure out the problem. Any idea?
(env) Csutkas-MacBookPro:greenium gezamacbookpro$ pip freeze
asgiref==3.3.1
Django==3.1.4
django-filter==2.4.0
django-mysql==3.10.0
mysqlclient==2.0.3
Pillow==8.0.1
pytz==2020.4
sqlparse==0.4.1
(env) Csutkas-MacBookPro:greenium gezamacbookpro$ python manage.py migrate
Traceback (most recent call last):
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/MySQLdb/__init__.py", line 18, in <module>
from . import _mysql
ImportError: dlopen(/Users/gezamacbookpro/env/lib/python3.9/site-packages/MySQLdb/_mysql.cpython-39-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
Referenced from: /Users/gezamacbookpro/env/lib/python3.9/site-packages/MySQLdb/_mysql.cpython-39-darwin.so
Reason: image not found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/gezamacbookpro/Developer/Django/greenium/manage.py", line 22, in <module>
main()
File "/Users/gezamacbookpro/Developer/Django/greenium/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/db/models/base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/db/models/base.py", line 326, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/db/models/options.py", line 206, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/db/utils.py", line 214, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
import MySQLdb as Database
File "/Users/gezamacbookpro/env/lib/python3.9/site-packages/MySQLdb/__init__.py", line 24, in <module>
version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined
Upvotes: 0
Views: 726
Reputation: 1
I had similar problem "Library not loaded: @rpath/libmysqlclient.21.dylib" after upgrading etc and tried lot and lot of things on Internet with no good results, but this finally worked for me changed my .zprofile with adding DYLD_LIBRARY_PATH
# Setting PATH for Python 3.9
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
export PATH
DYLD_LIBRARY_PATH="/usr/local/lib:${DYLD_LIBRARY_PATH}"
export DYLD_LIBRARY_PATH
Upvotes: 0
Reputation: 189
Finally I got through. What I did:
I uninstalled completely mysql from my mac followed by this: https://gist.github.com/vitorbritto/0555879fe4414d18569d
I do not know it is related here or not but I updated brew and got an error on cleanup that brew cleanup: Error: Permission denied @ apply2files
I fixed this according to this post (https://github.com/Homebrew/homebrew-core/issues/45009):
sudo chown -R "$(whoami)":admin /usr/local/lib
Then I made the mysql installation according to @cizario 's suggestion (thanks a lot) to this way: https://pypi.org/project/mysqlclient/
I used the first method and I didn't install the mysql-client.
# Assume you are activating Python 3 venv
$ brew install mysql
$ pip install mysqlclient
After that I started mysql service with brew:
brew services start mysql
I made a new database:
mysql -u root
CREATE DATABASE sample;
Then I updated the django settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'sample',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': '',
}
}
And now the python manage.py migrate
worked!
Upvotes: 1