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
Related
I am trying to run Test.py using os.system(path) where I specify the path of the file but I am getting an error. Basically, I want to run Test.py from here and display the output.
import os
os.system(rf"C:\\Users\\USER\\OneDrive-Technion\\Research_Technion\\Python_PNM\\Sept15_2022\\220\\1\\Test.py")
The error is
The system cannot find the path specified.
you are passing a python file, not an executable. You can pass python yourfile.py.
By the way, I would reconsider what you are doing, executing a python script from another python script is quite strange.
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 need some help. Is there a possibility to let python start the command line in windows and let the command line execute a script in another python version on my pc?
Expample: I have two versions of python on my pc. One is within Anaconda and the other one is pure Python. Now I have some scripts I want to be executed in specific order. My problem is, that the Google Analytics API doesn't work with Anaconda and some other packages (like Simpy) doesn't work with pure Python. So I need to work with two different versions of python for one project.
Now I want to write a litte python file, which opens the command line and executes the scrips in specific order on my different Python-versions.
I know how to run a python file on the command line. It's via
C:\path_to_python\python.exe C:\path_to_file\file.py
But how can I make a python script executing that line above in the command line?
Hope someone can help me.
Thanks.
import os
os.system("C:\path_to_python\python.exe C:\path_to_file\file.py")
os.system() returns the command's exit value so if you need some output from the script this won't work.
I suggest you look at subprocess
# this is new to python 3.5
import subprocess
cmd = subprocess.run(["C:/path_to_python/python.exe", "C:/path_to_script/script.py"], stdout=subprocess.PIPE)
return_string = cmd.stdout
# alternative for getting command output in python 3.1 and higher
import subprocess
return_string = subprocess.check_output(["C:/path_to_python/python.exe", "C:/path_to_script/script.py"])
Instead you can try writing a batch file in which you can specify the order how you want to run the files and with which version you have to run the file.
lets say first i want to run a file in python2.7 and the later in python3.4 and my files were in d:/pythonfiles
RunningSequence.bat
d:
cd D:\pythonfiles
c:\python27\python.exe python27file.py
c:\python34\python.exe python34file.py
try this and let me know :
import sys
with open(sys.argv[1], 'r') as my_file:
exec(my_file.read())
I am new to both Python and Linux and as such request simple explanations with minimal assumed knowledge where possible please, however I am more than willing to invest time and effort to learn.
I have a Raspberry Pi 2 (Model B V1.1) that is running Linux. I interact with this pi via putty.
I am trying to create a simple competitive reflex game, consisting of 2 buttons and a single LED. My goal is to have the LED light up after a short interval, and the first player to press their button wins.
I am writing the script for this with python (specifically 2.7.3)
My issue is that i am unable to run ANY .py file from within putty, i always receive the same error:
Syntax error: word unexpected (expecting ")")
To determine if the issue was an error in my code, i created a very very simple .py file, to check if the same error occurs, and it did. So i currently believe even if my code was functional, something is stopping me from running ANY .py file.
The process I am using is as follows:
First I create a new python file from within putty:
sudo nano test.py
Next I enter my python code (very simple for now, as i cannot get ANY .py file to run)
for each in range(5):
print 'hello'
I then press CTRL + O to write the file, hit enter, then CTRL + X to exit
Finally, I make the file executable using
sudo chmod u+x test.py
and try to run it
sudo ./test.py
again, a similar error occurs
Syntax error: "(" unexpected
I then decided to enter the code directly into the python shell, using
sudo python
>>>for each in range(5):
... print 'hello'
This time the output is the desired outcome:
hello
hello
hello
hello
hello
So there is no problem in executing python code directly from the shell, I am just unable to execute any previously saved .py file
Any insight into what could be causing this is much appreciated, and I apologise if I have not provided enough information to be useful for you.
Thanks in advance!
Short answer: Either run these as python filename.py, or else add the line #!/usr/bin/python to the top of your Python scripts.
Long answer: When you run a file from the command line in Linux (which is what the Raspberry Pi is running), by default it assumes that the file is a shell script file (usually Bash script). So it uses the Bash shell (or some other shell, but it's usually Bash) to interpret the file, and Bash doesn't know Python syntax. If you want to run your file using a different interpreter (Python, in this case), you have to add a "magic line" at the top of the file starting with #! (usually pronounced "hash-bang", and sometimes pronounced "shebang" for short). Following the #! characters is the full path of the interpreter to use, e.g. /usr/bin/python for Python scripts. (You can also use /usr/bin/env python as another answer suggested; I prefer /usr/bin/python because it's impossible to get the wrong Python interpreter that way. But that's getting into advanced topics that may be more than you need right now.)
So when you put the line #!/usr/bin/python at the top of your Python scripts, you're telling the Linux system which interpreter to run the program with, and then it should All Just Work™.
Also, STOP using sudo to edit and run these! That's just asking for trouble.
If you wish to execute like this you need the following line as the first line
#!/usr/bin/env python
This will tell bash (or equivalent) to execute the file with the Python interpreter.
If you don't want to do that then you can execute the script like this:
$ python test.py
If you go this route then you don't need to grant execute permissions on the script itself.
Also, scripts shouldn't be executed with sudo unless absolutely necessary.
When I import the wx module in a python interpreter it works as expect. However, when I run a script (ie. test.py) with wx in the imports list, I need to write "python test.py" in order to run the script. If I try to execute "test.py" I get an import error saying there is no module named "wx". Why do I need to include the word python in my command?
PS the most helpful answer I found was "The Python used for the REPL is not the same as the Python the script is being run in. Print sys.executable to verify." but I don't understand what that means.
Write a two line script (named showexe.py for example):
import sys
print sys.executable
Run it both ways as showexe.py and python showexe.py. It will tell you if you're using the same executable in both cases. If not, then it'll depend on your operating system what you have to do to make the two run the same thing.
If you start your script with something like #!/usr/local/bin/python (but using the path to your python interpreter) you can run it without including python in your command, like a bash script.