11
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[ただのメモ] SQLAlchemy create_engine で No module named 'MySQLdb' エラー

Last updated at Posted at 2018-01-11

環境

  • macOS Sierra
  • Python 3.6.3(pipenvからの仮想環境を雰囲気で使用)

No module named 'MySQLdb' エラーでた

$ pipenv install SQLAlchemy
$ pipenv shell
bash-3.2$ python
>>> from sqlalchemy.engine import create_engine
>>> engine = create_engine('mysql://hoge:hoge@localhost/sampledb', echo=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/.local/share/virtualenvs/hoge-dVWIvnjK/lib/python3.6/site-packages/sqlalchemy/engine/__init__.py", line 419, in create_engine
    return strategy.create(*args, **kwargs)
  File "/Users/user/.local/share/virtualenvs/hoge-dVWIvnjK/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 80, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/Users/user/.local/share/virtualenvs/hoge-dVWIvnjK/lib/python3.6/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 102, in dbapi
    return __import__('MySQLdb')
ModuleNotFoundError: No module named 'MySQLdb'

mysql-python と mysqlclient

インターネッツに氾濫する情報からmysql-pythonというキーワードを得てインストールにチャレンジからのConfigParserが見つからないエラー。

$ pipenv install mysql-python
Installing mysql-python…
Collecting mysql-python
  Downloading MySQL-python-1.2.5.zip (108kB)
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/t3/1nvj6g1x08n6yqmnrd4446_m0000gn/T/pip-build-tixjw71o/mysql-python/setup.py", line 13, in <module>
        from setup_posix import get_config
      File "/private/var/folders/t3/1nvj6g1x08n6yqmnrd4446_m0000gn/T/pip-build-tixjw71o/mysql-python/setup_posix.py", line 2, in <module>
        from ConfigParser import SafeConfigParser
    ModuleNotFoundError: No module named 'ConfigParser'

ふたたびインターネッツの海へ。

アベさん「ConfigParserはPython3でconfigparserに名前かわったから、そのパッケージはPython3をサポートしてなそう」
読み方わからない名前のひと「mysql-pythonからフォークされたmysqlclientがPython3サポートしてるからそれ使え」

$ pipenv install mysqlclient
Installing mysqlclient…
Collecting mysqlclient
  Downloading mysqlclient-1.3.12.tar.gz (89kB)
Building wheels for collected packages: mysqlclient
  Running setup.py bdist_wheel for mysqlclient: started
  Running setup.py bdist_wheel for mysqlclient: finished with status 'done'
  Stored in directory: /Users/user/Library/Caches/pip/wheels/df/bb/60/bf7c315cbe163515db1c846e4ffa5557dd785c82e82f3492e8
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.12

再チャレンジ。

$ pipenv shell
bash-3.2$ python
>>> from sqlalchemy.engine import create_engine
>>> engine = create_engine('mysql://hoge:hoge@localhost/sampledb', echo=False)
>>> print(engine)
Engine(mysql://hoge:***@localhost/sampledb)
11
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?