我使用pip在python中安装了mysql插件和lib,当我在pycharm中使用它时它正在工作。但是如果我尝试使用命令python和文件名使用cmd提示执行相同操作,则会抛出错误,说明"没有模块名称' mysql'" 。所以我也在python34目录的libs中检查了它,我可以在那里看到可用的包。我无法弄清楚问题是什么,以及如何克服这个问题。 这是我的代码,它在pycharm中正常工作但如果我尝试使用cmd提示符执行它会抛出错误
import mysql.connector
from mysql.connector import errorcode
class DBConnection:
global connect
global config
def __init__(self):
"Setting the paramenter to set the DB"
config = {
'user': 'root',
'password': 'VMware@1234',
'host': '127.0.0.1',
'database': 'super_nova',
'raise_on_warnings': True,
};
def connectToDB(self):
"connection of the database is done here"
try:
#connect = mysql.connector.connect(**config)
connect = mysql.connector.connect(user='root', password='VMware@1234',host='127.0.0.1' ,database='super_nova')
return connect
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
def closeDB(self,connection):
"Make sures the connections is closed"
connection.close()
from com.test import test2
def fun():
test2.DBConnection().connectToDB()
if __name__ == '__main__':
fun()
错误信息是:
from com.test import test2 File "C:\Users\nkumarn\TestPythons\com\test\test2.py", line 1, in <module> import mysql.connector ImportError: No module named 'mysql'
请帮我解决一下。 感谢您抽出宝贵时间阅读本文。