python'no模块名为mysql.connector;在Raspberry Pi上运行脚本时
我写了一个Python脚本,使用Python-3.7.3在Raspberry Pi 3上运行MariaDB软件包。脚本如下:
#============================================================================
# Test Snippet #1
import mysql.connector
from mysql.connector
import connect, errorcode, Error
############################################################################
# Begin execution here:
print (">> Program Start >>")
#---------------------------------------------------------------------------
# Connect to the test database:
try:
dbConn=\
connect (user='WebServer', password='$$$$$$$$', host='localhost', \
database='HamRadio')
except Error as err:
print ("! Error #", err.errno, \
" encountered when connecting to test database !")
#---------------------------------------------------------------------------
# Search the test table for certain entries:
curSel=dbConn.cursor ()
strStatement="SELECT * FROM HamRadio.Log WHERE Call_Sign like 'OK%' " + \
"ORDER BY Call_Sign"
try:
curSel.execute (strStatement)
except Error as err:
print ("! Error #", err.errno, " encountered when executing SELECT command !")
#---------------------------------------------------------------------------
# Display the rows selected:
for row in curSel:
print ("Row Contents=", row)
#---------------------------------------------------------------------------
# Close the database table:
curSel.close ()
如果我使用Python3 IDE(Python Shell)执行此脚本,则该脚本可以正常工作。
但是,如果我尝试从命令行执行相同的脚本:
pi@raspberrypi:〜$ sudo python testsnippet_1.py
显示以下错误消息:
'''''
Traceback (most recent call last):
File "TestSnippet_1.py", line 4, in <module>
import mysql.connector
ImportError: No module named mysql.connector*
'''' 我怀疑这个问题可能与某种环境路径有关,但是我缺乏在覆盆子PI环境中使用Python的经验来充分了解正在发生的事情。
任何帮助都将不胜感激,因为我在这一点上处于停滞状态。
I wrote a Python script to test the MariaDB package using Python-3.7.3 running on a Raspberry Pi 3. The script is shown below:
#============================================================================
# Test Snippet #1
import mysql.connector
from mysql.connector
import connect, errorcode, Error
############################################################################
# Begin execution here:
print (">> Program Start >>")
#---------------------------------------------------------------------------
# Connect to the test database:
try:
dbConn=\
connect (user='WebServer', password='$$$$', host='localhost', \
database='HamRadio')
except Error as err:
print ("! Error #", err.errno, \
" encountered when connecting to test database !")
#---------------------------------------------------------------------------
# Search the test table for certain entries:
curSel=dbConn.cursor ()
strStatement="SELECT * FROM HamRadio.Log WHERE Call_Sign like 'OK%' " + \
"ORDER BY Call_Sign"
try:
curSel.execute (strStatement)
except Error as err:
print ("! Error #", err.errno, " encountered when executing SELECT command !")
#---------------------------------------------------------------------------
# Display the rows selected:
for row in curSel:
print ("Row Contents=", row)
#---------------------------------------------------------------------------
# Close the database table:
curSel.close ()
If I execute this script using the Python3 IDE (Python shell), the script works correctly.
However, if I attempt to execute the same script from the command line:
pi@raspberrypi:~ $ sudo python TestSnippet_1.py
The following error message is displayed:
'''
Traceback (most recent call last):
File "TestSnippet_1.py", line 4, in <module>
import mysql.connector
ImportError: No module named mysql.connector*
'''
I suspect that the issue may be related to some sort of environment path but I lack the experience using Python in a Raspberry Pi environment to fully understand what is happening.
Any help would greatly be appreciated because I am at a standstill at this point.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过数小时的调查,我确定误差的原因是在命令行中使用' sudo '(它调用了不同的路径变量集)!呢仅键入: python testsnippet_1.py 而不是 sudo python testsnippet_1.py 使程序能够执行无误!!非常微妙,但非常致命!
After many hours of investigation, I determined that the cause of the error was the use of 'sudo' in the command line (which invokes a different set of path variables)!! Typing just: python TestSnippet_1.py instead of sudo python TestSnippet_1.py enables the program to execute with no errors!! Very subtle but very deadly!!