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.
Related
I have a .py python script and when I run it typing ./filename.py I obtain a syntax error. However, when I run it typing python filename.py my program executes correctly.
How to make it run correctly typing ./filename.py ? I think it is related to the $PATH variable but I don't have any further idea.
put either a shebang at the start of the file, or run with python
for example:
#!/usr/bin/env python3
or run:
python3 filename.py
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
I can use python 3 in terminal fine, but I don't know how to make it so terminal will run a program that I have written in python 3.
What do i have to do to associate the .py file extension with python3.2.3 for terminal and not python2.7.1
I am using textwrangler as my text editor, but will happily use any editor if it will run, though I don't think this is my problem as idle doesn't work either and it doesn't have line numbers in it either.
Kind regards
Rob
Add a python3 hashbang to the beginning of your scripts:
#!/usr/bin/env python3
# do stuff
Then, you can make your script executable and run it:
chmod +x script.py
./script.py
try python3 yourprogram.py in your terminal.
or by adding this line on the top of our programs, this is the path to your interpreter:
#!/usr/local/bin/python3.2
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
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.