Permission Denied when executing python file in linux - python

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

Related

A python problem in running the command to execute a python file in vs code

I was trying to run a python file "exe.py" on vs code by typing:
python3 .\exe.py
But it comes with the following replys from vs code:
python3: can't open file '/Users/exercise/my-python-app/myenv/.exe.py': [Errno 2] No such file or directory
No ideas why couldn't it works given that I already installed the python package and running the command in appropriate environment.
You are running
python3 .\exe.py
which is escaping the e and therefore is replaced by e as there's no matching escape char and it becomes
python3 .exe.py
You need, as other response states
python3 exe.py
or
python3 ./exe.py
In fact python3 is installed in your system but you are typing incorrectly the command. Imagine that you have the python file named exe.py with the following code
print("Hello world")
If you are in the same directory of the file you can execute it using the command
python3 exe.py
Another way to execute exe.py is using the shebang line. The shebang is a line that you can write to tell to the operative system which executable must use to interpret the code of the file. If you add the typical shebang for python3 in linux at the beginning of exe.py
#!/usr/bin/env python3
print("Hello world")
and you tip the command
./exe.py
you will see the result of the execution too. If you get an error probably you have to give permissions to the file with the command
chmod 777 exe.py
You can find a lot of resources talking about the shebang. The following link talks about the shebang in python
https://www.pythonpool.com/python-shebang/
I hope you found it useful.
Greetings.

Can apache run a python script which is NOT executable?

I setup Ubuntu server 18.04 LTS, LAMP, and mod_mono (which appears to be working fine alongside PHP now by the way.) Got python working too; at first it gave an HTTP "Internal Server Error" message. sudo chmod +x myfile.py fixed this error and the code python generates is displayed fine. But any time the execute permission is removed from the file (such as by uploading a new version of the file), the execute bit is stripped and it breaks again.
A work-around was implemented with incrontab, where the cgi-bin folder was monitored for changes and any new writes caused chmod +x %f to be ran on them. This worked for awhile, then stopped, and seems a hokey solution at best. Perl, PHP, even ASPX do not need to be marked executable - only python.
Is there any way Apache can "run" python without the file marked as executable?
The reason PHP works, is because the interpreter is loaded into Apache. So Apache interprets the code.
For your Python, it runs as a CGI, so the interpreter is outside of Apache.
In your Python script, you probably have a #!/usr/bin/python first line (or something similar). This tells the script to run using this interpreter. This requires executable permission on the .py file, and allows you to call myfile.py directectly.
Instead run it like this: /usr/bin/python myfile.py. This way the interpreter is the executable, and it will run myfile.py as the code.
Examples
You want to run the py file "alone":
file.py
#!/usr/bin/python
print("Hello")
Running it:
./file.py
You want to run it via the python executable, like you want via Apache:
file.py
print("Hello")
Running it:
/usr/bin/python file.py
I don't think Apache is capable of serving executed python scripts without the execute bit set on the .py file.
But here is a work-around: simply leave that file marked executable, but import a second python file. That second file does not need to be marked executable.
myfile.py (marked as executable and read-only - use this with apache):
#!/usr/bin/python3
# enable debugging
# helper to run the other, non-executable file
# do not add .py to the import "filename"
import myfile2
myfile2.py (marked RW only, edit this file freely):
# this is the code which can change frequently
# and does not need to be marked executable...
print("Content-type: text/html\n\n")
print("<html><head><title>Python</title></head>")
print("<body>Hello, World!</body></html>")

Ubuntu run python script on system startup that is using Firefox

I wrote python script that uses subprocess.pOpen() module to run and manipulate with 2 GUI programs: Firefox and VLC player. I am using Ubuntu 14.04 LTS operating system in Desktop mode.
My problem is when I try to run that python script when system starts, script is running but Firefox or VLC don't start.
So far, I tried to make shell script to run my python script and then with crontab with #reboot /home/user/startup.sh to execute my python script. I set all permissions for every script that is using. I gave my user root permisions so everything is OK with that.
I also tried to run my script putting command "sudo python /path/to/my/script.py" in /etc/rc.local file but that also is not helping.
I googled and found out people using .desktop files that they put in ~/.config/autostart/ directory but that also failed. Example of what I wrote:
[Desktop Entry]
Type=Application
Exec="sudo python /home/user/path_to_my_script/my_script.py"
X-GNOME-Autostart-enabled=true
Name=screensplayer
Comment=screensplayer
And I saved this as program.desktop in ~/.config/autostart/ directory but it does not work. I am sure there is a way to fix this but don't know how. Any help will be appreciated!
Found solution to my problem. When you are running commands with pOpen in python like this:
FNULL = open(os.devnull, 'w')
_FIREFOX_START = ["sudo", "firefox", "test.com/index.html"]
subprocess.Popen(self._FIREFOX_START, stdout=self.FNULL, stderr=subprocess.STDOUT)
it won't run apps because of "sudo" word, when I removed it, it worked.
Also run gnome-session-properties in terminal and add new startup application, be aware that you have to execute python script without sudo, like this:
python /home/user/path_to_script/script.py
Also, I granted my user root privileges so kepp that in mind.

python executing in IDLE, but not in termnal

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.

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