mysql-connector won't import into script in pycharm

13,098

Solution 1

You need to import the connector as

import mysql.connector

Check the examples in the docs for this

If that doesn't work, then there might be an inconsistency between the interpreter that pycharm uses and the one you installed the package for. In pycharm, go to

File->Settings->Project Interpreter

In the terminal, enter

where python #Windows
which python #Linux

and also

where/which pip

make sure that the interpreter configured in pycharm is the same that appears when typing which/where python in the command line/shell. Also make sure that pip also points to the same python distribution.

Solution 2

It may be because you are using a virtual environment inside pyCharm, while you might have installed the library using system's default pip.

Check Preferences->Project->Python Interpreter inside Pycharm, and see if your library is listed there. If not, install it using + icon. Normally, if you use pyCharm's inbuilt terminal, it is already using the same virtual env as your project. So using pip there may help.

Usage syntax is as below:

import mysql.connector

conn = mysql.connector.connect(
         user='root',
         password='#####',
         host='127.0.0.1',
         database='some_db')

conn.close()

Solution 3

Go to project interpreter and download mysql-connector.You need to install it also in pycharm

Share:
13,098

Related videos on Youtube

samueljames3
Author by

samueljames3

MySQL novice, Python newb. Having fun learning.

Updated on June 04, 2022

Comments

  • samueljames3
    samueljames3 about 2 years

    I have successfully installed mysql-connector using pip.

    Installing collected packages: mysql-connector
    Running setup.py install for mysql-connector ... done
    Successfully installed mysql-connector-2.1.6
    

    However, in PyCharm when I have a script that uses the line:

    import mysql-connector
    

    PyCharm gives me an error saying there isn't a package called "mysql" installed. Is there some sort of syntax that should be used to indicate that the entire package name contains the "-" and is not just "mysql"?

    When I run my script in IDLE, mysql.connector imports just fine. (I changed it to mysql-connector after seeing the "-" in the name of the package and having trouble in PyCharm.)

    EDIT: per @FlyingTeller's suggestions, in the terminal, "where python" returns C:...Programs\Python\Python36-32\python.exe. "where pip" returns ...Python\Python36-32\Scripts\pip.exe. The interpreter in PyCharm for this project is this same filepath & exe as "where python" in the terminal.

    Per @Tushar's comment, this program isn't using a virtual environment and the mysql-connector library is already present in the Preferences->Project->Python Interpreter.

    Thanks for the feedback and any additional guidance you may be able to provide.

    • FlyingTeller
      FlyingTeller almost 6 years
      If you have a comment to an answer, then pls don't add it to your question, because the author of the answer will not be notified in any way. I just stumpled on this by pure chance. Always comment on the respective post if you want to adress the author of it. Adding additional info to your question to indicate what you have tried so far is of course good and needed, but you should also comment on an answer to notify the author that he/she should take another look
    • FlyingTeller
      FlyingTeller almost 6 years
      besides from that, have you also changed your import to import mysql.connector?
    • samueljames3
      samueljames3 almost 6 years
      I am kinda new to these forums. A sincere thanks for the guidance.
  • samueljames3
    samueljames3 almost 6 years
    in the terminal, "where python" returns C:...Programs\Python\Python36-32\python.exe. "where pip" returns ...Python\Python36-32\Scripts\pip.exe. The interpreter in PyCharm for this project is this same filepath & exe as "where python" in the terminal. <br/> I did change the import in my mail script file to mysql.connector, and ignored the error that PyCharm was giving me "Package requirement 'mysql' is not satisfied". And everything ran properly. So perhaps this is an instance where I should "Ignore requirement" in PyCharm?