---
slug: "python3-symbol-not-found-in-flat-namespace-_mysql_affected_rows"
title: "対応方法 When using MySQLdb with Python and encountering \"symbol not found in flat namespace '_mysql_affected_rows'\""
description: "Fix `symbol not found in flat namespace '_mysql_affected_rows'` when importing MySQLdb in Python — rebuild mysqlclient against the right MySQL client library."
url: "https://www.ytyng.com/en/blog/python3-symbol-not-found-in-flat-namespace-_mysql_affected_rows"
publish_date: "2023-11-09T03:39:23Z"
created: "2023-11-09T03:39:23Z"
updated: "2026-05-11T13:03:57.557Z"
categories: ["Django", "Python"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20250503/5d3ab2b6eb504f0dabf659d1a4ef82d3.png.webp?width=768"
has_video: true
has_music: true
video_urls: ["https://media.ytyng.net/ytyng-blog/295/featured-video-1.mp4", "https://media.ytyng.net/ytyng-blog/295/featured-video-2.mp4", "https://media.ytyng.net/ytyng-blog/295/featured-video-3.mp4"]
music_urls: ["https://media.ytyng.net/ytyng-blog/295/featured-music-295-1.mp3", "https://media.ytyng.net/ytyng-blog/295/featured-music-295-4.mp3"]
lang: "en"
---

# 対応方法 When using MySQLdb with Python and encountering "symbol not found in flat namespace '_mysql_affected_rows'"

Attempted to operate MySQL using Python on Apple Silicon, but encountered the following error:

```
ImportError: dlopen(/.../site-packages/MySQLdb/_mysql.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace '_mysql_affected_rows'
```

Referring to the comment in the following link:

[Macos M1 mysqlclient Symbol not found: _mysql_affected_rows ERROR · Issue #496 · PyMySQL/mysqlclient](https://github.com/PyMySQL/mysqlclient/issues/496#issuecomment-1614688099)

In my environment, the output was:

```shell
% echo $MYSQLCLIENT_LDFLAGS
-L/opt/homebrew/opt/openssl/lib -L/opt/homebrew/opt/mysql/lib -L/opt/homebrew/opt/zlib/lib -L/opt/homebrew/lib
```

So, I modified it to:

```shell
% echo $MYSQLCLIENT_LDFLAGS
-L/opt/homebrew/opt/openssl/lib -L/opt/homebrew/opt/mysql/lib -L/opt/homebrew/opt/zlib/lib -L/opt/homebrew/lib -lmysqlclient -rpath /opt/homebrew/opt/mysql/lib
```

After that, I executed the following commands:

```shell
pip uninstall mysqlclient
pip cache purge
pip install mysqlclient
```

If there are no errors when executing:

```shell
python3

>>> import MySQLdb
>>>
```

then it's OK.
