shell脚本有效,但在启动时无效.[树莓派]



我有一个在树莓pi上运行的python脚本。

我定义了一个launcher.sh文件,并尝试使用sh launcher.sh运行程序

文件

cd /home/pi/Documents
PYTHONPATH=/home/pi/.local/lib/python3.10/site-packages
python3 /home/pi/Documents/myfile.py

我试过chmod launcher.shsudo chmod 775 launcher.sh

但当我在启动时启动启动器时,我会在cronlog中记录一个错误:

导入pandas作为pd ModuleNotFoundError没有名为pandas 的模块

我在Raspberry 3.9和3.10上有两个python版本(不幸的是,没有在环境中(。

pip list给我:pandas 1.4.3

python --version给我Python 3.10.0

python3 --version也给了我Python 3.10.0

/home/pi/.local/lib/中,我确实有一个python 3.10和python 3.9文件夹。两个网站包中都有一个pandas文件夹。我刚刚看到,pyhton3.9文件夹也有一个pandas-1.4.3dist.info文件夹(python3.10没有(。

crontab行如下:

@reboot sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1

我不明白。。。python 3.9文件夹是否正在以某种方式拦截。。。但我确实设置了Pythonpath,并且尝试使用sh launcher.sh运行确实有效,所以我想知道出了什么问题。不知怎么的,我想是用错了蟒蛇。

感谢您的帮助

编辑:我重命名了/home/pi/.local/lib/python3.9以摆脱sidpackage。。。但没有变化

编辑:我试图用更改python 3.10和3.9的优先级

sudo update-alternatives --install $(which python) python $(readlink -f $(which python3.10)) 3
sudo update-alternatives --install $(which python) python $(readlink -f $(which python3.9)) 2

运气不佳

编辑:添加了python3——版本建议

编辑:尝试为python3.9安装pandas时出现错误消息pyhton3.9 -m pip install pandas见下文输出。

几乎是最终版本:。。。。。。。。。。。。

我像这里建议的那样卸载(清除(python,然后通过这个很棒的链接重新安装python3.10不幸的是,在卸载时,我猜python2.7也被卸载了,导致系统在启动时完全失败->在晚上3点钟失去了所有的希望;-(现在重新设置sd映像。

当我用新的图像运行它时,我会更新最后一次

非常感谢大家的关心和参与。我喜欢这个社区

> ERROR: Exception: Traceback (most recent call last):   File
> "/usr/lib/python3/dist-packages/pip/_internal/cli/base_command.py",
> line 223, in _main
>     status = self.run(options, args)   File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py",
> line 180, in wrapper
>     return func(self, options, args)   File "/usr/lib/python3/dist-packages/pip/_internal/commands/install.py",
> line 297, in run
>     session = self.get_default_session(options)   File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py",
> line 78, in get_default_session
>     self._session = self.enter_context(self._build_session(options))   File
> "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py",
> line 88, in _build_session
>     session = PipSession(   File "/usr/lib/python3/dist-packages/pip/_internal/network/session.py",
> line 248, in __init__
>     self.headers["User-Agent"] = user_agent()   File "/usr/lib/python3/dist-packages/pip/_internal/network/session.py",
> line 131, in user_agent
>     zip(["name", "version", "id"], distro.linux_distribution()),   File
> "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 125, in linux_distribution
>     return _distro.linux_distribution(full_distribution_name)   File "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 681, in linux_distribution
>     self.version(),   File "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 741, in version
>     self.lsb_release_attr('release'),   File "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 903, in lsb_release_attr
>     return self._lsb_release_info.get(attribute, '')   File "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 556, in __get__
>     ret = obj.__dict__[self._fname] = self._f(obj)   File "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 1014, in _lsb_release_info
>     stdout = subprocess.check_output(cmd, stderr=devnull)   File "/usr/lib/python3.9/subprocess.py", line 424, in check_output
>     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,   File "/usr/lib/python3.9/subprocess.py", line 528, in run
>     raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command '('lsb_release', '-a')'
> returned non-zero exit status 1.

答案是contab以root身份运行应用程序,因此这些包必须与sudo 一起安装

sudo pip install pandas 

解决了问题。向类似的代码大喊

我在您的脚本中没有看到shabang。

通常,通过cli执行的脚本需要

执行权限:chmod +x。以及在文件开头的shabang#!

#!/usr/bin/env bash 
cd /home/pi/Documents
PYTHONPATH=/home/pi/.local/lib/python3.10/site-packages
python3 /home/pi/Documents/myfile.py

希望这能有所帮助。

最新更新