I have a file: test.py
Permissions on this file are set to 777, I am attempting to run the following script:
#!/usr/lib/python2.7
print("Content-Type: text/html\n")
print("test")
I receive the following error:
The server encountered an internal error and was unable to complete your request.
Error message:
End of script output before headers: test.py
If you think this is a server error, please contact the webmaster.
Error 500
Python is installed:
$ python --version
>> Python 2.7.4
Solved the problem, you need to use the location of the python interpreter in the first line of the code which can be found by running the following script in the python interpreter (can be invoked by typing python into the terminal):
import sys; sys.executable;
Related
I have successfully gotten a shell script to run whenever a particular usb is plugged into my RPi using the following in /etc/udev/rules.d/test.rules
ACTION=="add", ATTRS{idVendor}=="****", ATTRS{idProduct}=="214f", RUN+="/home/pi/script.sh"
In /home/pi/script/sh is
#!/bin/bash
echo "Hey!" >> /home/pi/hey.txt
/home/pi/python_script.py
This does successfully create a file in /home/pi called hey.txt with "Hey!" in it. However, it seems that the execution of this shell script fails trying to run /home/pi/python_script.py. I have made it executable with chmod +x /home/pi/python_script.py and am even able to run the shell script from terminal and it will work perfectly fine. The first line of my python script is #!/usr/bin/python3 which is the location of my python3 executable when I do which python3. I have also tried to have the line that runs the python3 script be different things like python3 /home/pi/python_script.py and /usr/bin/python3 /home/pi/python_script.py and /usr/bin/env python3 /home/pi/python_script.py to no avail.
The error I get when I do tail -f /var/log/syslog and plug in the USB is
Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.
Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.
Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.
Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.
Also, the python3 script has code to create a file in the directory as well as write to the log so that I may verify it is running. Here is the beginning of the python3 script.
#!/usr/bin/python3
import serial
import subprocess
from time import sleep, time
import logging
from systemd.journal import JournaldLogHandler
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(module)s - %(funcName)s: %(message)s")
logging.info("Starting script!")
log = open("/home/pi/log.txt", "w")
In summary, I am able to run the script from terminal successfully and able to run the python3 script as both an executable and using python3. I am also able to see that the script does execute the first task it has when the usb is plugged in, but fails seemingly at the python3 portion.
Is there something wrong with the way I am trying to run python? I have written systemd services that successfully run python scripts, so I am confused.
EDIT:
Thanks to #KlausD I see that the issue can be elaborated to that when I run it from terminal, I get no errors, but when it runs automatically from udev, I get an import error for certain modules like logging and serial, but not time.
Here is the error:
ModuleNotFoundError: No module named 'serial`
It looks like the issue was that the python3 interpreter didn't know where to look for modules, so the answer here fixed my issue.
https://askubuntu.com/questions/1406907/python-script-called-by-udev-rule-unable-to-import-modules-modulenotfound
I put the following code at the beginning of my script and swapped out the below path for my python3 version's path and it runs!
import os
import sys
sys.path.append(os.path.abspath("/usr/local/lib/python3.9/dist-packages"))
I am getting this error:
Server error!
The server encountered an internal error and was unable to complete your request.
Error message: End of script output before headers: python.py
If you think this is a server error, please contact the webmaster.
Error 500
192.168.64.2 Apache/2.4.41 (Unix) OpenSSL/1.1.1d PHP/7.4.1 mod_perl/2.0.8-dev Perl/v5.16.3
I have done the following:
Added .py to Addhandler in httpd.conf
I have also installed pip and pymysql.
I am using python 3.8.3
I have also have the permissions set to chmod 755
have also tried chmod 755, +x but no luck I have also checked the error logs but no clear problem
In the error logs it says: End of script output before headers: python.py
and
(2)No such file or directory: exec of '/opt/lampp/htdocs/11-python/python.py' failed: /opt/lampp/htdocs/11-python/python.py
btw I am using Mac
Heres my code:
#!/usr/bin/local/env python3
print ('Content-type: text/html\r\n')
print ('\r\n')
print ()
print ('<html>')
print ('<head>')
print ('</head>')
print ('<body>')
print ('Hello Python')
print ('</body>')
print ('</html>')
My old CentOS 5 apache server can run Perl script CGI without any problem.
# cat t1.cgi
#!/usr/local/bin/perl
use CGI;
my $cgi = CGI->new();
print $cgi->header;
print "Test\n";
# GET HTTP://www.example.com/cgi/t1.cgi
Test
python scripts can be run under shell without any problem:
# cat t2.cgi
#!/usr/local/bin/python3
print("Content-Type: text/html\n")
print("hello!")
# python3 t2.cgi
Content-Type: text/html
hello!
but failed as a python cgi script:
# GET http://www.example.com/cgi/t2.cgi
<HTML>
<HEAD><TITLE>An Error Occurred</TITLE></HEAD>
<BODY>
<H1>An Error Occurred</H1>
500 Internal Server Error
</BODY>
</HTML>
apache error logs show the error was:
Fatal Python error: _Py_HashRandomization_Init: failed to get random numbers to initialize Python
Python runtime state: preinitialized
Premature end of script headers: t2.cgi
any idea will be appreciated!
fixed it by
mknod -m 444 /dev/random
chmod 444 /dev/urandom
my old centos don't have /dev/random, and the default file mode for urandom was 600, read only by root.
I want to start a pythonscript by pushing a Button in a swift macOS application. I come up with:
let process = Process()
process.launchPath = "/usr/bin/python3"
process.currentDirectoryPath = "\(NSHomeDirectory())" + "/PycharmProjects/untitled5"
process.arguments = ["myscript.py"]
process.launch()
but I get "launch path not accessible" error by executing. If I change launchPath to:
process.launchPath = "/usr/bin/python"
everything works fine, but now I getting python compiling errors because myscript is written in python3.6.0, I have to use python3 because of using a library.
When I open Finder and go to "/usr/bin/python3" it says not found, but python3 is installed, I used it in Pycharm and I'm also able to start python3 in terminal.
In terminal "python3 ~/PycharmProjects/untitled5/myscript.py" works.
On your terminal type
which python3
this will return the path that is accessed when you run python3 from the command line
I have created this executable out of a python script. All it does is prints 'Hello World'. I am trying to run this off SQLPlus.
My SQlPlus query is as provided below:
START 'C:\USERS\ADMINISTRATOR\DESKTOP\dist\HW.exe'
I keep getting the following error:
Using 'Host', gives:
Any help/suggestion/edits would be appreciated.
I have also plainly tried to run the python script 'HW.py' resulting in the following error:
What is the right way to execute the HW.exe from SQLPlus?