techouse / mysql-to-sqlite3

Transfer data from MySQL to SQLite
https://techouse.github.io/mysql-to-sqlite3/
MIT License
217 stars 31 forks source link

ImportError: cannot import name 'CMySQLConnection' from 'mysql.connector' #50

Closed robbinonline closed 1 year ago

robbinonline commented 1 year ago

I try install this on my linux vm but I get this error when I try to run mysql2sqlite

(venv) ➜  ~ mysql2sqlite --help
Traceback (most recent call last):
  File "/usr/bin/mysql2sqlite", line 5, in <module>
    from mysql_to_sqlite3.cli import cli
  File "/usr/lib/python3.11/site-packages/mysql_to_sqlite3/__init__.py", line 4, in <module>
    from .transporter import MySQLtoSQLite
  File "/usr/lib/python3.11/site-packages/mysql_to_sqlite3/transporter.py", line 16, in <module>
    from mysql.connector import CMySQLConnection, MySQLConnection, errorcode
ImportError: cannot import name 'CMySQLConnection' from 'mysql.connector' (/usr/lib/python3.11/site-packages/mysql/connector/__init__.py)

I have installed it like this:

python3 -m venv env
source env/bin/activate
pip install mysql-to-sqlite3
mysql2sqlite --help
techouse commented 1 year ago

What's your MySQL connector version?

robbinonline commented 1 year ago
(venv) ➜  ~ pip install mysql-connector-python
Requirement already satisfied: mysql-connector-python in ./venv/lib/python3.11/site-packages (8.0.33)
Requirement already satisfied: protobuf<=3.20.3,>=3.11.0 in ./venv/lib/python3.11/site-packages (from mysql-connector-python) (3.20.3)
techouse commented 1 year ago

Looks like for some reason

from mysql.connector import CMySQLConnection

works on macOS but not on your specific Linux installation. Oddly enough also the tests made on Ubuntu Linux passed.

I made the import more specific now

from mysql.connector.connection_cext import CMySQLConnection

Would you mind sharing your specific Linux flavor & Python setup?

techouse commented 1 year ago

Fix released in v2.0.2

robbinonline commented 1 year ago

The error is gone, only a new came up

➜ ~ mysql2sqlite --help
Traceback (most recent call last): File "/usr/lib/python3.11/site-packages/mysql/connector/connection_cext.py", line 64, in import _mysql_connector ModuleNotFoundError: No module named '_mysql_connector'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/bin/mysql2sqlite", line 5, in <module>
    from mysql_to_sqlite3.cli import cli
  File "/usr/lib/python3.11/site-packages/mysql_to_sqlite3/__init__.py", line 4, in <module>
    from .transporter import MySQLtoSQLite
  File "/usr/lib/python3.11/site-packages/mysql_to_sqlite3/transporter.py", line 17, in <module>
    from mysql.connector.connection_cext import CMySQLConnection
  File "/usr/lib/python3.11/site-packages/mysql/connector/connection_cext.py", line 85, in <module>
    raise ImportError(
ImportError: MySQL Connector/Python C Extension not available (No module named '_mysql_connector')
techouse commented 1 year ago

Hmm, sounds similar to this https://www.reddit.com/r/learnpython/comments/tcngxh/mysql_connectorpython_c_extension_not_available/

Can you re-install the Python MySQL connector using pip?

pip install --force-reinstall mysql-connector-python

Also, would you mind sharing your specific Linux flavor & Python setup cause it looks like your installation is somehow broken.

robbinonline commented 1 year ago

Unfortunately, that is not the solution. I try to uninstall, reinstall. When I use it after uninstall I got the error:

ModuleNotFoundError: No module named 'mysql'

and after install the same error.

Python version: 3.11.3 on Home Assistant OS 10.3 I came here after this topic: https://community.home-assistant.io/t/migrating-from-mysql-mariadb-back-to-sqlite/545837/52

The original error is also mentioned there, but also people execute with succes

techouse commented 1 year ago

I've swapped out

from mysql.connector.connection_cext import CMySQLConnection

for

from mysql.connector.abstracts import MySQLConnectionAbstract

and will release it in v2.0.3.

techouse commented 1 year ago

Home Assistant OS 10.3

Ah, so that could be the issue. I am only testing against Ubuntu and have no clue what this Linux flavor is.

Anyhow, you can always use the v1 branch. In v2 I threw all of the deprecated old code out.

techouse commented 1 year ago

Fix released in v2.0.3.

robbinonline commented 1 year ago

Use the v1 branch was the trick, now everything works fine and migration was successful :)

techouse commented 1 year ago

Do you mind trying it with v2.0.3? I'm curious if it will work.

robbinonline commented 1 year ago

Yes it works now with v2.0.3 at least the command: mysql2sqlite --help I have not done the migration again, but I assume that it will work now.

techouse commented 1 year ago

Cool. It's interesting that this particular Linux distro doesn't support the MySQL C extension.