Reputation:
Whilst running python scripts from my linux terminal, I find myself typing in python myfile.py
way too much. Is there a way on linux (or windows) to execute a python script by just entering in the name of the script, as is possible with bash/sh? like ./script.py
?
Upvotes: 2
Views: 386
Reputation: 6185
The first line of your Python script should be:
#!/usr/bin/env python
or
#!/usr/bin/env python3
Depending on your version, and if Python 3
is your default or not.
Then, set executable bits at the shell (maybe with sudo
if needed):
chmod +x my_script_name.py
Note that with the above done, you could rename your Python script
mv my_script_name.py my_script_name
and then execute your Python script just by:
my_script_name
at the shell line.
Upvotes: 0
Reputation: 378
you will need to chmod 0755 script.py
and as a first line in script have something like
#!/usr/bin/python
Upvotes: 1
Reputation: 1537
At the top of the script, put
#!/usr/bin/python
or whatever the path to Python is on your computer (the result of which python
on Linux). This tells the system to run your script using python
. You'll also need to do chmod +x script.py
for it to work.
Or if you're really lazy, use alias p=python
or something.
Upvotes: 3