Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

python - getting NameError when tries to migrate mySQL database into django project

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...