ModuleNotFoundError:没有名为“mysql”的模块;已安装 mysql-connector-python

发布于 2025-01-17 03:45:01 字数 988 浏览 3 评论 0原文

我正在尝试将 python 脚本连接到 RaspberryPi4 上的 MySQL 或 MariaDB 服务器。

我的 python 脚本现在只包含 import mysql.connector。但是当我尝试通过 sudo python3 startdb.py 启动它时,我只是收到 import mysql.connector ModuleNotFoundError: No module named 'mysql' 的错误。

当我通过 sudo python startdb.py 启动脚本时,出现另一个错误:import mysql.connector ImportError: No module named mysql.connector

我在许多网站或论坛上搜索了解决方案。我大多只是找到各种版本的 pip install mysql-connector-python (也有 pip3mysql-connector-python-rf>mysql-connector) 来运行,但它们都不适合我。我认识到的唯一区别是,我之前使用 sudo pythonsudo python3 都收到了错误 ModuleNotFoundError,但现在我只使用 <代码>sudo python3。

有谁知道如何解决这个问题?

我的脚本不在 /home/pi/ 的子目录中,而是在 /home/ 中,这可能是问题所在吗?

编辑:我只是尝试使用鼠标通过桌面模式执行脚本,然后单击“运行”,它就起作用了。但是当我在桌面模式或 SSH 会话中使用命令行时,它不起作用。

另一个编辑:看起来当我在没有 sudo 的情况下启动脚本时它会工作得很好。其实不知道为什么,但我现在很好。但了解和理解为什么 sudo 会使其“崩溃”会非常有趣。

谢谢并很高兴听到一些解决方案:D

Cooki

I'm trying to connect my python scripts to an MySQL or MariaDB Server on my RaspberryPi4.

My python script right now just contains import mysql.connector. But when I try to start it via sudo python3 startdb.py I just get import mysql.connector ModuleNotFoundError: No module named 'mysql' as an error.

I get an other error, when I start the script via sudo python startdb.py: import mysql.connector ImportError: No module named mysql.connector.

I searched for a solution on many sites or forums. I mostly just found various versions of pip install mysql-connector-python (also with pip3, mysql-connector-python-rf or mysql-connector) to run but none of them worked for me. The only difference I recognized is that I previously got the error ModuleNotFoundError with both sudo python and sudo python3, but now I only get it with sudo python3.

Does anyone know how to solve this?

Could the fact that my script isn't in a sub-directory of /home/pi/, but instead of /home/, be the problem?

Edit: I just tried executing the script via the desktop mode using my mouse and just clicking on run and it worked. But when I'm using the command line in desktop mode or with a SSH session it doesn't work.

Another Edit: It looks like when I'm starting the script without sudo it'll work just fine. Don't actually know why's that, but I'm good for now. But would be very interesting to know and understand why the sudo makes it "crash".

Thanks and happy to hear some solutions :D

Cooki

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

匿名。 2025-01-24 03:45:01

raspbian 在运行中提供用户模式,只是在桌面中向用户提供一些权限,以 root 身份运行应用程序以访问所有必要的属性,在下载和安装项目包时使用 sudo 执行所有初始步骤

raspbian give user mode in running, just in Desktop gives some permission to user for run app as root to access all necessary attributes , use sudo with all initial steps when you download and install project package's

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