Python EOFerror when importing a module after unplugging Raspberry Pi - python

I have a python3.2 script running from rc.local at startup on a raspberry pi - Raspbian OS, it imports a file called inouts.py module that i made and lives in the same directory, ive updated the sys.path.append(...)
The script worked fine for weeks. Today I had to unplug the rpi without shutting down.
After rebooting the script fails to open and gives the error:
EOFError: EOF read where not expected
The inouts.py is definitely the module causing the error as I have it on its own line.
If I change the name from inouts.py to inouts2.py the script works.
If I run it as python2 it also works.
Can anybody point me in the right direction on what might be causing this filename to cause this error?
Traceback (most recent call last):
File "rf2.py", line 3, in <module>
import inouts
EOFError: EOF read where not expected

I solved this problem myself today by deleting all of the *.pyc files in the __pycache__ subdirectory.

Related

Issues running working python script from systemd on Raspberry Pi

I have a python script, using pygame and pyautogui which works when run through terminal or any IDLE on my Raspberry Pi 3 Model B V1.2. The script opens a webpage, and reads joystick inputs through pygame.
I want the script to run at boot after a network is connected, so have created a service in /etc/systemd/system/.
When run by the service, the script has an error when using various libraries including pygame and pyautogui.
My service is as follows:
[Unit]
Description=My magic service
After=multi-user.target
Requires=network.target
[Service]
Type=simple
User=pi
ExecStart=/usr/bin/python3 /home/pi/FinalCode.py
Restart=always
StandardOutput=file:/tmp/FinalTests.log
StandardError=inherit
[Install]
WantedBy=multi-user.target
The welcome message from pygame prints (Hello from the pygame community... etc), however it the returns the following error:
Traceback (most recent call last):
File "/home/pi/FinalCode.py", line 89, in <module>
for event in pygame.event.get(): # read joystick commands
pygame.error: video system not initialized
I then commented out all the pygame, but a similar issue also occurs with pyautogui, which returns the following error:
Traceback (most recent call last):
import pyautogui
File "/home/pi/.local/lib/python3.9/site-packages/pyautogui/__init__.py", line 249, in <module>
import mouseinfo
File "/home/pi/.local/lib/python3.9/site-packages/mouseinfo/__init__.py", line 223, in <module>
_display = Display(os.environ['DISPLAY'])
File "/usr/lib/python3.9/os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'DISPLAY'
I have checked "sys.prefix" and "sys.base_prefix" and both in the terminal and from the service they are "/usr" (I think this shows that it is running in the same environment?). Both the service and idle/terminal are running Python 3.9.2 through "/usr/bin/python3"
How do I get the systemd service to run in exactly the same way as the terminal or IDLE running the script?
The basic problem you're running into is that the Systemd unit has no idea where you want this thing displayed. It is missing the DISPLAY environment variable, which is set when Xorg is running but will not be set otherwise.
I think this answer to a similar question might help you out. Long story short - Make your service a user service and change some dependencies and then import the variables from where ever you start Xorg (Login managers have some way of handling this but I only ever really used startx until I switched to wayland and so I have little to offer here).
Found a solution that works, the issue was as bbenne10 suggested.
With hindsight, somewhat unsurprisingly, pyautogui also needs the display so had no issues afterwards.

Error processing line 1 of distutils-precedence.pth (no module named '_distutils_hack')

I'm on Windows 10 and working in a git-bash terminal with anaconda environment. When I open a new terminal, I'm immediately presented with:
Error processing line 1 of C:\Users\e360769\AppData\Local\miniforge3\lib\site-packages\distutils-precedence.pth:
Traceback (most recent call last):
File "C:\Users\e360769\AppData\Local\miniforge3\lib\site.py", line 169, in addpackage
exec(line)
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named '_distutils_hack'
Remainder of file ignored
This same error comes up any time I run "pip" or "python" on the command line. The command still executes correctly, but only after displaying that error. Note that this started after installing a collection of packages needed for a project I'm working with, and after investigating I found some people attributed this to the setuptools package (I have v41.2.0), but even when I uninstall it, the issue persists. Also note that this is the contents of my distutils-precedence.pth file:
import os; var = 'SETUPTOOLS_USE_DISTUTILS'; enabled = os.environ.get(var, 'local') == 'local'; enabled and __import__('_distutils_hack').add_shim();
I'm not sure this is actively (for now) causing any issues with executing my python scripts, etc., but I'd like to know what's going on and how to fix this. Would appreciate any help!

