python executing in IDLE, but not in termnal - python

I have a python script on a Raspberry Pi reading the temperature and humidity from a sensor. It works fine when started in IDLE, but when I try starting it in a terminal I get the message:sudo: unable to execute .thermostaatgui.py: No such file or directory. The first line in the script is: #! /usr/bin/python, the same as in other scripts that run without problems and the script is made executable with chmod +x.
In the script Adafruit_DHT, datetime and time are imported, other scripts that work do the same.

+1 on the above solution.
To Debug
try this
Type "pwd" on your terminal. This will tell you where you are in the shell.
Then type "ls -lah" and look for your script. if you can not find it, then you need to "cd" to the directory where the script exists and then execute the script

Looks like you might have just made a typo:
sudo .thermostaatgui.py
should probably be
sudo ./thermostaatgui.py
assuming that you're in the directory containing your script, and that it's called thermostaatgui.py.

Well, still a little puzzled why it happened, but anyway this solved the problem:
As a workaround, I copied the contents of "thermostaatgui.py" over the contents of a working script ("mysimpletest.py"), saved it and it runs OK.

Related

Run terminal script in python?

I am using python and I am trying to run a shell script that is located in another folder I am trying
subprocess.call(['source','../Apps/appName/run'])
Where 'run' is a shell script I wrote and made an executable. But it keeps giving errors such as
No such file or directory or **No such file or directory: "source"
I have also tried the following
subprocess.call(['source','../Apps/appName/run'])
subprocess.call(['source ../Apps/appName/run'])
subprocess.call(['.','../Apps/appName/run'])
I am trying to run the script and leave it alone (as in I do not want any return value or to see what the output of the shell script is.
Thank you in advance
source is a shell builtin that reads a file and interprets the commands in the current shell instance. It's similar to C #include or Python import. You should not be using it to run shell scripts.
The correct way to run a script is to add a shebang like #!/bin/bash to the first line, and chmod +x yourscriptfile. The OS will then consider it a real executable, which can be executed robustly from any context. In Python, you would do:
subprocess.call(['../Apps/appName/run'])
If for whichever reason this is not an option, you can instead explicitly invoke bash on the file, since this is similar to what would happen if you're in bash and type source:
subprocess.call(['bash', '../Apps/appName/run'])

Executing a python script from the terminal in Unix

I have a python script and I can't figure how to execute it in the terminal. The script has the #!/usr/bin/python at the beginning, is executable and I've tried locating myself in the right directory and python name.py but what I want to print (ergo what the script says it should print) doesn't print in the terminal.
I feel I'm missing something... I just started with this so... Help!
If name.py is executable, you can run it using:
./name.py
As for the #!, it is better to use as a first line:
#!/usr/bin/env python
This way path to python interpreter is not hardcoded, the first python found in $PATH is used instead.
Also: it makes your script to run in fresh environment (you can see man env for more information).

Permission Denied when executing python file in linux

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

Shell script called using Python subprocess call() takes no effect

I am having problems executing a shell script using subprocess.call() and I made this test in Python console to try to figure out which is the problem:
import subprocess
subprocess.call(["touch", "/tmp/out.txt"])
This works and creates the file in tmp folder. However, none of these two calls work:
subprocess.call(["sh", "/tmp/test.sh"])
subprocess.call(["/tmp/test.sh"])
/tmp/test.sh:
#!/bin/bash
touch out.txt
exit 0
Basically, executing an script from subprocess.call() is not producing any output. I gave full permissions to files and folders to avoid any problem. It seems the problem may be related to the user executing the script, but it is the same user as in the first case, which is working.
Any idea what the problem could be?
BTW, I am using Ubuntu 12.04 LTS, Python 2.7.4.
Thanks in advance.
Your script is executed correctly. The problem is with your bash script.
You have to understand that this script inherits working directory of the parent script. Therefore out.txt will be created in the directory where you run the python script which makes use of subprocess.call.

Permission denied for Python script using Bash?

I am new to Ubuntu... I am trying to run my first simple python program "Hello World" ...
After running following commands in terminal
1. chmod +x filename.py
2. ./filename.py
terminal is showing following error "bash: ./filename.py: Permission denied"
what can I do for solve about problem?
Do you have the appropriate incantation at the top of your python file? e.g.,
#!/usr/bin/python (or alternatively #!/usr/bin/env python)
Just to clarify, chmod +x only makes a file executable, it doesn't run it.
And I'm assuming your script looks like nothing more complex than this:
#!/usr/bin/env python
print 'hello world'
Some possibilities:
What does it say if you type umask? chmod +x will only make a file executable for you if your umask doesn't block the user executable bit. A typical umask such as 0022 will not block the user execute bit, but a umask like 0122 can. (See the Description section of chmod(1) for more info.)
To execute a script such as a Python script, you also need read permission. Try chmod u+rx filename.py and execute the script again.
It's also remotely possible that whatever interpreter you've specified in the file with the "hashbang" line at the beginning of your file (e.g. #!/usr/bin/env python) isn't executable, although that in my experience yields a different error message.
I deal with the same problem on my new system.
It is the third time I tried to solve this, and your post is the first one appearing on google results. My post is late, but think that it will help another users with the same problem.
In my case, it was about partition table setup.
Check in your /etc/mtab file how python script is being stored. Check if there is a clause: noexec
noexec is a flag that forbid executing under the partition. By default, it is set with exec. But, sometimes, this kind of things happen.
Now, it is working fine here.

Categories