5. Using Python on a Mac

저자

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.

5.1. MacPython을 구하고 설치하기

macOS used to come with Python 2.7 pre-installed between versions 10.8 and 12.3. 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.

설치 후 얻을 수 있는 것은 여러 가지가 있습니다:

  • A Python 3.12 folder in your Applications folder. In here you find IDLE, the development environment that is a standard part of official Python distributions; and PythonLauncher, which handles double-clicking Python scripts from the Finder.

  • 파이썬 실행 파일과 라이브러리를 포함하는 프레임워크 /Library/Frameworks/Python.framework. 설치기는 이 위치를 셸 경로에 추가합니다. MacPython을 제거하려면, 이 세 가지를 지우면 됩니다. 파이썬 실행 파일에 대한 심볼릭 링크는 /usr/local/bin/에 있습니다.

애플에서 제공한 파이썬 빌드는 /System/Library/Frameworks/Python.framework/usr/bin/python에 각각 설치됩니다. 애플에서 제어하고 애플이나 제삼자 소프트웨어에서 사용하므로, 절대로 수정하거나 삭제해서는 안 됩니다. Python.org에서 최신 파이썬 버전을 설치했다면, 컴퓨터에 서로 다르지만 동작하는 두 개의 파이썬 설치를 갖게 된다는 것을 기억하십시오, 경로와 사용이 여러분이 원하는 것과 일치하는 것이 중요합니다.

IDLE에는 파이썬 설명서에 액세스할 수 있는 도움말 메뉴가 포함되어 있습니다. 파이썬을 완전히 처음 접한다면, 이 설명서에서 자습서 소개를 읽는 것으로 시작해야 합니다.

다른 유닉스 플랫폼에서 파이썬에 익숙하다면 유닉스 셸에서 파이썬 스크립트를 실행하는 부분을 읽어야 합니다.

5.1.1. 파이썬 스크립트를 실행하는 방법

Your best way to get started with Python on macOS is through the IDLE integrated development environment, see section 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 (https://macvim-dev.github.io/macvim/) and Aquamacs (http://aquamacs.org/).

터미널 창에서 스크립트를 실행하려면 /usr/local/bin이 셸 검색 경로에 있도록 해야 합니다.

Finder에서 스크립트를 실행하려면 두 가지 옵션이 있습니다:

  • 스크립트를 PythonLauncher로 드래그하십시오.

  • Finder 정보 창을 통해 여러분의 스크립트(또는 모든 .py 스크립트)를 여는 기본 응용 프로그램으로 PythonLauncher를 선택하고 스크립트를 더블 클릭하십시오. PythonLauncher에는 스크립트를 시작하는 방법을 제어하는 다양한 설정이 있습니다. Option-드래그하면 하나의 호출에 대해 이를 변경할 수 있으며, 환경 설정 메뉴를 사용하여 전역적으로 변경할 수 있습니다.

5.1.2. 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.

파이썬 3.9에서는, python이나 pythonw를 모두 사용할 수 있습니다.

5.1.3. 구성

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.

MacPython의 파이썬 패키지 설치에 대한 자세한 내용은, 추가 파이썬 패키지 설치하기 절을 참조하십시오.

5.2. IDE

MacPython에는 표준 IDLE 개발 환경이 따라옵니다. IDLE 사용에 대한 좋은 소개는 http://www.hashcollision.org/hkn/python/idle_intro/index.html 에서 찾을 수 있습니다.

5.3. 추가 파이썬 패키지 설치하기

추가 파이썬 패키지를 설치하는 몇 가지 방법이 있습니다:

  • 패키지는 표준 파이썬 distutils 모드를 통해 설치할 수 있습니다 (python setup.py install).

  • 많은 패키지는 setuptools 확장이나 pip 래퍼를 통해 설치할 수도 있습니다, https://pip.pypa.io/ 를 참조하십시오.

5.4. Mac에서의 GUI 프로그래밍

Mac에서 파이썬으로 GUI 응용 프로그램을 작성하기 위한 몇 가지 옵션이 있습니다.

PyObjC는 애플의 Objective-C/Cocoa 프레임워크에 대한 파이썬 바인딩입니다. 이 프레임워크는 가장 현대적인 Mac 개발의 기초입니다. PyObjC에 대한 정보는 https://pypi.org/project/pyobjc/ 에서 얻을 수 있습니다.

표준 파이썬 GUI 툴킷은 크로스 플랫폼 Tk 툴킷(https://www.tcl.tk)을 기반으로 하는 tkinter입니다. Tk의 Aqua-네이티브 버전은 애플에 의해 OS X에 번들로 제공되며, 최신 버전은 https://www.activestate.com 에서 내려받아 설치할 수 있습니다; 소스에서 빌드할 수도 있습니다.

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.

5.5. 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/.

5.6. 기타 자원

MacPython 메일링리스트는 Mac 파이썬 사용자와 개발자를 위한 훌륭한 지원 자원입니다:

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

또 다른 유용한 자원은 MacPython 위키입니다:

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