Raspberry Pi Pico ImportError: no module named 'machine'

I am running the following blink program on my raspberry pi pico. I am using circuit python.
from machine import Pin
import time
led = Pin(13, Pin.OUT)
while True:
led(1)
time.sleep(1)
led(0)
time.sleep(1)
When I run it though it gives this error:
Traceback (most recent call last):
File "code.py", line 1, in <module>
ImportError: no module named 'machine'
I have tried to find if I need to download a library file or any thing about the machine module, but I have found nothing. If you know why it can't find the machine module that would be greatly appreciated.
Your code is for micropython. Circuitpython is different. See here https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out
from digitalio import DigitalInOut, Direction, Pull
led = DigitalInOut(board.LED)
I was having same issue and a search found this thread.
I changed the Interpreter setting in Thonny (under options) from Local Python 3, to MicroPython (Raspberry Pi Pico)
I think you are using the incorrect uf2 file as the official (stable) version is not out as of writing this.
In order to check this, type the following in MircoPython's command line:
import sys
sys.implementation
(name='micropython', version=(1, 19, 1), _machine='Raspberry Pi Pico with RP2040', _mpy=4102)
If the response shows "Pico" and not "Pico W" then copy the latest version from here and copy it onto your Pico W (in USB mode)

Running python file within a shell script : module not found error

I'm beginner to both bash and python.
I'm working in Ubuntu env.
to be short, I've created a shell script 'format_data.sh' that runs python file 'proc_ko.py' within it.
#!/bin/bash
...
python path/to/python/file/proc_ko.py
...
And the python file 'proc_ko.py' imports a module called khaiii
from khaiii import KhaiiiApi
api = KhaiiiApi()
...
But when I try to execute 'format_data.sh', I get this import error from python file.
Traceback (most recent call last):
File "media/sf_projet/pe/pe/PROGRAMME/SCRIPTS/proc_ko.py", line 5, in
from khaiii import KhaiiiApi
ImportError: No module named khaiii
which doesn't occur when I execute python file independently.
Python file 'proc_ko.py' itself doesn't have any error and 'khaiii' is well installed.
so I don't understand why import error occurs only through the shell script.
If u need more details to figure out, I'll be happy to provide. Thanks in advance for help.

Pycharm error with easygui: No module named 'global_state'

I am working on a script which calls easygui. This has been added to the virtual environment.
The lines involving easygui are
#import module
from easygui import *
#set message and title
msg="Hello World!"
title="Sample Program"
# a simple window showing a message, a title and a ‘Ok’ button
msgbox(msg,title)
The script throws the error below. However, it runs perfectly when I call it from the command line. Why is pycharm throwing an error, but not the command line? Thanks.
Traceback (most recent call last):
File "/Users/nickriches/PycharmProjects/pythonProject3/main.py", line 12, in <module>
from easygui import *
File "/Users/nickriches/PycharmProjects/pythonProject3/venv/lib/python3.7/site-packages/easygui/__init__.py", line 34, in <module>
from .boxes.button_box import buttonbox
File "/Users/nickriches/PycharmProjects/pythonProject3/venv/lib/python3.7/site-packages/easygui/boxes/button_box.py", line 18, in <module>
import global_state
ModuleNotFoundError: No module named 'global_state'
I had this error this afternoon (MacOS M1, running python 3.9), and this is how I fixed it.
Basically the error is caused by the absence of a properly functioning tkinter installation, even though the error is complaining about "global_state". You can work this out by tracing through the code, which I will not do here.
You can prove to yourself that there is a defect in your tkinter installation by opening python in REPL mode and typing:
"import tkinter".
If you get an error, then you are on the right track to continue below.
The solution therefore is to properly get tkinter working.
I installed tkinter on my Mac by going to https://www.activestate.com/products/tcl/downloads/ and installing it.
Then I installed the "python-tk" module by running:
"brew install python-tk".
After that, when I opened python in REPL and ran "import tkinter", things worked.

Categories