python'no模块名为mysql.connector;在Raspberry Pi上运行脚本时

发布于 2025-01-27 20:10:24 字数 2092 浏览 3 评论 0原文

我写了一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念绕指尖 2025-02-03 20:10:24

经过数小时的调查,我确定误差的原因是在命令行中使用' 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!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文