1

Goal.

I want to build a small weatherstation using a Raspberry Pi 3 B. For measuring temperature and humidity I use an Adafruit DHT22 sensor. There is a Python library to read the sensor.

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

After making a measurement the measurement data shall be sent to a server.

What I did.

This Adafruit_DHT library is quite picky. For development I had the following setup:

  • Plan: Develop on a laptop not directly on the Pi. Save everything to Github and then just call git clone ... on Pi to get the code.

  • Since using pipenv is a recommended workflow I tried to write the code on my laptop and test it using a dummy function for the acutal measurements. That did not work since upon calling pipenv install Adafruit_DHT there was an error because Adafruit_DHT only works on a Pi or an Beaglebone Black.

  • So, I just wrote the code on my laptop and pushed it to Github without having tested it.

  • Then I used ssh to connect to the Pi and git clone ... everything.

  • pipenv install worked.

  • pipenv run python measurements.py worked too.

  • Then I wanted to start the script upon startup. I read https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/

  • For this I would need to use pipenv run /home/pi/.../main.py, maybe even with superuser privileges.

Encountered Problem.

It does not work since pipenv tries to create a new virtual environment all the time. I did not find anythink like yarn --cwd as in Run yarn in a different path .

Then I tried pipenv install --system I got

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...

Some how the hash changed. How can that happen? (btw, I ran pipenv install just 1 minute before pipenv install --system).

Questions.

  1. What would be a recommended way to run this program on startup?

  2. My main problem is not the problem itself. Maybe, I could get it to work without pip or pipenv. Nevertheless, I like sandboxing. In C++ for example one would compile it -> just one executable.

  3. What about freezing the program with e.g. pyInstaller? How to deal with pipenv and pyInstaller at the same time.

I am writing this post, since I have already encountered much simpler workflows like Javascript + nodeJs + yarn.

I'd appreciate any help.

Michael S
  • 466
  • 1
  • 4
  • 12

1 Answers1

0

For question 1 there's couple ideas discussed in Issue on pipenv's Github: recommended method of use in crontabs? · Issue #1369

Suggested option for activating the correct env is to cd into the directory before using pipenv run, something like

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

Other options are to activate the virtual env without pipenv

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

or point directly to the Python inside virtualenv

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