Python 重新运行脚本时找不到脚本模块
Python script module not found on rerun of script
我是 运行 RaspberryPi 4 上的 python 脚本,今天当我再次 运行 该脚本时,它向我显示错误 ModuleNotFoundError: No module named 'adafruit_ssd1306'
虽然我 运行 昨天大约有 10 次相同的脚本,它 运行 完全没有任何错误。我没有更改脚本中的任何内容,甚至没有更改它的位置。我尝试强制重新安装库,
运行 来自另一个位置的脚本,重新启动 pi,运行 它作为 sudo
但它们中的 none 都起作用了。
通过使用 pip3 freeze
它表明该软件包已安装并尝试再次安装该软件包表明要求已经满足。
Python版本: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
1- 确保您输入的模块名称正确
2- 确保使用正确的 python 版本
python3:
python3 script.py
3- 还要确保它安装了正确的 pip 安装程序,您可能错误地将 pip 用于 python2
python3 -m pip install some_module
4- 创建虚拟环境的更好方法
virtualenv -p python3 env
# then
. env/bin/activate
python # ← will run python3
你可以阅读
this answer on stack overflow about venv and pip versions
和official documentation for venv
如果可能需要任何不同的设置,您可以在 raspberry pi 上搜索虚拟环境,但我记得它很可能像 Ubuntu
有时我忘记激活 virtualenv 时会发生这种情况,即使我可以在没有 virtualenv 的情况下使用 pip 或 pip3 安装新库。但是 python main.py 不会 运行。然后我发现我没有开启虚拟环境
我是 运行 RaspberryPi 4 上的 python 脚本,今天当我再次 运行 该脚本时,它向我显示错误 ModuleNotFoundError: No module named 'adafruit_ssd1306'
虽然我 运行 昨天大约有 10 次相同的脚本,它 运行 完全没有任何错误。我没有更改脚本中的任何内容,甚至没有更改它的位置。我尝试强制重新安装库,
运行 来自另一个位置的脚本,重新启动 pi,运行 它作为 sudo
但它们中的 none 都起作用了。
通过使用 pip3 freeze
它表明该软件包已安装并尝试再次安装该软件包表明要求已经满足。
Python版本: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
1- 确保您输入的模块名称正确
2- 确保使用正确的 python 版本
python3:
python3 script.py
3- 还要确保它安装了正确的 pip 安装程序,您可能错误地将 pip 用于 python2
python3 -m pip install some_module
4- 创建虚拟环境的更好方法
virtualenv -p python3 env
# then
. env/bin/activate
python # ← will run python3
你可以阅读
this answer on stack overflow about venv and pip versions
和official documentation for venv
如果可能需要任何不同的设置,您可以在 raspberry pi 上搜索虚拟环境,但我记得它很可能像 Ubuntu
有时我忘记激活 virtualenv 时会发生这种情况,即使我可以在没有 virtualenv 的情况下使用 pip 或 pip3 安装新库。但是 python main.py 不会 运行。然后我发现我没有开启虚拟环境