简体   繁体   中英

Python - import mysql - IDLE/works VisualStudio/dosn't work

Simple code, which I use to study python. The code work in the default editor IDLE from python installation, but in Visual Studi Code doesn't work. I have instaled mysql, mysql-connector, mysql-connector-python...

import mysql.connector
from mysql.connector import Error

def connect():
    try:
        conn = mysql.connector.connect(host='localhost', database='test', user='root', password='*******')
        if conn.is_connected():
            print('Connected to MySql database')
    except Error as e:
        print(e)
    finally:
        conn.close()
connect()

IDLE VISUAL STUDIO CODE

Installed packages
C:\mp>pip list
Package                  Version
------------------------ -------
cachetools               4.0.0
dnspython                1.16.0
google-api-python-client 1.7.11
google-auth              1.10.2
google-auth-httplib2     0.0.3
httplib2                 0.16.0
mypy                     0.761
mypy-extensions          0.4.3
**mysql                    0.0.2
mysql-connector          2.2.9
mysql-connector-python   8.0.19
mysqlclient              1.4.6**
oauth2client             4.1.3
pip                      20.0.1
protobuf                 3.6.1
pyasn1                   0.4.8
pyasn1-modules           0.2.8
PyDrive                  1.3.1
PyYAML                   5.3
rsa                      4.0
setuptools               41.2.0
six                      1.14.0
typed-ast                1.4.1
typing-extensions        3.7.4.1
uritemplate              3.0.1  

The problem was the name of the file which contained the code in Visual Studio code.

I try to "import mysql" and the name of the file which contains the main training code is mysql.py

After the change of name was done(mysql.py > mysqltest.py), the code begins to work. That means when you try to import something in your program, python first checks the folder of the programs and after that the PAHT of libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM