cscuilla
Posts: 58
Joined: Tue Apr 26, 2016 6:35 pm

Command line shortcut to run Python script.

Mon May 23, 2016 7:37 pm

I have a script saved at /home/pi/scripts/myscript.py..

To run the scirpt I type:
sudo python /home/pi/scripts/myscript.py

Is their a way to create a command line shortcut to run the script?

IE: I would like to simply type "myscript" in the command line to get the script to run.

Thanks for your time.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Command line shortcut to run Python script.

Mon May 23, 2016 7:53 pm

1) create myscript.py and place it in your PYTHONPATH---> /home/pi/Python3/

1b) create myscript_main.sh and place it in your PYTHONPATH

2) create myscript.sh and place it in your bin directory---> /home/pi/bin/

3) make the myscript.sh, myscript_main.sh file(s) executabe:

chmod 0754 /home/pi/bin/myscript.sh
chmod 0754 /home/pi/Python3/myscript_main.sh

/home/pi/bin/myscript.sh

Code: Select all

#!/bin/sh
export PATH=$PATH:/home/pi/bin/
export PYTHONPATH=/home/pi/Python3/

exec /home/pi/Python3/myscript_main.sh
/home/pi/Python3/myscript_main.sh

Code: Select all

#!/usr/bin/python3
from myscript import *     <---- this imports myscript.py

rest of script goes here

notes:
1) you will have to create bin in /home/pi/
2) you will have to create Python3 in /home/pi/
3) myscript.py contains your python header information
4) /home/pi/Python3/myscript_main.sh is your main python code
5) /home/pi/bin/myscript.sh is your python environment setup code

To launch your code just type

myscript.sh
marcus
:ugeek:

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Command line shortcut to run Python script.

Mon May 23, 2016 9:23 pm

Or you can just do it the linux way

2 choices
1)
Create a bash script with 2 lines in it and place it in /usr/local/bin you could call it myscript

Code: Select all

#!/bin/bash
python /home/pi/scripts/myscript.py
Then make it executable

Code: Select all

chmod +x ~/bin/myscript
To run it just type

Code: Select all

sudo myscript
2)
Make your script executible

Code: Select all

chmod +x /home/pi/scripts/myscript.py
make sure to add the correct shebang line to your script

Code: Select all

#!/usr/bin/env python2
or

Code: Select all

#!/usr/bin/env python3
Then symlink to it

Code: Select all

sudo ln -s /home/pi/scripts/myscript.py /usr/local/bin/myscript
To run it just type

Code: Select all

sudo myscript
I would go for choice 2. If you ever change where myscript.py is located you just update the symlink with the new path

Code: Select all

sudo ln -s /new/path/to/myscript.py /usr/local/bin/myscript
Also if your script is just a single file then you can move it to either /usr/local/bin or /home/pi/bin

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: Command line shortcut to run Python script.

Mon May 23, 2016 9:28 pm

I'd just create an alias for "myscript".

Code: Select all

nano ~/.bash_aliases
Scroll down to the bottom and add a new alias.

Code: Select all

alias myscript='sudo /usr/bin/python /home/pi/scripts/myscript.py &'
The next time you log in you'll be able to just type myscript to run it.

cscuilla
Posts: 58
Joined: Tue Apr 26, 2016 6:35 pm

Re: Command line shortcut to run Python script.

Mon May 23, 2016 11:06 pm

Thanks. It's working. I used the second option.. Thank you

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Command line shortcut to run Python script.

Mon May 23, 2016 11:38 pm

JimmyN wrote:I'd just create an alias for "myscript".
If you have to type it a lot, just name it without any extension. If you go the alias route for something like this, you've created two places you need to update when you move the script to another box. And what happens if you go, gosh, I think I'll rewrite this thing in python, it's getting boring in bash? Now you've got to learn the new name!

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Command line shortcut to run Python script.

Tue May 24, 2016 12:17 am

