Hướng dẫn nameerror: name _mysql is not defined mac m1 - nameerror: name _mysql không được xác định mac m1

I am trying to include mysql into my django and face the error of

in  version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined

I think this might be an error due to the new M1 chip and mac OS system, but I can't figure out how to fix it. [I am using mac Monterey with M1 chip.]

Here is the package version:

mysql-connector-python    8.0.29
mysqlclient               2.1.1
mysql  Ver 8.0.29 for macos12.2 on x86_64 [Homebrew]
Python 3.8.9

I've read it somewhere about my python needs to be at the homebrew directory, but not sure how to do it.

which python3
/usr/bin/python3

I've also seen other solution of using

arch -x86_64 brew install mysql

It didnt work for me. here is my .zshrc file now

export PATH="/usr/local/opt/python/libexec/bin:$PATH"
export PATH="/usr/local/opt/mysql-client/bin:$PATH"
export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$PATH"
export PATH="/usr/local/opt//bin:$PATH"

and here is my bash_profile

export PATH="/usr/local/opt/python/libexec/bin:$PATH"
export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"
export PATH="/usr/local/opt/mysql-client/bin:$PATH"

Intro

Are you on a new Mac ARM chip, working on a Django project, and using MySQL as your backend DB? This article is aimed at you.

And maybe, just maybe, you got this weird error when trying to runserver or migrate. "NameError: name ‘_mysql’ is not defined", then this article is definitely for you.

Photo by Ryan Snaadt on Unsplash

