我想创建一个需要连接到MySQL数据库的样本Python程序。
import mysql.connector
# opens the file file.txt in read mode
fileptr = open("sample-table","r")
if fileptr:
print("file is opened successfully")
#running a for loop
for i in fileptr:
print(i) # i contains each line of the file
#closes the opened file
fileptr.close()
但我得到这个错误
Traceback (most recent call last):
File "check-db.py", line 1, in <module>
import mysql.connector
ModuleNotFoundError: No module named 'mysql'
它看起来像所需要的模块不存在。是否有可能包括安装模块时无需root访问权限?
安装PIP是这样的:
apt install python-pip #python 2
apt install python3-pip #python 3
尝试安装mysql
使用这个命令:
pip install pymysql