stderr wrote:
JimmyN wrote:I'd just create an alias for "myscript".
If you have to type it a lot, just name it without any extension. If you go the alias route for something like this, you've created two places you need to update when you move the script to another box. And what happens if you go, gosh, I think I'll rewrite this thing in python, it's getting boring in bash? Now you've got to learn the new name!
Aliases 'tend' to be used for system commands plus options (I have defined a 'root' alias that is 'sudo su -', 'll' which is 'ls -lrta', and 'poff' for 'sudo poweroff'') .

But I wonder why the OP needs to runs the python code as root as it can be devastating if things go wrong (as was recently demostrated by someone who wouldn't listen to the advice given)?

cscuilla
Posts: 58
Joined: Tue Apr 26, 2016 6:35 pm

Re: Command line shortcut to run Python script.

Tue May 24, 2016 12:27 am

I don't need to run as root. I guess it's a habit I should break..

Also, when I type..

nano ~/.bash_aliases

I get a blank page. Is this correct?

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Command line shortcut to run Python script.

Tue May 24, 2016 12:37 am

cscuilla wrote:I don't need to run as root. I guess it's a habit I should break..

Also, when I type..

nano ~/.bash_aliases

I get a blank page. Is this correct?
Yes, it we be blank until you put your aliases in it.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Command line shortcut to run Python script.

Tue May 24, 2016 12:55 am

cscuilla wrote:I don't need to run as root. I guess it's a habit I should break..
In the least, you risk ending up with permissions that make it hard to not use sudo all the time. If you are having to use sudo in the home directory of the user that you are running as, that is likely the problem.
Also, when I type..
nano ~/.bash_aliases
I get a blank page. Is this correct?
This is because that file doesn't exist and you are creating it anew. Go look in ~/.bashrc and near the top you'll see some examples of aliases that are already available. Make some sort of change to the file and then run

exec bash

and test them out.

It would be good, though, to add a user and test in that user because if the .bashrc or any other file that runs at start up gets messed up, you might not be able to log into that account easily. The alias stuff shouldn't be a problem but if you radically mess with things, it might be.

There is also the ~/.profile file that is run under certain circumstances. You might add

echo "running the ~/.profile file"

to that file and

echo "running the ~/.bashrc file"

to that file so you can get a feel for when each is run. Note that there is a weird problem that might occur where doing this causes sftp to complain for some reason. This only happened going into Ubuntu 14.04 AMD64 but remove what I just said fixed it. If you remember that there might be some issue like that, I think it's worth it to have this in the files.

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Command line shortcut to run Python script.

Tue May 24, 2016 1:03 am

cscuilla wrote:I don't need to run as root. I guess it's a habit I should break..
Please do. It doesn' really matter too much on the Pi (if you don't mind trashing the whole system now and again), but I hope that people going into sysadmin roles don't think that running as root will solve all their problems - I've seen (and had to fix) a system brought down (costing £10K a minute in lost revinue) by a DBA running a 'cleanup' script as root which wiped most of the database (a typo in a variable name) left me facing loads of people city-types literally screaming at me to restore it.

Aydan
Posts: 719
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Command line shortcut to run Python script.

Wed May 25, 2016 10:06 am

cscuilla wrote:I have a script saved at /home/pi/scripts/myscript.py..

To run the scirpt I type:
sudo python /home/pi/scripts/myscript.py

Is their a way to create a command line shortcut to run the script?

IE: I would like to simply type "myscript" in the command line to get the script to run.

Thanks for your time.
Do you have

Code: Select all

#!/usr/bin/python
as a first line in you script?
This will tell the shell which interpreter to start the script with.
Add execute rights as appropriate (chmod ug+x myscript.py allows user and group to execute the script)
Now you can just type

Code: Select all

./myscript
from /home/pi/scripts
if you want to be able to run it with just "myscript.py" then move it into e.g. /usr/bin (basically any path that is part of PATH)

Regards
Aydan

User avatar
rpdom
Posts: 17043
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Command line shortcut to run Python script.

Wed May 25, 2016 5:41 pm

Aydan wrote:Do you have

Code: Select all

#!/usr/bin/python
as a first line in you script?
This will tell the shell which interpreter to start the script with.
Add execute rights as appropriate (chmod ug+x myscript.py allows user and group to execute the script)
Now you can just type

Code: Select all

./myscript
from /home/pi/scripts
if you want to be able to run it with just "myscript.py" then move it into e.g. /usr/bin (basically any path that is part of PATH)
Yes, but that won't run the script using sudo.

There are various methods you can use.

Create a new directory (if it doesn't exist) call "bin" in your home directory. Next time you log in that will be in your PATH.
Create a script in that directory (nano bin/myscript) like this

Code: Select all

#!/bin/bash
sudo python /home/pi/scripts/myscript.py
Make it executable

Code: Select all

chmod +x bin/myscript
That should do it.

The other method is to set up a command alias

Code: Select all

alias myscript="sudo python /home/pi/scripts/myscript.py
but that will only last until you log out again. Make it permanent by creating a file called .bash_aliases
Then logout and in again and when you type "alias" you should see your command.

Code: Select all

pi@raspi3 ~ $ cat .bash_aliases 
alias myscript="sudo python /home/pi/scripts/myscript.py"
pi@raspi3 ~ $ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias ls='ls --color=auto'
alias myscript='sudo python /home/pi/scripts/myscript.py'
pi@raspi3 ~ $ myscript
python: can't open file '/home/pi/scripts/myscript.py': [Errno 2] No such file or directory
My Pi gives an error because I don't have a file called /home/pi/scripts/myscript.py, but yours should work.

User avatar
DougieLawson
Posts: 38895
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Command line shortcut to run Python script.

Wed May 25, 2016 6:22 pm

Easiest way to run python program with sudo is to change the shebang line from

Code: Select all

#!/usr/bin/python3
to

Code: Select all

#!/usr/bin/sudo /usr/bin/python3
make the script executable and it will run as root.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Command line shortcut to run Python script.

Wed May 25, 2016 6:41 pm

tom.slick wrote:Or you can just do it the linux way ...
ha! oh tom, that's like saying there's a Legos way of stacking plastic blocks!

:lol:

gnu+linux = infinite configurability | unlimited flexibility
marcus
:ugeek:

e-raser
Posts: 71
Joined: Sat Apr 04, 2015 1:30 pm

Re: Command line shortcut to run Python script.

Sun Aug 21, 2016 3:44 pm

rpdom wrote: The other method is to set up a command alias

Code: Select all

alias myscript="sudo python /home/pi/scripts/myscript.py
but that will only last until you log out again. Make it permanent by creating a file called .bash_aliases
Then logout and in again and when you type "alias" you should see your command.

Code: Select all

pi@raspi3 ~ $ cat .bash_aliases 
alias myscript="sudo python /home/pi/scripts/myscript.py"
pi@raspi3 ~ $ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias ls='ls --color=auto'
alias myscript='sudo python /home/pi/scripts/myscript.py'
My Pi gives an error because I don't have a file called /home/pi/scripts/myscript.py, but yours should work.
Hi,

I´m into creating some alias permanent. But it unfortunately does not work (running on Raspbian = Debian Jessie). Whenever a new SSH session is opened, there is no alias anymore. So it is NOT PERMANENT :( Any ideas?!? Should I open a new topic for this?
1x Nextcloud & Pi-hole & ... on Raspbian @ Pi (4 4 GB)
1x Kodi media center on LibreELEC @ Pi 3 B+

Return to “Python”