4. Using Python on a Mac
************************

Autor:
   Bob Savage <bobsavage@mac.com>

Python on a Mac running macOS is in principle very similar to Python
on any other Unix platform, but there are a number of additional
features such as the IDE and the Package Manager that are worth
pointing out.


4.1. Obteniendo e instalando MacPython
======================================

macOS since version 10.8 comes with Python 2.7 pre-installed by Apple.
If you wish, you are invited to install the most recent version of
Python 3 from the Python website (https://www.python.org).  A current
"universal binary" build of Python, which runs natively on the Mac's
new Intel and legacy PPC CPU's, is available there.

Lo que obtienes después de instalar es una serie de cosas:

* Una carpeta "Python 3.9" en su carpeta "Applications". Aquí
  encontrará IDLE, el entorno de desarrollo que es una parte estándar
  de las distribuciones oficiales de Python; y PythonLauncher, que se
  encarga de hacer doble clic en los scripts de Python desde el
  Finder.

* Un *framework* "/Library/Frameworks/Python.framework", el cual
  incluye los ejecutables y librerías de Python. El instalador añade
  esta ubicación a su variable de entorno. Para desinstalar MacPython,
  usted puede simplemente remover estas tres cosas. Un enlace
  simbólico al ejecutable de Python es colocado en /usr/local/bin/.

La compilación proporcionada por Apple de Python se instala en
"/System/Library/Frameworks/Python.framework" y "/usr/bin/python",
respectivamente. Nunca debe modificarlos ni eliminarlos, ya que están
controlados por Apple y son utilizados por software de Apple o de
terceros. Recuerde que si elige instalar una versión más reciente de
Python desde python.org, tendrá dos instalaciones de Python diferentes
pero funcionales en su computadora, por lo que será importante que sus
rutas y usos sean consistentes con lo que desea hacer.

IDLE incluye un menú de ayuda que le permite acceder a la
documentación de Python. Si es completamente nuevo en Python, debe
comenzar a leer la introducción del tutorial en ese documento.

Si está familiarizado con Python en otras plataformas Unix, debe leer
la sección sobre cómo ejecutar scripts Python desde el *shell* de
Unix.


4.1.1. Cómo ejecutar un *script* de Python
------------------------------------------

Your best way to get started with Python on macOS is through the IDLE
integrated development environment, see section El IDE and use the
Help menu when the IDE is running.

If you want to run Python scripts from the Terminal window command
line or from the Finder you first need an editor to create your
script. macOS comes with a number of standard Unix command line
editors, **vim** and **emacs** among them. If you want a more Mac-like
editor, **BBEdit** or **TextWrangler** from Bare Bones Software (see
http://www.barebones.com/products/bbedit/index.html) are good choices,
as is **TextMate** (see https://macromates.com/). Other editors
include **Gvim** (http://macvim-dev.github.io/macvim/) and
**Aquamacs** (http://aquamacs.org/).

Para ejecutar su *script* desde la ventana Terminal, debe asegurarse
de que: "/usr/local/bin" esté en su ruta de búsqueda de *shell*.

Para ejecutar su *script* desde el Finder, tiene dos opciones:

* Arrástrelo a **PythonLauncher**

* Seleccione **PythonLauncher** como aplicación predeterminada para
  abrir su *script* (o cualquier *script* .py) a través de la ventana
  de información del buscador y haga doble clic en ella.
  **PythonLauncher** tiene varias preferencias para controlar cómo se
  inicia su secuencia de comandos. La opción de arrastrar le permite
  cambiarlos para una invocación, o usar su menú de Preferencias para
  cambiar las cosas globalmente.


4.1.2. Ejecutar scripts con una GUI
-----------------------------------

With older versions of Python, there is one macOS quirk that you need
to be aware of: programs that talk to the Aqua window manager (in
other words, anything that has a GUI) need to be run in a special way.
Use **pythonw** instead of **python** to start such scripts.

Con Python 3.9, usted podrá utilizar ya sea **python** o **pythonw**.


4.1.3. Configuración
--------------------

Python on macOS honors all standard Unix environment variables such as
"PYTHONPATH", but setting these variables for programs started from
the Finder is non-standard as the Finder does not read your ".profile"
or ".cshrc" at startup. You need to create a file
"~/.MacOSX/environment.plist". See Apple's Technical Document QA1067
for details.

Para obtener más información sobre la instalación de paquetes de
Python en MacPython, consulte la sección Instalación de paquetes
adicionales de Python.


4.2. El IDE
===========

MacPython se entrega con el entorno de desarrollo IDLE estándar. Se
puede encontrar una buena introducción al uso de IDLE en
http://www.hashcollision.org/hkn/python/idle_intro/index.html.


4.3. Instalación de paquetes adicionales de Python
==================================================

Existen varios métodos para instalar paquetes Python adicionales:

* Los paquetes se pueden instalar a través del modo *distutils*
  estándar de Python ("python setup.py install").

* Muchos paquetes también se pueden instalar a través de la extensión
  **setuptools** o el *wrapper* **pip**, consulte
  https://pip.pypa.io/.


4.4. Programación de GUI en Mac
===============================

Hay varias opciones para crear aplicaciones GUI en Mac con Python.

*PyObjC* es un enlace de Python al *framework* Objective-C/Cocoa de
Apple, que es la base del desarrollo más moderno de Mac. La
información sobre PyObjC está disponible en
https://pypi.org/project/pyobjc/.

El kit de herramientas estándar de Python GUI es "tkinter", basado en
el kit de herramientas Tk multiplataforma (https://www.tcl.tk). Apple
incluye una versión nativa de Aqua de Tk, y la última versión puede
ser descargada e instalada desde https://www.activestate.com; También
se puede incorporar desde la fuente.

*wxPython* is another popular cross-platform GUI toolkit that runs
natively on macOS. Packages and documentation are available from
https://www.wxpython.org.

*PyQt* is another popular cross-platform GUI toolkit that runs
natively on macOS. More information can be found at
https://riverbankcomputing.com/software/pyqt/intro.


4.5. Distribuyendo aplicaciones de Python en la Mac
===================================================

The standard tool for deploying standalone Python applications on the
Mac is **py2app**. More information on installing and using py2app can
be found at https://pypi.org/project/py2app/.


4.6. Otros recursos
===================

La lista de correo de MacPython es un excelente recurso de soporte
para usuarios y desarrolladores de Python en Mac:

https://www.python.org/community/sigs/current/pythonmac-sig/

Otro recurso útil es el wiki de MacPython:

https://wiki.python.org/moin/MacPython
