How to Make an Alias (shortcut) to Run a Python Program?

Discussion in 'Mac Ham Radio on macOS & iOS' started by N0TZU, Feb 8, 2020.

ad: L-HROutlet
ad: l-rl
ad: Left-3
ad: L-MFJ
ad: Subscribe
ad: abrind-2
ad: Left-2
  1. N0TZU

    N0TZU Platinum Subscriber Platinum Subscriber QRZ Page

    Seems easy, but there appears to be no way to direct the execution to the Python interpreter, rather than opening the file for editing. Sheesh, Windows let’s you specify this in a shortcut, but apparently not MacOS. I can run it from a command window or from Idle but those are cumbersome compared to clicking on an icon.

    Am I missing the obvious or do I need to write an apple script or something?
     
  2. K3DCW

    K3DCW QRZ Lifetime Member #212 Volunteer Moderator Platinum Subscriber Life Member QRZ Page


    I've never messed with Python on macOS. Hopefully someone else has and can offer some advice.
     
  3. AG6QR

    AG6QR Premium Subscriber QRZ Page

    Two steps:

    1. Edit the file, and make sure the start of the first line looks like:
    #!/usr/bin/python​
    The very first character in the file should be the '#'.

    2. Make sure the file is executable. You can do this by opening a terminal, navigating to the directory containing the python program, and typing "chmod 755 pythonprog.py" But of course, substitute the name of your python program where I typed "pythonprog.py"

    MacOS is really Unix, with a bit of a Mac GUI wrapped around it. The above two steps are straight out of any guide to Unix.

    Now, whenever you click on the program or type its name at a terminal, it will execute via python. If you prefer, you may rename the file to eliminate any suffix it may have, (e.g. rename pythonprog.py to pythonprog) but that's optional.

    If you put the program in your ~/Desktop folder, it will show up on your desktop.
     
    N0TZU and K3DCW like this.
  4. K3DCW

    K3DCW QRZ Lifetime Member #212 Volunteer Moderator Platinum Subscriber Life Member QRZ Page

    Good points, Rich, and that should get Bob rolling.

    I should've thought of this as I use scripts in my system CRON folders to do stuff like update call sign lists and the like on a daily/weekly basis. This is Scripting 101 on macOS/Unix.

    Thanks!
     
  5. N0TZU

    N0TZU Platinum Subscriber Platinum Subscriber QRZ Page

    Thanks! I’ll try that.
     
  6. N0TZU

    N0TZU Platinum Subscriber Platinum Subscriber QRZ Page

    Sadly, this didn't work. I did put the line in (tried both ~python and ~python3), and changed the permissions but still couldn't get it to execute except by invoking it by command line in terminal, which is the same as before.

    The program I'm using is NanoVNASaver, and put the line in the file NanoVNASaver.py. Maybe there's something more complex going on, maybe other scripts that get called that aren't executables?
     
  7. K3DCW

    K3DCW QRZ Lifetime Member #212 Volunteer Moderator Platinum Subscriber Life Member QRZ Page

    Use Automator to create an application which will execute that script at the command line every time you need it, then save it in your Dock or Applications folder.

    I can't remember the exact steps as I'm not in front of my Mac, but that's exactly the kind of thing that Automator excels at.
     
    N0TZU likes this.
  8. N0TZU

    N0TZU Platinum Subscriber Platinum Subscriber QRZ Page

    Thanks, I’m reading about it right now.
     
  9. AG6QR

    AG6QR Premium Subscriber QRZ Page

    What command are you using to invoke it via the command line in terminal? I'm guessing it might be:

    python NanoVNASaver.py
    or
    python3 NanoVNAServer.py

    If it's the first, what is the response at the command line from the command
    which python

    If it's the second, what is the response at the command line from the command
    which python3

    Whatever that is, put it after the #! in the first line of the program. For example, if you use the command:
    python NanoVNASaver.py

    and if the response to "which python" is "/opt/local/bin/python"

    Then the first line of NanoVNASaver.py should be

    #!/opt/local/bin/python
     
  10. N0TZU

    N0TZU Platinum Subscriber Platinum Subscriber QRZ Page

    Thanks! This gave me the clue to figure it out.

    The program is invoked simply with nanovnasaver. No python3 needed ahead of it. So, there must already be an executable and it must know where Python 3 is or be in the path.

    which shows Python 2.7 is in /usr/bin, not used for this program.

    But which python3, required for NanoVNASaver returns
    /Library/Frameworks/Python.framework/Versions/3.8/bin/python3

    Sure enough there is an executable there for NanoVNASaver. Making an alias of this and putting it on the desktop works fine.

    Here is that executable text, if you are interested:

    #!/Library/Frameworks/Python.framework/Versions/3.8/bin/python3
    # EASY-INSTALL-ENTRY-SCRIPT: 'NanoVNASaver==0.2.2','console_scripts','NanoVNASaver'
    __requires__ = 'NanoVNASaver==0.2.2'
    import re
    import sys
    from pkg_resources import load_entry_point

    if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(
    load_entry_point('NanoVNASaver==0.2.2', 'console_scripts', 'NanoVNASaver')()
    )

    I used the normal Python3 installer, rather than using Brew as the NanoVNASaver notes suggested. Python3 shows up in the Application folder. I'm guessing that Brew would have put it in /usr/local/bin. I'm not sure if this is going to cause other problems later...

    If you haven't noticed, I'm out of my depth with this stuff - CS isn't my strong suit and I never learned anything about Unix...:)
     
    Last edited: Feb 9, 2020
    AG6QR and K3DCW like this.

Share This Page