Alias to Launch Python .py Script
Question: I am trying to create an Alias to launch mystepper6.py and moveit.py and sudo
ps ax by placing the following alias' in sudo nano ~/.bashrc (Note: I am using
Python 2 for this script.)

    
    
    reboot='sudo reboot'
    ax='sudo ps ax'
    runstepper='python home/pi/mystepper6.py'
    moveit='sudo python home/pi/moveit.py'
    

The alias reboot works fine but none of the others work at all. All I get is
"bash: runstepper: command not found".

I am doing this because I am trying to control my webcam on my Raspberry Pi 2
using my iPhone with the iFreeRDP app. I use remote desktop connection from my
Windows 10 laptop. The problem with this app and some other similar apps, is
that the period and space keys do not function (it is a know reported issue).
This makes typing full commands impossible.

Incidentally, I tried to use the VNC Viewer iPhone app and got my Raspberry Pi
2 hijacked when I loaded the required software onto the RPi2 requiring me to
get a new SD card. Fortunately, I just cloned my SD card a few hours prior.
Long story, but I am very weary about using VNC Viewer now.

Please help me with my alias' so I can either type one word with no spaces or
periods or create a desktop short cut that I can double click so I can use it
as a workaround for the deficiencies of these otherwise good apps. I am not
sure the Ctrl + C works on the app keyboards either, so a short cut for that
would be good as well.


Answer: to create aliases in your shell, you shall use the
[`alias`](http://ss64.com/bash/alias.html) shell directive:

    
    
    alias reboot='sudo reboot'
    alias ax='sudo ps ax'
    

To run `ps ax` you do not need to `sudo` first. If you're running a standard
kernel, any user can see the list of all processes without special privileges.

For the two python aliases:

    
    
    alias runstepper='python home/pi/mystepper6.py'
    alias moveit='sudo python home/pi/moveit.py'
                              ^-- missing / here
    

do not forget about the first `/` in the path, or whenever you launch the
aliased command, you'll have python look up for the script relatively to the
current directory. i.e. if you're in `/home/pi`, it will look it up into
`/home/pi/home/pi/movestepper6.py` and tell you the script does not exists. So
the proper command should be:

    
    
    alias runstepper='python /home/pi/mystepper6.py'
    alias moveit='sudo python /home/pi/moveit.py'
    

* * *

Though as a suggestion to you, instead of making aliases to run python
scripts, I'd make them into a proper python package. Considering that within
both codes your entry points are a function called `main()`, i.e., both
scripts end with:

    
    
    if __name__ == "__main__":
        main()
    

you should create a directory for your project:

    
    
    cd /home/pi
    # create a directory for your python project:
    mkdir motion_control
    # create a directory to place your scripts within:
    mkdir motion_control/motion_control
    # adding an empty __init__.py file makes that directory a python package
    touch motion_control/motion_control/__init__.py
    nano motion_control/setup.py
    

and now you just have to add this within the setup.py file:

    
    
    from setuptools import setup
    
    setup(name='motion_control',
          version='0.1',
          description="Python library to operate stuff that move on my rasppi",
          long_description='explain how to use the tools installed by this package',
          classifiers=[],
          keywords='raspberrypi motion control',
          author='YOU',
          author_email='YOUR@EMAIL',
          url='ANY URL YOU THINK IS RELEVANT',
          license='MIT', # or any license you think is relevant
          packages=['motion_control'],
          zip_safe=False,
          install_requires=[
              # add here any tool that you need to install via pip 
              # to have this package working
              'setuptools',
          ],
          entry_points="""
          # -*- Entry points: -*-
          [console_scripts]
          runstepper = motion_control.mystepper6:main
          moveit = motion_control.moveit:main
          """,
    )
    

the `entry_points` part is very important, as it's telling python where to
look for the first function to execute to have the script run. For example:

    
    
          moveit = motion_control.moveit:main
    

means "look for the main() function within the moveit module in the
motion_control package". So adapt accordingly! As a note: don't make that
`main()` function take any parameter, but rather do the argument parsing
within it (if you parses arguments).

and finally, to install it, all you need to do is:

    
    
    cd motion_control
    sudo python setup.py install
    

and you'll have `runstepper` and `moveit` installed in the same directory as
your python executable.

HTH

