Search code examples
pythonmysqlpycharm

mysql-connector won't import into script in pycharm


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.


Solution

  • 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()