Python execution works in Shell but not in Terminal - python

I have a Raspberry Pi and I'm attempting to create a Python script. However the issue is that I'm unable to run the script from the Terminal, it throws syntax errors yet the same code works just fine in the Python 3.5.3 Shell.
I'm trying the simplest thing such as a printand I've tried various ways with the parentheses and quotation marks, yet no luck with executing the script in the terminal.
I'll include a simple Imgur link of a screenshot, showing how the code is successfully executed in the Shell but not in the terminal.
https://imgur.com/a/lLSnq
The code:
print ("test")
Any assistance is greatly appreciated in advance!

The error was that your terminal didn't know that the code you tried to execute was python, therefore it tried to execute it with the bash interpreter.
Adding the correct shebang to specify the use of the python interpreter fixed the problem.
#!/usr/bin/env python
print("test")

you can execute python scripts, i.e. the script boa.py from terminal by python boa.py

Related

How to fix error message "from: can't read /var/mail/pwn" [duplicate]

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

VS Code had a problem in running the code

I tried to run a python program in VS Code. But my program didn't run. The terminal opened and a weird arrow was there in the terminal. This is the screenshot of that.
This is the weird arrow and the program is not running. Any ideas why this is happening and how to fix it?
Thanks in advance.
Firstly, the arrows are included in the default python IDE means that VScode ran the command to execute your code. Give your pc a restart. Now, let us check if python is working or not or VS code is having some trouble. Type the following command in cmd to execute the code-
python "$PATH"
Rember to replace $PATH to the path of the file i.e where your file is stored. For eg. I've my python files stored in D drive in a python folder, so I'll use-
python "D:\Python\Hello.py"
If this works, python is working fine and if not, try reinstalling python and check the box which says Add python to Path or Environment variables. Then open VS code try to run the program again. But click the button only once and be patient because clicking it multiple times causes execute the same command again and cause a problem. It's my personal experience. Wait 5 minutes. Not works. Don't worry, there's a problem with the run extension you are using. I'll recommend the Code runner by Jun han. I personally use it. Type this in the extension search box-
formulahendry.code-runner
Install it and then try again.
Kill the terminal, and retry. If not work, restart the VSCode.

Executing command line program (Abaqus-python script) from python script

I am trying to run an Abaqus-python script file from another python script executed in Spyder (python 3.7, windows 10).
If I run the following command in CMD
abaqus cae -noGUI model.py
the code executes as expected. However, if I try to run cp = subprocess.run('abaqus cae -noGUI model.py',shell=True)
I don't get any output. Below is what is returned in cp.
Notes:
The Abaqus-python file I am trying to run is in the same folder as the python script I have.
I have tried various sp.call, sp.run, os.systems in different string and list formats.
"C:\Abaqus\Commands" and "C:\SIMULIA\Commands" are both in my PATH environment variable.
I think the -noGUI call is important to the problem. I can't seem to find others on SO with this issue.
After a fair amount of digging, I think the issue is that the PATH for my python install is done thought Anaconda so I have a different path this vs CMD. I fixed this by adding the full Abaqus location to the command. See below.
subprocess.call(r'C:\SIMULIA\Commands\abaqus job=Job-1 ask_delete=OFF interactive', shell=True)
I am not sure what your problem is due to, but note that Abaqus scripting is based on python 2.7, therefore you might need to downgrade it

using bash commands in python on mac: error 127

I am using am using Python 2.7 on MacOS and want to use a bash command within a python script.
command = "someProgram --option1 value 1 --option2 value 2"
I had to include the path of this program in my bash_profile in order to run it. I tested so far:
os.system(command)
and
subprocess.check_call(command.split(" "),shell=True)
Neither worked. The latter threw error 127 and the first one only returned 32512. A google search told me that this occurs when the command is not known.
If I now start this command within the terminal everything works perfectly fine.
Do I have to include something such that python can find this command? Why is this behavior?
With shell=True the cmd needs to be a string.
subprocess.check_call(command, shell=True)
where command is of type str
Thanks for your help. The final solution is kind of stupid. I started spyder via the anaconda GUI. If I do so the above code does not work.
If I run this directly via the console or start spyder via the console everything is fine. It seems that the bash_profile is not loaded when spyder is loaded but requires the console to do so

Running Python script from cmd line but start with import in code

I am running a Python script from Linux command line, and the script itself, on the first line, import several modules. I got some error message and searched online. Here is a reply from the author of the Python script:
it appears that you are running dexseq_count.py as if it were a shell script, rather than from Python. As a consequence, the first line of the script is interpreted as the Linux command 'import' rather than as Python code, leading to the error you report.
I am curious if the first line of import in Python has been mis-interpretated in Linux, and if so, how can I solve this problem? I have to run in the cmd line instead of in Python.
Thanks so much!
Two solutions here:
You can run the script using python like this: python my_program.py or add this at the top of the file: #!/usr/bin/env python which will switch from bash to python to run this script

Categories