I have a question regarding a Python running problem.
I set up a local server with the Windows command prompt using the python -m http.server 8000 command, but when I run my script on http://localhost:8000/cgi-bin/hello.py, it just shows me the source code instead of actually running the script.
I use the code i found on this link.
#!/usr/bin/python
print('Content-Type: text/html')
print()
print('<html>')
print('<head><title>Hello from Python</title></head>')
print('<body>')
print('<h2>Hello from Python</h2>')
print('</body></html>')
I've seen several people having the same problem, but I couldn't really find a solution.
I work with Python 3.5
You should follow the documentation. The command for Python 3 is:
python -m http.server --cgi 8000
Related
If I run this on my (Linux) command line.
python3 -m serial.tools.list_ports
I get the result:
/dev/ttyUSB0
1 ports found.
What files (or sequence of files) are run by this -m switch?
I have various python directories in my /usr/ directory but no specific serial.py file (I can see serialcli.py & serialutil.py, serialize.py etc) so the command is being 'turned' into some form to use a 'serial based' file, but which one?.
How does it generate the output?
In python code when I use this command no output is generated so I guess the -m switch taps into an output routine??
The output from this command (and other examples) is quite useful and I may want to use it in python itself rather than from BASH.
I am aware it's not meant for this but for testing purposes, but still... ;)
Q: So what files are actually 'tapped into' in this 'serial' example?
The -m flag in python is used for:
-m mod : run library module as a script (terminates option list)
What does "terminates option list" mean?
It means that, any future options get passed to the program you are providing and not to python.
So in your scenario, it's running the list_ports module as a script
python3 -m serial.tools.list_ports
How can you use it in python itself?
You can import the serial module and call functions inside list_ports. I am very sure, you have these files in your workspace / environment, because you have installed pyserial
Alternatively - you can also install pyserial in a virtual environment, and run some experiments.
If you "really, really" want to use this like a bash command, within a python script? Do take a look at the subprocess module.
Subprocess
Pyserial - serial.tools.list_ports
I am running a (bio)python script which results in the following error:
from: can't read /var/mail/Bio
seeing as my script doesn't have anything to with mail, I don't understand why my script is looking in /var/mail.
What seems to be the problem here? i doubt it will help as the script doesn't seem to be the problem, but here's my script anyway:
from Bio import SeqIO
from Bio.SeqUtils import ProtParam
handle = open("examplefasta.fasta")
for record in SeqIO.parse(handle, "fasta"):
seq = str(record.seq)
X = ProtParam.ProteinAnalysis(seq)
print X.count_amino_acids()
print X.get_amino_acids_percent()
print X.molecular_weight()
print X.aromaticity()
print X.instability_index()
print X.flexibility()
print X.isoelectric_point()
print X.secondary_structure_fraction()
what is the problem here? bad python setup? I really don't think it's the script.
No, it's not the script, it's the fact that your script is not executed by Python at all. If your script is stored in a file named script.py, you have to execute it as python script.py, otherwise the default shell will execute it and it will bail out at the from keyword. (Incidentally, from is the name of a command line utility which prints names of those who have sent mail to the given username, so that's why it tries to access the mailboxes).
Another possibility is to add the following line to the top of the script:
#!/usr/bin/env python
This will instruct your shell to execute the script via python instead of trying to interpret it on its own.
I ran into a similar error when trying to run a command.
After reading the answer by Tamás,
I realized I was not trying this command in Python but in the shell (this can happen to those new to Linux).
Solution was to first enter in the Python shell with the command python
and when you get these >>>
then run any Python commands.
Same here. I had this error when running an import command from terminal without activating python3 shell through manage.py in a django project (yes, I am a newbie yet). As one must expect, activating shell allowed the command to be interpreted correctly.
./manage.py shell
and only then
>>> from django.contrib.sites.models import Site
Put this at the top of your .py file (for Python 2.x)
#!/usr/bin/env python
or for Python 3.x
#!/usr/bin/env python3
This should look up the Python environment. Without it, it will execute the code as if it were not Python code, but shell code. If you need to specify a manual location of the Python environment, put
#!/#path/#to/#python
for Mac OS just go to applications and just run these Scripts Install Certificates.command and Update Shell Profile.command, now it will work.
For Flask users, before writing the commands, first make sure you enter the Flask shell using:
flask shell
I am working with my Raspberry Pi 2 B+ and I am using Raspbian. I have a python script located at /home/pi/Desktop/control/gpio.py
When I type /home/pi/Desktop/control/gpio.py into the command line, I get the message
bash: /home/pi/Desktop/control/gpio.py Permission denied
I have tried running sudo -s before running that command also but that doesnt work. My python script is using the Rpi.GPIO library.
If someone could please explain why I am getting this error it would be appreciated!
You will get this error because you do not have the execute permission on your file. There are two ways to solve it:
Not executing the file in the first place. By running python gpio.py python will load the file by reading it, so you don't need to have execute permission.
Granting yourself execute permission. You do this by running chmod u+x yourfile.py.
However, doing so will not work unless you add a shebang at the top of your python program. It will let your linux know which interpreter it should start. For instance:
#!/usr/bin/env python
This would try to run python using your current $PATH settings. If you know which python you want, put it here instead.
#!/usr/bin/python3
Remember the shebang must be the very first line of your program.
do like this maybe work:
cd /home/pi/Desktop/control/
python gpio.py
Because gpio.py is not a executable file, you should run it by python instead
I am trying to run a python3 script remotely trough ssh, first of all i would like to know if this is even possible if the machine i am trying to run the script on doesnt have a python3 interpreter only a 2001 version of python.
And also i am using the following command to run the script , but its not working:
spawn sh -c "ssh -oPort=$port $ip /usr/bin/env < /home/pythonscript
Pythonscript contains a command meant to output the connected COM ports,it is the following:
import serial.tools.list_ports
print([comport.device for comport in serial.tools.list_ports.comport()])
The output that i get from this is a bunch of system information belonging to the machine i am connecting to, stuff like HOSTNAME,USER,MACHTYPE,MAIL,SHELL,OSTYPE
How would i get my intended output from the command that i am executing
All help appreciated
Firstly you will need to install python3 on each server you will be running this on. You will also need to install pip3 and install pyserial. You could also use virtualenv if you want but I'll leave that to you.
Found a small bug in that script. According to the latest version anyway it's "comports" not "comport: https://pyserial.readthedocs.io/en/latest/tools.html
Updated Version:
from serial.tools.list_ports import comports
print([comport.device for comport in comports()])
I was able to run that remotely simply by doing the following
ssh localhost "python3 /home/user/projects/stack_overflow/56900773_remote_python_ssh/pythonscript.py"
Change localhost to whatever server you want and swap my path for the full path to your script.
When I ran that command I just get a blank list, probably because I have no comports :)
I am running a (bio)python script which results in the following error:
from: can't read /var/mail/Bio
seeing as my script doesn't have anything to with mail, I don't understand why my script is looking in /var/mail.
What seems to be the problem here? i doubt it will help as the script doesn't seem to be the problem, but here's my script anyway:
from Bio import SeqIO
from Bio.SeqUtils import ProtParam
handle = open("examplefasta.fasta")
for record in SeqIO.parse(handle, "fasta"):
seq = str(record.seq)
X = ProtParam.ProteinAnalysis(seq)
print X.count_amino_acids()
print X.get_amino_acids_percent()
print X.molecular_weight()
print X.aromaticity()
print X.instability_index()
print X.flexibility()
print X.isoelectric_point()
print X.secondary_structure_fraction()
what is the problem here? bad python setup? I really don't think it's the script.
No, it's not the script, it's the fact that your script is not executed by Python at all. If your script is stored in a file named script.py, you have to execute it as python script.py, otherwise the default shell will execute it and it will bail out at the from keyword. (Incidentally, from is the name of a command line utility which prints names of those who have sent mail to the given username, so that's why it tries to access the mailboxes).
Another possibility is to add the following line to the top of the script:
#!/usr/bin/env python
This will instruct your shell to execute the script via python instead of trying to interpret it on its own.
I ran into a similar error when trying to run a command.
After reading the answer by Tamás,
I realized I was not trying this command in Python but in the shell (this can happen to those new to Linux).
Solution was to first enter in the Python shell with the command python
and when you get these >>>
then run any Python commands.
Same here. I had this error when running an import command from terminal without activating python3 shell through manage.py in a django project (yes, I am a newbie yet). As one must expect, activating shell allowed the command to be interpreted correctly.
./manage.py shell
and only then
>>> from django.contrib.sites.models import Site
Put this at the top of your .py file (for Python 2.x)
#!/usr/bin/env python
or for Python 3.x
#!/usr/bin/env python3
This should look up the Python environment. Without it, it will execute the code as if it were not Python code, but shell code. If you need to specify a manual location of the Python environment, put
#!/#path/#to/#python
for Mac OS just go to applications and just run these Scripts Install Certificates.command and Update Shell Profile.command, now it will work.
For Flask users, before writing the commands, first make sure you enter the Flask shell using:
flask shell