Knight_Crawler
Knight_Crawler

Reputation: 13

Python script module not found on rerun of script

I am running a python script on RaspberryPi 4, and today when I ran the script again, it showed me the error ModuleNotFoundError: No module named 'adafruit_ssd1306' although I ran this same script yesterday about 10 times, it ran perfectly without any errors. I did not change anything in the script, not even its location. I tried force reinstalling the library, running the script from another location, rebooting the pi, running it as sudo but none of them worked either. By using pip3 freeze it shows that the package is installed and trying to install the package again says that the requirement is already satisfied.

Python version: 3.7

Main.py

import time
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
import requests

WIDTH = 128
HEIGHT = 64  # Change to 64 if needed
BORDER = 5

BASE_URL = "https://api.openweathermap.org/data/2.5/weather?"
API_KEY = "API Key"
sign = '°'

# Use for I2C.
i2c = board.I2C()
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C)

dir_pin = digitalio.DigitalInOut(board.D23)
step_pin = digitalio.DigitalInOut(board.D24)
call_b = digitalio.DigitalInOut(board.D18)

dir_pin.direction = digitalio.Direction.INPUT
step_pin.direction = digitalio.Direction.INPUT
call_b.direction = digitalio.Direction.INPUT
dir_pin.pull = digitalio.Pull.UP
step_pin.pull = digitalio.Pull.UP

prev_value = True

cities = ['Delhi','Tokyo','London','San Francisco',
        'kanpur','Mumbai','portugal','Toronto','Sydney','Paris','New York','Dubai',
        'Moscow','Rome','Madrid','Riyadh','Bangkok','Cairo','Istanbul','Kolkata',
        'Beijing','Seoul','Shanghai','Osaka','Hong Kong','Bangalore','Kathmandu',
        'Chennai','Kathmandu','Manila','Jakarta','Tehran','Baghdad','Riyadh','Abu Dhabi',
        'Dubai','Kuala Lumpur','Chennai','Kathmandu','Manila','Jakarta','Tehran','Baghdad']

font_size = 12
font = ImageFont.truetype('/home/pi/Desktop/Poppins-Regular.ttf', font_size)
font_big = ImageFont.truetype('/home/pi/Desktop/Poppins-Regular.ttf', font_size+4)
font_xl = ImageFont.truetype('/home/pi/Desktop/Poppins-Regular.ttf', font_size+7)

max_cities = len(cities) - 1
value = 0



def get_temp(city):
        url = BASE_URL + "q=" + city + "&appid=" + API_KEY
        r = requests.get(url)
        if r.status_code == 200:
                data = r.json()
                temp = data['main']['temp']
                humidity = data['main']['humidity']
        temp = int(temp)
        humidity = int(humidity)
        temp = temp - 273
        #humidity = humidity+"%"

        temp = str(temp)+sign+'c'
        # h = "Humidity: "+str(humidity)
        return temp

def create_image(temp,city):
        image = Image.new("1", (oled.width, oled.height))
        draw = ImageDraw.Draw(image)
        (fw,fh) = font.getsize(city)
        (w,h) = font_xl.getsize(temp)
        draw.text((128//2 - fw//2,12),city,fill=255,font=font) # city name
        draw.text((128//2 - w//2,30),temp,fill=255,font=font_xl) # temp
        return image

def text_image(city):
        image_ = Image.new("1", (oled.width, oled.height))
        draw = ImageDraw.Draw(image_)
        (font_width, font_height) = font_big.getsize(city)
        draw.text((128//2 - font_width//2, 64//2 - font_height//2),city, font=font_big, fill=255)
        oled.fill(0)
        oled.image(image_)
        oled.show()

g = "Temprature\n     Meter"
intro = Image.new("1", (oled.width, oled.height))
d = ImageDraw.Draw(intro)
# Draw text
(font_width, font_height) = font.getsize(g)
d.text((20, 6),
        g, font=font_big, fill=255)

oled.fill(0)
oled.image(intro)
oled.show()
timer = 0

while True:
        if prev_value != step_pin.value:
                if step_pin.value == False:
                        if dir_pin.value == False:

                                print("left")  # left
                                value = value + 1
                                if value > max_cities:
                                        value = 1
                                print(value)
                                selected_city = cities[value]
                                print(selected_city)
                                text_image(selected_city)

                        else:
                                print("right")  # right
                                if value == 0:
                                        value = max_cities
                                else:
                                        value = value - 1
                                print(value)
                                selected_city = cities[value]
                                print(selected_city)
                                text_image(selected_city)
                                prev_value = step_pin.value

        while call_b.value == False:
                timer = timer+1

        if timer > 70000 and timer < 150000:

                oled.fill(0)
                oled.show()
                timer = 0
                print("blank")
                time.sleep(2)
                print("active")

        elif timer < 15000 and timer > 500:

                t = get_temp(selected_city)
                print(t)
                 # display the data to the OLED
                image = create_image(t,selected_city)
                oled.fill(0) #clear display
                oled.show()
                oled.image(image)
                oled.show()
                timer = 0
                time.sleep(1)
        else:
            timer = 0

Upvotes: 1

Views: 522

Answers (2)

1- make sure you typed the name of module correctly

2- make sure you use the correct python version

python3:

python3 script.py

3- also make sure it installed with the correct pip installer you may used pip for python2 by mistake

python3 -m pip install some_module

4- better way to make a virtual environment

virtualenv -p python3 env
# then
. env/bin/activate
python              # ← will run python3

you may read

this answer on stack overflow about venv and pip versions

and official documentation for venv

you may search for virtual environment on raspberry pi if there is any different setups may be needed but as I remember it is most likely like Ubuntu

Upvotes: 0

dimz
dimz

Reputation: 179

it is sometimes happen when I forgot to activate the virtualenv, even I can install new libraries using pip or pip3 without virtualenv. But python main.py won't run. Then I realized that I haven't turn on the virtual environment.

Upvotes: 1

Related Questions