0

Occasionally my PiHole goes down and I'm unavailable to resolve any sites, it just did it the other day for hours before I noticed.

I'm trying to run this to catch the times that I miss it so it doesn't mess up anything for anyone else on the network.

For some reason, it's telling me on line 1, no module named Requests when I already have it.

Verified by running pip show requests and it shows version 2.25.1, installed using sudo pip install requests.

I'm running Python 2.7.18. I have another Pi I tested it on which doesn't seem to show any problems but I have omv installed on it so I couldn't test with Pihole without fear of breaking something.

Any advice on how to get the code working? Thanks.

In the end I'm planning to have a cronjob that will start on reboot, launch tmux and run this.

Edit: It looks like I already had requests installed as a dependency for another program.

The location is /home/usr/.local/lib/python3.9/site-packages

import requests
import subprocess
import time

def check_reachability(url):
  try:
    response = requests.get(url)
    if response.status_code == 200:
      return True
    else:
      return False
  except:
    return False

def reboot_pihole():
  subprocess.run(["pihole", "restartdns"])

while True:
  reachable = check_reachability("htts://google.com")
  if not reachable:
    reboot_pihole()
  time.sleep(60) # check every 60 seconds
  • 1
    "No module named requests" isn't a problem caused by your code, so just showing the code to your application doesn't help us solve it. We need to know _how you installed `requests`_, _where_ it's installed, and how the `sys.path` active when it can be found compares with the one that's active when it can't. – Charles Duffy Jan 03 '23 at 20:26
  • 1
    (If you used a `pip` tied to one version of Python, but then a `python` executable from another, that's a common way to get this problem; likewise installing into a virtualenv but trying to run against a system interpreter or the inverse; but right now the question has no details relevant to detecting any of those scenarios. See [mre] guidelines: We want the _shortest possible_ code or set of steps we can use to cause the same problem to happen somewhere we can see it ourselves). – Charles Duffy Jan 03 '23 at 20:27
  • 1
    BTW, Python 2.x is out-of-support; you should really try to move to Python 3. (And if the copy of `pip` you ran was installing packages for a Python 3 interpreter rather than for your Python 2.7.18, that would explain the problem). – Charles Duffy Jan 03 '23 at 20:28

1 Answers1

0

I solved it by just running

python3 program.py

instead of

python program.py

Edit: The program was written for Python 3 when I have both Python 2.7 and 3 installed.