06/08/2019

How to Run Python Script On macOS in 5 Simple Steps?

Follow 5 simple steps to Run the Python Script On macOS. Python on a Macintosh with Mac OS X is in norm very comparable to Python on various Unix platforms, however, several supplementary traits like Package Manager and IDE are worthwhile pointing out.

5 Simple Steps to Run Python Script On macOS- Table of Content

  1. Download and Installation of Mac Python
    1. Running Python Script 
    2. Run Scripts with a GUI
    3. Configuration
  2. The IDE- Second Step to Run Python Scripts on Mac
    1. Installing Additional Packages of Python
    2. Mac GUI Programming
    3. Distributing Python Applications on the Mac
    4. Additional Resources
Modules that are specific to the Mac are well documented in https://docs.python.org/2/library/mac.html#mac-specific-services.

If you are familiar with Python Programming on the UNIX platform or Windows then you need to understand that what you will experience in Mac OS 9 or its predecessors may vary with sharp contrasts. An elaborate discussion is beyond the scope of this writing.

Download and installation of Mac Python- Run Python Scripts on Mac


Apple INC has been generous. Hence, this latest rendition of the Mac operating system i.e. Mac OS X 10.8 comes with an ad hoc installation of Python 2.7. Nevertheless, you always have the option of installing the latest release of Python from the Python website (https://www.python.org).

A current "Universal Binary" Python compilation is available there, running natively on the new Intel and PPC CPUs inherited from the Mac.

First Step to Run Python Scripts on Mac OSx

What you get after installation is a series of things

A MacPython 2.7 folder in your Applications folder. Here you will find IDLE, it is the official development environment as an integral part of the Python distributions; Python Launcher, which handles Python clicks by double-clicking the Finder; and the tool called build applet, which provides the provision of packaging Python scripts in the form of stand-alone applications on your system.

The framework /Library/Frameworks/Python.framework, including libraries and the executable file of Python. The installer is responsible for adding the location into the shell path.

If you ever need to uninstall Python from the Mac OS make sure you permanently delete the following three things. A symbolic link to the executable file of Python is placed in /usr/local/bin /.

The Python compilation provided by Apple has a copy of installation /usr/bin/python, and /System/Library/Frameworks/Python. framework respectively. Always remember that Apple controls these. Moreover, Apple and other third-party software may use it.

Hence, be careful not to terminate or manipulate them. Suppose you need to install the latest configuration of Python on your Mac. To accomplish this task it is a mandate to have an installation of two functional units independent of each other. Hence, it is imperative to ensure that your purpose is complementary to the routes and uses.

IDLE features a menu for help that will grant you the provision of Python documentation. If you have never programmed in Python before the introduction given in the tutorial is a good starting point.

If you have familiarity with working on Python in different UNIX platforms, then you need to go through the section that highlights the executing script of Python from the.UNIX Shell

1.1. Running Python Script


The best possible way to run Python on Mac OS X is by making use of the integrated environment of development called IDLE, see the IDE section for using the Help menu while you are running the IDE.

If you are desirous of running the Python script from the command line of the Terminal Window or through the Finder, first for creating a script you need a text editor. When it comes to command-line editors Mac OS X has plenty of them in its offering.

Vim and Emacs are two of these, which are quite similar to command-line editors given in UNIX. Other popular editors are BBEdit, Gvim, Aquamacs, TextMate, and TextWrangler.

To run your script from the Terminal window, you must ensure that /usr/local/bin is in your shell search path.

There are two options if you wish to execute your script from the finder:

You can use the PythonLauncher and its drag-and-drop

Do not forget to select the default application. Choose Python Launcher to open the script (or a different .py script) from the search engine information window.

You need to start it with a double click. When it comes to executing a script there are several references provided in the PythonLauncher. By default, you can use the drag-and-drop option, or customize the preferences menu and inflict the changes globally. 

Second Step to Run Python Scripts on Mac OS

1.2. Run Scripts with a GUI


With previous versions of Python, there is a peculiarity of Mac OS X that you should keep in mind: programs communicating with the window manager called Aqua (in other words, anything that has a GUI) must have a unique way to run.

Instead of using Python for running the script, you can use Python. You have the option of using Python or Python with Python 2.7

1.3. Configuration


Python in OS X complements the UNIX environment variables, like the PYTHONPATH. The Finder holds the configuration setting of these variables. Remember that the settings are not standardized. The Finder is incapable of reading its .cshrc or .profile when it's a start-up.

Hence, you need to create a ~/.MacOSX/environment.plist file. See the technical document Apple QA1067 for more details.

The IDE- Second Step to Run Python Scripts on Mac


The development environment of MacPython is the standard IDLE. You can find a comprehensive induction into the use of IDLE at https://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html.

2.1. Installing Additional Packages of Python


Install additional Python packages there are two ways
You have the option of using the standard distribution mode (python setup.py install).

You can install different packages through the extension of the setup tool or the pip wrapping, check https://pip.pypa.io/.

2.2. Mac GUI programming


You have different options for creating GUI applications in a Mac OS X through Python.

PyObjC serves as a link between Python link and Apple's Objective-C framework called Cocoa, which serves as the foundation for contemporary Mac development. You can get all the necessary information on PyObjC at https://pythonhosted.org/pyobjc/.

Tkinter is the standard toolkit of Python GUI, which is based on the Tk toolkit cross-platform (https://www.tcl.tk). Apple includes an Aqua-native version of Tk, and Apple can download and install it from https://www.activestate.com; you can develop it from the source.

wxPython is a GUI toolkit that is compatible with cross-platform running natively on Mac OS X. Packages and documentation are available at http://www.wxpython.org.

Read: Executive Summary of Python Language

PyQt is a well-known cross-platform compatible GUI-based toolkit running natively on Mac OS X. Get more information at https://riverbankcomputing.com/software/pyqt/intro.

2.3. Distributing Python Applications on the Mac


If you wish to run Python scripts in the form of a standard Mac application then you can use the Build Applet tool featured in the folder MacPython 2.7. However, it has a limitation. You cannot use it to distribute a Python application to different users.

On Mac is py2app is the standard tool to implement independent Python applications. You can find more information about the installation and use of py2app at http://undefined.org/python/#py2app.

2.4 Additional Resources


Python developers can try their hands on the MacPython mailing list, which is a kind of support tool and to learn more about Python Read Python Strings.
Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

1 comment:

  1. Visit us for the best Articles on App Development and trends. Mostly for those who looking for Taxi Apps and Business can view the importance and gain knowledge on how to choose the best Taxi Service Providers and how to choose the best Taxi Booking App for your Business and enlarge the business to next level.
    Visit here for the best knowledge: https://www.taxionthego.com/blog/

    ReplyDelete