python脚本在启动时从终端执行时无法找到软件包,我在做什么错? [raspbian]

发布于 2025-02-04 14:38:04 字数 774 浏览 2 评论 0原文

抱歉,如果这是显而易见的或多余的,我一直在此网站上寻找解决此问题的答案,但是我找不到任何可以解决

我要实现的目标的东西,就是当我的Raspberry Pi时执行Python脚本打开(桌面加载后),

我一直在尝试通过在/home/pi/.bashrc的末尾添加这些行来做到这

echo Running at boot

sudo /usr/bin/python3.7m  /home/pi/Desktop/Program/NameOfTheProgram.py

一点。标题是,当我打开终端时执行脚本,因此在启动时,python似乎无法找到包裹

    from SSIM_PIL import compare_ssim as ssim #Import ssim from ssim package
ModuleNotFoundError: No module named 'SSIM_PIL'

使用从终端执行脚本时不会出现此问题

/usr/bin/python3.7m  /home/pi/Desktop/Program/NameOfTheProgram.py

时,当我通过在这种情况下 该程序是按预期执行的,所以我唯一的猜测是我使用的python版本不应该是问题,因为我100%确定它是相同的,而且显然应该工作,但是我可能会误解

我也试图将相同的代码行放入/etc/rc.local中,但我毫无用处,

我缺少某些东西吗?先感谢您

sorry in case this is obvious or redundant, I've been looking around on this site for an answer to this problem but I couldn't find anything that would fix it

What I am trying to achieve is executing a Python script when my raspberry pi turns on (after the desktop loads in)

I've been trying to do so by adding these lines at the end of /home/pi/.bashrc

echo Running at boot

sudo /usr/bin/python3.7m  /home/pi/Desktop/Program/NameOfTheProgram.py

The problem, as you might've guessed from the title, is that when the script is executed as I open the terminal, and consequently at bootup, python doesn't appear to be capable of finding a package

    from SSIM_PIL import compare_ssim as ssim #Import ssim from ssim package
ModuleNotFoundError: No module named 'SSIM_PIL'

This problem does not arise when I execute the script from the terminal by using

/usr/bin/python3.7m  /home/pi/Desktop/Program/NameOfTheProgram.py

in which case the program is executed as intended, so my only guess is that this shouldn't be a problem with the python version I'm using, since I'm 100% sure it's the same one and it apparently should work, but I may be mistaken

I've also tried to put those same lines of code in /etc/rc.local, but to no avail

Am I missing something? Thank you in advance

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

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

发布评论

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

评论(2

青巷忧颜 2025-02-11 14:38:04

可以在不同的时刻运行脚本:

  1. 在系统启动时(请参阅 superuser上的答案,或使用cron's @reboot feature ) 。
  2. 当用户登录到他们的外壳时。
  3. 当用户打开新终端时。
  4. 在任意给定时间(使用croncrontab;读取手册页面!),

涉及2和3;这在某种程度上取决于您使用的外壳。
阅读用户外壳的手册,并在文件部分中查看。

如果脚本不需要根特权,我喜欢使用cron,因为它支持运行命令作为非root用户。

It is possible to run scripts at different moments:

  1. At system boot (see this answer on superuser, or use cron's @reboot feature).
  2. When users log in to their shell.
  3. When users open a new terminal.
  4. On an arbitrary given time (using cron and crontab; read the manual pages!)

Concerning 2 and 3; this depends somewhat on the shell you use.
Read the manual of the user's shell and look in the FILES section.

If a script does not require root privileges, I'm a fan of using cron, because it supports running commands as a non-root user.

栀梦 2025-02-11 14:38:04

再次是我,就像Roberto指出的那样,从命令中删除sudo确实可以在打开终端窗口时执行程序确实有效

,但不幸的现在需要做的是找到一种使用终端命令打开终端窗口的方法,并且该程序应正常工作,

这是可以承认的,但是只要程序可以

在启动i'上打开终端i'' VE在lxSession文件夹中编辑了autoStart文件,通过向其添加@lxtryninal,如这个答案

我正在回答自己的问题,因为我不知道如何将评论标记为“答案”

It is me again, like Roberto pointed out removing sudo from the command did indeed work to execute the program when a terminal window is opened

Unfortunately this didn't initiate the program at startup, but in theory all I need to do now is find a way to open a terminal window with a terminal command, and the program should be working correctly

This is admittedly a bit contrived, but it's good enough as long as the program works

To open the terminal on boot I've edited the autostart file in the lxsession folder by adding @lxterminal to it, as illustrated in this answer

I'm answering to my own question as I don't know how to mark a comment as "answer", if that's even possible

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