I'm not one to give long introductions about why I'm working in Django, how long I spent trying to fix this [though I'd love to brag about it], or giving you some life story like the blogs for recipes do, so let's get to it.

Resource

A man named Adam Johnson comes to our rescue here, and the problem solution is listed in more depth here.

Installation

That article pretty much outlines it. I've tried this solution using both Conda and Pipenv, and it's worked in both. So, from an environment perspective we have something that should work at least from these two approaches.

To install using Pipenv:

Navigate to the folder where your environment is in

pipenv shell

pipenv install pymysql

To install using Conda:

conda activate {env_name}

pip install pymysql

Note: I pretty much use pip for everything and I avoid Homebrew like the plague because it frequently causes clashes between Brew and Conda. I manage my environments in CondaForge, download packages from pip where I can, and use Forge for everything else.

The Code

That's the installation. Now, for the code part, it's pretty simple.

At the beginning of the settings.py in my main folder, I add:

import pymysql

Followed by, somewhere near the top of this file [I added it after the databases section]:

# Fake PyMySQL’s version and install as MySQLdb

# //adamj.eu/tech/2020/02/04/how-to-use-pymysql-with-django/

pymysql.version_info = [1, 4, 2, “final”, 0]

pymysql.install_as_MySQLdb[]

Sam, Why Does this Work?

Adam says it much better than I can even begin to explain.

Conclusion

Now, you should be able to do whatever you needed to do. I was migrating when I discovered the error and was able to migrate after this.

Part of learning is debugging, and a lot of debugging is persistence. Hang in there, if this doesn't fix your problem, maybe it will give you a different error message. We will consider a different error message progress.

If you benefitted from this story, send me a message via Medium or LinkedIn! I am an extrovert and thrive on making connections. I also believe connections open the door to discover what we want to do in life.

Until next debug session,
Sam

Có ai tìm thấy giải pháp không? Tôi đang vật lộn với vấn đề này, tôi đã cài đặt M1 Python 3.9 bản địa, dự án hoạt động hoàn hảo với SQLite nhưng khi tôi chuyển cơ sở dữ liệu sang MySQL mặc dù tôi đã tải xuống MySQL Client, tôi nhận được lỗi dưới đây [Venv] [cơ sở] % python3 Management.py Makemigrations Traceback [cuộc gọi gần đây nhất cuối cùng]: file "/người/salishkumar/backend/tiffinwala/venv/lib/python3.9 Nhập _MysQL ItrongRor: dlopen [/sers/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/mysqldb/_mysql.cpython-39-darwin. '
I have installed native m1 python 3.9, project working perfectly with sqlite but when I switch database to mysql although I have downloaded mysql client
I get below error
[venv] [base] salishkumar@Salishs-MacBook-Pro tiffinwala % python3 manage.py makemigrations
Traceback [most recent call last]:
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/MySQLdb/init.py", line 18, in
from . import _mysql
ImportError: dlopen[/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/MySQLdb/_mysql.cpython-39-darwin.so, 0x0002]: symbol not found in flat namespace '_mysql_affected_rows'

Trong quá trình xử lý ngoại lệ ở trên, một ngoại lệ khác đã xảy ra:

Traceback [cuộc gọi gần đây nhất cuối cùng]: file "/người sử dụng Trong tệp thực thi chính_from_command_line [sys.argv] tệp "/người/người/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages [] Tệp "/người sử dụng /SalishKumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/init.py ", dòng 24, trong appup apps.population [settings.installed_apps] VenV/lib/python3.9/trang web trang web/django/apps/registry.py ", dòng 114, trong tập tin dân cư app_config.import_models [] trang web-packages/django/apps/config.py ", dòng 300, trong intert_models self.models_module = intert_module [mo dels_module_name] Tệp "/người sử dụng/salishkumar/co dòng 1030, trong tệp _gcd_import "", dòng 1007, trong tệp _find_and_load "", dòng 986, trong tệp _find_and_load_unlocked "", dòng 680, trong tệp _load_unlocked "" Tệp "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages Tệp "/Users /SalishKumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/models/base.py ", dòng 122, trong new_class.add_to_class ['_ meta' Tệp "/Người dùng/Salishkumar/Backend/Tiffinwala/Ven v/lib/python3.9/trang web gói/django/db/model/base.py ", dòng 326, trong add_to_class value.contribution_to_class [cls, tên] tệp" hoặc phụ trợ/tiffinwala/venv/lib/python3.9/trang web trang web/django/utils/kết nối.py ", dòng 15, trong getattr return getattr [self._connections [self._alias] /backend/tiffinwala/venv/lib/python3.9/site-packages/django/utils/connection.py ", dòng 62, trong getItem Conn = self.create_connection [bí danh] Venv/lib/python3.9/trang web trang web/django/db/utils.py ", dòng 204, trong create_connection backend = load_backend [db ['động cơ']] lib/python3.9/trang web trang web/django/db/utils.py ", dòng 111, trong load_backend return_module ['%s.base ' % backend_name] Tệp "/người sử dụng /Users /tiffinwala/venv/lib/python3.9/site-packages/mysqldb/init.py ", dòng 24, trong phiên bản_info, _mysql.Version_info
File "/Users/salishkumar/backend/tiffinwala/manage.py", line 22, in
main[]
File "/Users/salishkumar/backend/tiffinwala/manage.py", line 18, in main
execute_from_command_line[sys.argv]
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/core/management/init.py", line 425, in execute_from_command_line
utility.execute[]
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/core/management/init.py", line 401, in execute
django.setup[]
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/init.py", line 24, in setup
apps.populate[settings.INSTALLED_APPS]
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models[]
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/apps/config.py", line 300, in import_models
self.models_module = import_module[models_module_name]
File "/Users/salishkumar/opt/anaconda3/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import[name[level:], package, level]
File "", line 1030, in _gcd_import
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "", line 850, in exec_module
File "", line 228, in _call_with_frames_removed
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/contrib/auth/models.py", line 3, in
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 47, in
class AbstractBaseUser[models.Model]:
File "/Users/salishkumar/backend/tiffinwala/venv/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/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/models/base.py", line 326, in add_to_class
value.contribute_to_class[cls, name]
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/models/options.py", line 207, in contribute_to_class
self.db_table = truncate_name[self.db_table, connection.ops.max_name_length[]]
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/utils/connection.py", line 15, in getattr
return getattr[self._connections[self._alias], item]
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/utils/connection.py", line 62, in getitem
conn = self.create_connection[alias]
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/utils.py", line 204, in create_connection
backend = load_backend[db['ENGINE']]
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/utils.py", line 111, in load_backend
return import_module['%s.base' % backend_name]
File "/Users/salishkumar/opt/anaconda3/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import[name[level:], package, level]
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 15, in
import MySQLdb as Database
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/MySQLdb/init.py", line 24, in
version_info, _mysql.version_info, _mysql.file
NameError: name '_mysql' is not defined

Bài Viết Liên Quan

Chủ Đề