Raspberry Pi - Python Deployment - Pipenv



Goal.

我想用Raspberry Pi 3 B建造一个小型气象站。为了测量温度和湿度,我使用Adafruit DHT22传感器。有一个 Python 库来读取传感器。

https://pypi.org/project/Adafruit-DHT/

测量后,测量数据应发送到服务器。

我做了什么。

这个Adafruit_DHT库非常挑剔。对于开发,我有以下设置:

  • 计划:在笔记本电脑上开发,而不是直接在Pi上开发。将所有内容保存到Github,然后在Pi上调用git clone ...即可获取代码。

  • 由于使用pipenv是推荐的工作流程,因此我尝试在笔记本电脑上编写代码并使用虚拟函数进行测试以进行精确测量。这不起作用,因为在调用pipenv install Adafruit_DHT时出现错误Adafruit_DHT仅适用于 Pi 或 Beaglebone Black。

  • 所以,我只是在我的笔记本电脑上编写了代码,并在没有测试的情况下将其推送到 Github。

  • 然后我用ssh连接到Pi并git clone ...所有内容。

  • pipenv install奏效了。

  • pipenv run python measurements.py也工作了。

  • 然后我想在启动时启动脚本。我读了 https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/

  • 为此,我需要使用pipenv run /home/pi/.../main.py,甚至可能具有超级用户权限。

遇到问题。

它不起作用,因为pipenv一直尝试创建新的虚拟环境。我没有找到任何像yarn --cwd像 Run 纱线一样在不同的路径上的想法 .

然后我尝试了pipenv install --system我得到了

pipenv install --system
Installing dependencies from Pipfile.lock (dcc369)…
An error occurred while installing adafruit-dht==1.4.0 --hash=sha256:e927f2232eff5335cb9d8a2cca6dcad4625e61f205b12e31ef04198ea6dec830! Will try again.
🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 8/8 — 00:00:16
Installing initially failed dependencies…
[pipenv.exceptions.InstallError]:   File "/home/pi/.local/lib/python3.7/site-packages/pipenv/core.py", line 1874, in do_install
[pipenv.exceptions.InstallError]:       keep_outdated=keep_outdated
[pipenv.exceptions.InstallError]:   File "/home/pi/.local/lib/python3.7/site-packages/pipenv/core.py", line 1253, in do_init
[pipenv.exceptions.InstallError]:       pypi_mirror=pypi_mirror,
[pipenv.exceptions.InstallError]:   File "/home/pi/.local/lib/python3.7/site-packages/pipenv/core.py", line 859, in do_install_dependencies
[pipenv.exceptions.InstallError]:       retry_list, procs, failed_deps_queue, requirements_dir, **install_kwargs
[pipenv.exceptions.InstallError]:   File "/home/pi/.local/lib/python3.7/site-packages/pipenv/core.py", line 763, in batch_install
[pipenv.exceptions.InstallError]:       _cleanup_procs(procs, not blocking, failed_deps_queue, retry=retry)
[pipenv.exceptions.InstallError]:   File "/home/pi/.local/lib/python3.7/site-packages/pipenv/core.py", line 681, in _cleanup_procs
[pipenv.exceptions.InstallError]:       raise exceptions.InstallError(c.dep.name, extra=err_lines)
[pipenv.exceptions.InstallError]: ['Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple', 'Collecting adafruit-dht==1.4.0 (from -r /tmp/pipenv-m9p05m29-requirements/pipenv-uuyle1le-requirement.txt (line 1))', '  Using cached https://www.piwheels.org/simple/adafruit-dht/Adafruit_DHT-1.4.0-cp37-cp37m-linux_armv7l.whl']
[pipenv.exceptions.InstallError]: ['THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.', '    adafruit-dht==1.4.0 from https://www.piwheels.org/simple/adafruit-dht/Adafruit_DHT-1.4.0-cp37-cp37m-linux_armv7l.whl#sha256=3a47d226e77186f89bf167a6568d9ae2cab119333c4b3f5a8ec460f9695a832c (from -r /tmp/pipenv-m9p05m29-requirements/pipenv-uuyle1le-requirement.txt (line 1)):', '        Expected sha256 e927f2232eff5335cb9d8a2cca6dcad4625e61f205b12e31ef04198ea6dec830', '             Got        3a47d226e77186f89bf167a6568d9ae2cab119333c4b3f5a8ec460f9695a832c']
ERROR: ERROR: Package installation failed...

哈希如何更改的一些内容。怎么会这样呢?(顺便说一句,我在pipenv install --system前 1 分钟跑pipenv install)。

问题。

  1. 在启动时运行此程序的推荐方法是什么?

  2. 我的主要问题不是问题本身。也许,我可以让它在没有pippipenv的情况下工作.不过,我喜欢沙盒。例如,在C++中,人们将编译它 ->只有一个可执行文件。

  3. 冻结程序怎么样,例如pyInstaller?如何同时处理pipenvpyInstaller

我正在写这篇文章,因为我已经遇到了更简单的工作流程,如Javascript + nodeJs + yarn。

我将不胜感激任何帮助。

对于问题 1,在 pipenv 的 Github 上的问题中讨论了几个想法:推荐在 crontab 中使用的方法? ·问题 #1369

激活正确环境的建议选项是在使用 pipenv run 之前将 cd 放入目录,如下所示

cd /home/pi/.../ && pipenv run main.py

其他选项是在没有管道的情况下激活虚拟环境

source /path/to/virtualenv/activate && python /home/pi/.../main.py

或直接指向 virtualenv 中的 Python

#!/bin/sh
VENV_PYTHON="/path/to/project-hAsHpTH/bin/python"
PROJECT="/path/to/some/project"
SCRIPT="script.py"
cd "${PROJECT}" && "${VENV_PYTHON}" "${SCRIPT}"

最新更新