Identical shebang not working - python

So I'm trying to create some scripts that I want to run without manually specifying the interpreter each time I run it.
#!/usr/bin/python
Above is the shebang on an existing script that runs like I want it to.
Below is the shebang of a script I wrote from scratch
#!/usr/bin/python
To me they look identical, but running the second one gives me a
helloWorld.py: permission denied
Both have been created using kate, utf-8 and unix lines.
Both are identical to me.
Any ideas?

The shebang may be correct, but the script also needs execute permissions.
# Anyone can execute
chmod +x helloworld.py
# Only the file owner can execute
chmod u+x helloworld.py

You need to set the permissions of the script. Try:
chmod u+x helloWorld.py
and run again.

The issue is not the permission of /usr/bin/python but rather of the actual script.
If you are running from the command line and not passing the script name as an argument to python then the script has to be executable.
If it is not then fix using chmod chmod +x helloworld.py

Related

How do I change the alias of python executable in command line?

Everytime I need to run a python file, I need to enter this into the command line.
python3.7 filename.py
Is there a way I can change the name from python3.7 to just py or something shorter?
alias py=python3.7
py filename.py
Add the alias to you bash_aliases to get it in every terminal
If you're using linux, you can shorten it to nothing by adding the line
#!/usr/bin/env python3.7
to the top of your python file. Then chmod 755 <filename.py> and run it like any other executable.

How to execute python3 program in shell script

I have a python program which I need to run at a particular day of a month, so I am using crontab for this task and create a shell script to run this python program.
This is part of my shell script:
#!/bin/bash
filepath='file2018'
cd ${filepath}
python3 file.py
When I run the crontab which executes the shell script, the log file shows the following error:
line 9: python3: command not found
I'm really confused about why this error occurs because I have already install python3 and I can run python3 directly from the command line.
Besides, if I replace python3 with python, the shell script works! My python version is python2, but I have to use python3 for this program, so I have to use python3 instead of python.
My operating system is Linux CentOS.
Hope someone can give me some tips!
You can give the full path to the python3 executable. You can get it using the which python3 command. Try it out.
in file.py add first line like below and add +x permission to file.py file
#!/usr/bin/python3
it will automatically execute, no need to mention python3 in the script
use "which python3" command to know exact path of python3 in your machine

Ubuntu - How to run python script after logging in?

I created a simple python script to change my ubuntu wallpaper. I want to this script run after whenever I logged in.
I tried to add command in startup application as python /bin/wallpaper_changer.py but it doesn't work.
I also tried to add a desktop entry in ~/.config/autostart/Myscript.desktop but it also doesn't work.
I also added this file path in crontab using sudo crontab -e #reboot python /bin/wallpaper_changer.py it also doesn't work.
I added entry in rc.local as python /bin/wallpaper_changer.py it also doesn't work
Maybe this link will help you. I prefer to put a start-up command in
/etc/rc.local. You can call it by typing on terminal sudo nano /etc/rc.local. You can also use crontab.
I found solution for this.
first make your source file executable.
chmod +x /path/to/your/file
after add it to cron tab.
sudo crontab -e
If it asks for any editor choose editor options.
after it add your path when # symbols end.
#reboot /path/to/your/file

run python script directly from command line

#!/usr/bin/env python
I put that at the top of a script. I've seen that should make the script runnable from the command line without the need for python programname.py. Unless I'm misunderstanding I should be able to use programname.py as long as I have the above line at the top of the script. Is this correct?
It isn't working for me I just get an error indicating that I would have to use python at the beginning of the 'call'.
Universal running of Python scripts
You can pretty much universally run without the shebang (#!) with
python myscript.py
Or nearly equivalently (it places the current directory on your path and executes the module named myscript) (preferably do this!):
python -m myscript
from the command line, as long as you have Python installed and on your path environment variable (i.e. set to run with python, which, if installed, would typically be the case).
Shebangs (#!) are a Unix thing.
The shebang, as you're using it, is typically for running on a Unix platform (typically Apple or Linux). Windows would typically require cygwin to use the shebang.
You can usually default to whatever python is available on your system path with:
#!/usr/bin/env python
Assuming you're on a Unix, you might try other locations for your python setup, like:
#!/usr/bin/python
Muddling through
You can see what python you're currently using by using the unix which command, so if you want to see where your python is coming from, use this command:
which python
or on Windows (cygwin probably can run the shebang):
where python
On Linux/Unix, you'll need execution perms to run the file as well, in that manner. Use chmod
chmod +x myscript.py
(chmod also may apply to Cygwin in Windows)
If you're not running as root, you may require sudo, and that would be
sudo chmod +x myscript.py
And then attempt to run (within the same directory) with
./myscript.py
make the file executable
sudo chmod +x /path/to/file.py
and then from the same directory as file.py:
./file.py

How do I get linux to automatically run my python script in the Python interpreter?

I've decided that it would be good for me to move outside of my .NET bubble and start experimenting with other technologies. I have Ubuntu12 running and python2.7 and 3.2 are installed. I can run code directly in the interpreters.
I have a basic script on the filesystem called Standalone.py:
#!/usr/bin/env python3.2
import sys
print("this is a standalone script.")
When I'm at my bash prompt I type $ python3.2 Standalone.py. I get a response saying this is a standalone script. But when I type $ Standalone.py then it tells me that the command is not found.
How do I run such scripts?
Thanks for any help.
update
I changed the permissions of Standalone.py to 755. Then I ran the command:
$ ./Standalone.py
and received the message:
: No such file or directory
I then switched the permissions of Standalone.py back to 644. Then when I ran
$ ./Standalone.py
I received the message
-bash: ./Standalone.py: Permission denied
Is there something I'm missing?
You need to make the script executable using
chmod +x Standalone.py
Usually, the current directory is not searched for executable files, so you need to use
./Standalone.py
to tell the shell that the script is in the current directory.
Make sure your script file has linux newline (just \n) not windows newline (\r\n). Did you write the script on windows? This happened to me once. You should check your editor settings.
Your script should start with #!/usr/bin/python not #!/usr/bin/env python3.2
Make sure you're in the folder where your script is located you can check with ls
chmod +x Standalone.py
./Standalone.py
At first, to excecute a script it need to be executable. So you either have to do a chmod +x $file or a chmod 0740 $file. When you set the file permission to 644 you are putting the execute right away, so if gives you an error. If you are unsure of execution right and octal notation, you can use this : http://permissions-calculator.org/decode/0644/.
To really answer your question then, if you want to call the script with $file.py it needs to be in your PATH variable. You can display it with echo $PATH. Those are the directories that are searched for script to execute. So you simply need to give your script the executable right and put it in one of the directory given by your PATH.
Can you check if /usr/bin/python or /usr/bin/python3.2 exists
Execute below comamnd:
which python3.2
and then use the resulting path on top of you script.

Categories