杰瑞科技汇

mac python launcher

Of course! The "Python Launcher" on macOS is a built-in, powerful, and often underutilized tool. It's essential for managing multiple Python versions and environments.

mac python launcher-图1
(图片来源网络,侵删)

Here’s a complete guide covering what it is, how to find it, how to use it, and its common use cases.


What is the Python Launcher?

The Python Launcher is a system utility that comes pre-installed with macOS. Its primary job is to automatically find and run the correct version of Python based on your instructions.

Key features:

  • Manages Multiple Versions: It can distinguish between Python 2, Python 3, Homebrew-installed Pythons, and system Pythons.
  • Shebang Line Support: It's the engine that allows you to run Python scripts directly from the terminal (e.g., ./my_script.py).
  • GUI Integration: It's what lets you double-click a .py file and choose which Python version to run it with.
  • Virtual Environment Awareness: It can be configured to run scripts within a specific virtual environment.

How to Find and Use It

A. From the Command Line (Most Common)

The launcher is invoked using the command python or python3. You don't run the pythonlauncher executable directly in most cases.

mac python launcher-图2
(图片来源网络,侵删)

Finding Available Versions: First, see which Python versions the launcher knows about.

# List all major Python versions the launcher can find
/usr/bin/python3 --list
# Example output:
# 3.10
# 3.9
# 3.8
# 2.7

Specifying a Version: You can explicitly tell the launcher which version to use.

# Use the latest Python 3 version
python3 my_script.py
# Use a specific minor version, e.g., Python 3.9
python3.9 my_script.py
# Use Python 2 (if installed)
python my_script.py

Using the -v Flag for Verbosity: This is extremely useful for debugging. It shows you exactly which Python executable the launcher is choosing and why.

python3 -v my_script.py

Example Output of -v:

mac python launcher-图3
(图片来源网络,侵删)
Using Python version 3.10 (found in /Library/Frameworks/Python.framework/Versions/3.10/bin/python3)
...
(Your script's output here)

This tells you it's using Python 3.10 located in your Homebrew directory.


B. From the GUI (Double-Clicking a Script)

This is where the launcher's user-friendly interface shines.

  1. Find a Python script (e.g., hello.py) in Finder.
  2. Right-click (or Control+Click) the file.
  3. Select "Get Info".
  4. In the "General" section, find the "Open with:" section.
  5. Click the dropdown menu. You will likely see "PythonLauncher" or "Python" as an option.
  6. If you don't see the version you want, click "Other...".
  7. Navigate to your Python executable. Common locations are:
    • System Python: /usr/bin/python3
    • Homebrew Python: /usr/local/bin/python3 (on Intel Macs) or /opt/homebrew/bin/python3 (on Apple Silicon Macs)
    • Pyenv Python: ~/.pyenv/versions/3.11.4/bin/python3.11
  8. Select the desired Python executable and check the box that says "Open with".
  9. Click "Open".

Now, when you double-click the hello.py file, it will run with the Python version you just selected.


Common Use Cases & Scenarios

Use Case 1: You Have Python 3.8 and 3.10 (from Homebrew)

You want to run a script that requires a specific library only available for 3.10.

# This will use the default Python 3 (likely the newest one)
pip install some-new-library # Installs into the default Python 3
# To run your script with Python 3.8, you must be explicit
python3.8 my_legacy_app.py

Use Case 2: You Use pyenv for Version Management

pyenv is a tool to install and switch between multiple Python versions. The Python Launcher works seamlessly with it.

  1. You've installed Python 3.11 with pyenv install 3.11.4.
  2. You've set it as your local version with pyenv local 3.11.4.

When you run python3 my_script.py in that directory, the launcher (influenced by pyenv's shell hooks) will automatically find and use the 11.4 version.

Use Case 3: Running a Script with a Shebang Line

This is a best practice for making your script self-contained. The first line of your script tells the system which interpreter to use.

my_project.py:

#!/usr/bin/env python3
import sys
print(f"Hello from Python {sys.version_info.major}.{sys.version_info.minor}")

Now you can make it executable and run it directly:

# Make the file executable
chmod +x my_project.py
# Run it directly. The OS uses the shebang line to call the Python Launcher.
./my_project.py

The #!/usr/bin/env python3 line is robust because it uses the system's PATH to find python3, allowing the launcher to do its job.


Configuration File (~/Library/Preferences/org.python.python.plist)

The launcher's behavior can be customized. It reads a configuration file located at ~/Library/Preferences/org.python.python.plist. You can edit this file with a property list editor (like the free "PlistEdit Pro" or Xcode) or by using the command line.

Common settings include:

  • Default Python Version: You can set which version of Python 3 is considered the "default" when you just type python3.
  • Virtual Environment Handling: You can configure it to automatically detect and use a venv or virtualenv if one is active in the terminal.

Summary Table

Method Command / Action When to Use It
Command Line (Explicit) python3.9 script.py When you need a specific, non-default Python version.
Command Line (Default) python3 script.py When you want the system's default Python 3.
Command Line (Verbose) python3 -v script.py For debugging to see which Python executable is being used.
GUI (Info Panel) Right-click script.py -> Get Info -> Open with... For one-off runs or setting a default GUI handler for a specific script.
Shebang Line #!/usr/bin/env python3 at the top of a script. For making scripts portable and executable directly from the terminal.
分享:
扫描分享到社交APP
上一篇
下一篇