Faulty Python script after using SFTP - python

I have a running python program I wrote in thonny.
Everything worked perfectly fine, when I ran it from the IDE and the terminal.
I run SSH on the Raspberry Pi I'm using for this application to start my program. Then I tried using SFTP to download the files the program puts out.
This worked too, but when I tried to start the program again, it suddenly errored out at (4th line, rest for context):
#PYGAME SETUP FOR SOUNDS
pygame.init()
pygame.mixer.init()
shuttersound = pygame.mixer.Sound('Shutter.wav')
savesound = pygame.mixer.Sound('Save.wav')
with:
Traceback (most recent call last):
File "/home/pi/Desktop/PictographBox/PictographBox_v1.1.py", line 19, in <module>
shuttersound = pygame.mixer.Sound('Shutter.wav')
pygame.error: Unable to open file 'Shutter.wav'
although the sound file remained unchanged.
I could still run the program perfectly fine from thonny.
I ran "dos2unix" on my script and it worked as expected again.
So I tried to use SFTP again and that produced the exact same problem on the same line (again fixed by "dos2unix").
I need to prevent this from happening, as downloading the files is crucial.
I first thought it was my FTP client, but I tried three different clients which produced the exact same result.
I didn't even open or otherwise touch the script with SFTP, just went into the folder.
Do I have a problem in my script?
All line breaks look fine.
No other files have problems, only my script and only after opening it's folder via SFTP.

Related

trying to run outside exe file from python freezes

I'm trying to run an executable file called CDQ.exe (which is supposed to create a text file) from within a python script. I'm using os.system(CDQ.exe) to run it. When I start the python script, the program freezes upon the os.system() call. I also don't get the text file. When I opened the task manager, I found CDQ was still running, and it kept running until I ended it.
When I try to run the CDQ.exe from the windows file browser by clicking it twice, the program appears for a couple of seconds in the task manager, then it closes, and my text file appears.
The executable file was created from c++ qt, and I'm using windows.
Does anyone know what's going on?

Inconsistent behavior of pyserial and rshell when executed in a .py file

I am trying to program my lego mindstorms inventor robot using python and this pypi package. It basically uses rshell to communicate with the lego robot that is running micropython. Here is the code that I am trying to execute.
from mindstorms import Hub
hub = Hub()
motor = hub.port.E.motor
print(motor)
motor.run_for_degrees(270)
When I run this code line by line in the python shell in the terminal everything works as expected. The motor turns. However, if I put this in a .py file, I get this error. rshell.pyboard.PyboardError: ('exception', b'', b'Traceback (most recent call last):\r\n File "<stdin>", line 1, in <module>\r\nAttributeError: \'NoneType\' object has no attribute \'run_for_degrees\'\r\n'
Seems like the issue is that the motor is not being detected by the brain when I run the script in a .py file.
I made another script for diagnostics. It is sending messages directly to the robot without using the pypi package.
board = Pyboard(device)
board.enter_raw_repl()
print(board.exec_("import hub;print(hub.port.A.info())"))
The result is "None". The brain by itself is detected because commands such as print(board.exec_("import hub;print(hub.info())")) run successfully. When I am trying to get any information about the port, the result is None, which makes me believe that the connected motor is not detected by the .py file. Again, if I simply run all this code in the python shell it works perfectly. I am confused by this irregular behavior.
Here are the steps that I took to troubleshot this(none of them worked):
Tried to run a .py file from the terminal using exec() function, and still get the error that the motor is None.
Checked that the python version of the shell and the version of the python that is running the .py file is the same.
Running this script on different machines.
Reinstalled lego mindstorms firmware and ensured that the latest version is running.
Any suggestions why this would this problem occur?
The issue is that you need to wait a few seconds after the initialization of the Mindstorms brain before performing any kind of motor initialization.
from mindstorms import Hub;
import time
hub = Hub();
time.sleep(2)
print(hub.port.A.motor);
hub.port.A.motor.run_for_degrees(30);

Reading Error Message when Clicking on Python File

If I run the python script file from IDLE or Windows command prompt, I am able to view the error message.
Script file:
print(3/0)
input()
Error:
Traceback (most recent call last):
File "...\TEST.py", line 1, in <module>
print(3/0)
ZeroDivisionError: division by zero
But if I run the file by double clicking on it, the window just closes and I do not know what the error is. How can I see it?
I am running Python 3.4.
If you just double-click the file, once you hit an error, it terminates the program, thus closing the console window. To see the errors, you should run it from the command prompt.
This is how its supposed to work.
When you "doubleclick" the .py file windows sees an executable file and invokes a cmd-shell and runs your python executable within (even though its only a text file, its considered as an executable - thats how you have set it in your system - this can be changed, say to make a doubleclick simply open the file in a text editor of your choice, like Notepad or Notepad++ or Python's default IDLE editor). Since division by 0 is an error, the cmd-shell is killed as soon as the error is seen in the .py file - your .py file is treated as an executable, much like how an .exe application crash doesn't wait for you before its killed.
If you do not want lose the window and wish to see the error, then you already seem to know what to do - run it by invoking python from a cmd-shell manually, or better still, use the built-in IDLE editor (Press F5 to run your script from an IDLE editor)

How to use websockify with python on windows or compiling websockify as windows executable (for more multiple client connection at a time)

I'm trying to get the websockify 0.6.0 running on windows but without any luck,
I have tried python websockify.py 1501 10.0.0.141:1501 but its not good, getting errors , like that:
Traceback (most recent call last):
File "websockify.py", line 1, in <module>
run
NameError: name 'run' is not defined
I also tried Compiling Websockify as Windows Executable , but this also didn't work
I use the following command run.exe 1501 10.0.0.141:1501 and it looks promising at the beginning, outputting the following to the console:
WARNING: no 'resource' module, daemonizing is disabled
WebSocket server settings:
- Listen on :1501
- Flash security policy server
- No SSL/TLS support (no cert file)
- proxying from :1501 to 10.0.0.141:1501
but then after trying to connect using the following from the browser ws://localhost:1501
**it outputs the following error
Traceback (most recent call last):
File "run", line 5, in <module>
File "websockify\websocketproxy.pyc", line 419, in websockify_init
File "websockify\websocket.pyc", line 1018, in start_server
UnboundLocalError: local variable 'exc' referenced before assignment
Any idea on how to use the websockify on windows / or how to use the compiled websockify as windows executable ?
The easiest way to get websockify working on Windows is to use the Node.js version of websockify (in the other/js directory). It works perfectly out of the box, with no shenanigans required.
To address this, use the modified following commands for your example source, start from the beginning of each step and see if it helps:
Firstly, install Portable Python 2.7
You then need to modify the setup.py (It looks like this is why you are getting your first error, as you may not have defined run):
from setuptools import setup, find_packages
# add (line 2):
import py2exe
setup(name=name,
# add (line 10):
console=['run'],
Ensure the above has executed correctly by inspecting setup.py and ensure it includes run.
In your local code, import the resources module to allow you to monitor, measure and control system resources utilized by your program
import resource
Inspect your local variable exc and ensure you have assigned a value to it before calling it (I'm guessing you may have attributed a system variable to it, but python was unable to do so as you did not have resources imported, and as such it was not assigned). If you like, put up an example of your code in the comment to this response and I'll take a closer look at this part.
Back to your source guide, navigate to the websockify folder in command prompt, then execute the following to compile websockify:
[Your path to Portable Python install]\App\python.exe setup.py py2exe --includes numpy
You will now see in the websockify directory a new dir 'dist' which contains the compiled exe. An example provided is:
run.exe 5901 localhost:5900
There is also a guide here to run websockify as a Windows Service if this suits (again mentioned in your source).
----Further edit for more detail----
Open up the two files that seem to be giving you issues (websockify\websocketproxy.pyc and websockify\websocket.pyc and ensure that any reference to a variable called "exc" is referenced after it has been assigned a value (small chance of an issue if you have not yet modified these files.
I believe that your code is relying upon making and monitoring changes to the system resources (such as ports etc) and you are not allowing your code to have these permissions, so it needs the resources module. If you are calling run.exe from a program (what I called your local code) then you need to import resources at the top. If you are just straight up calling the run.exe program from a command line, then try making this new program and see if this helps. If not, send me the contents of your websockify folder and run.exe and I will take a look
# Program Name: goAndRun.py
# Code:
import sys, string, os, arcgisscripting, resource
os.chdir( 'C:\\[Path_to_the_dir_containing_run.exe]' )
os.system( '"C:\\[Path_to_the_dir_containing_run.exe]\run.exe, 5901 localhost:5900"' )
And then use the command:
python goAndRun.py
Not being in your environment, I cannot tell if this will execute exactly as I have written it. The last line may also be:
os.system( '"C:\\[Path_to_the_dir_containing_run.exe]\run.exe"', '5901 localhost:5900' )

Python scripts hang on exception until ctrl-c

I have an issue with running python scripts on one server running RH Linux with Python 2.6.6. When I run any script and it produces an exception, the script hangs until I press CTRL-C and then it prints the traceback information. It happens when I run the script from the command-line without calling python directly, making use of the shebang on the first line of the script. If I execute the script by calling python , I don't have the same hanging issue. I've searched and seen similar issues, but they were related to specific libraries where this happens with all python scripts. I've tried a different servers with the same script and didn't have the issue. I have included a simple script that I have been testing with. It tries to open a non-existent file.
#!/usr/bin/env python
tempfile = open('noexists.txt','r')
When I execute the code as "test.py" on the command-line, I get the following response:
~/bin$> test.py
^CTraceback (most recent call last):
File "/export/home/jwd3/bin/test.py", line 2, in <module>
tempfile = open('noexists.txt','r')
IOError: [Errno 2] No such file or directory: 'noexists.txt'
If I execute it as "python test.py" then I get the following response:
~/bin$> python test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
tempfile = open('noexists.txt','r')
IOError: [Errno 2] No such file or directory: 'noexists.txt'
The difference is difficult to tell here, but it is very evident when executing. Look at the first sample output and notice the "^C" before the Traceback. Until I hit CTRL-C, the script was just hanging. The second sample output returns the traceback of the exception imediately without hanging.
I've tried moving the script to a new location, changing the shebang from #!/usr/bin/python to #!/usr/bin/env python and different scripts. In all cases they behave the same. Any help would be greatly appreciated. I do not want to call all python scripts using "python " format.
Thanks to the help from the suggestions above, I was able to find the solution to the issue. After running strace, I found the code was hanging on a socket connection. Searching for the same socket connection info, I found another issue already posted on Stack Overflow that was similar. I then found a second issue that was also very similar. Both issues had the same suggestion. Apparently the Python script was conflicting with a service called abrtd. The suggestion was to restart this service (abrtd). After restarting it, the issue with the Python script hanging was resolved.

Categories

Resources