杰瑞科技汇

Python Mac IDLE里pip命令怎么用?

Of course! This is a very common question for new Python users on macOS. Let's break it down into simple, understandable steps.

Python Mac IDLE里pip命令怎么用?-图1
(图片来源网络,侵删)

The core concepts you need to understand are:

  1. Python: The programming language itself.
  2. IDLE: The basic, simple editor that comes with Python. It's great for beginners but not for serious development.
  3. pip: The package manager for Python. It's a tool used to install and manage "packages" (which are libraries or modules of code written by others).

Here is a complete guide on how to handle these three on your Mac.


Part 1: Understanding Python on macOS

Modern macOS (from Catalina 10.15 onwards) comes with Python 3 pre-installed. This is great because you don't need to download it from python.org.

How to check if you have Python 3 and where it is:

Python Mac IDLE里pip命令怎么用?-图2
(图片来源网络,侵删)

Open the Terminal app (you can find it in Applications/Utilities or search for it with Spotlight Cmd + Space).

Type the following command and press Enter:

python3 --version

You should see something like Python 3.x.x (e.g., Python 3.9.6). This confirms you have a system version of Python 3.

Important: On the Mac, python (without the 3) might point to an older, system-managed Python 2 (which is deprecated). It's a best practice to always use python3 to be explicit.

Python Mac IDLE里pip命令怎么用?-图3
(图片来源网络,侵删)

Part 2: Using IDLE

IDLE is the simple graphical interface that comes with Python. It lets you write and run code in a single window.

How to launch IDLE:

  1. Open the Terminal app.

  2. Type the following command and press Enter:

    python3 -m idlelib
    • This command tells Python 3 to run the idlelib module, which starts the IDLE graphical interface.

You will see a window titled "Python 3.x.x Shell" appear. This is the IDLE interactive interpreter. You can type Python code directly into it and press Enter to see the results.

To create a new file:

  • Inside the IDLE shell, go to the menu bar and click File -> New File.
  • A new, empty window will open. This is your text editor.
  • Write your code (e.g., print("Hello, World!")).
  • Save the file (e.g., hello.py).
  • Run it by clicking Run -> Run Module (or pressing F5).

A Note on IDLE: While IDLE is functional, most professional developers use more advanced code editors like Visual Studio Code (VS Code), PyCharm, or Sublime Text. These offer features like syntax highlighting, auto-completion, and debugging tools that make coding much easier.


Part 3: Using pip to Install Packages

pip is the tool you'll use most often. It downloads and installs packages from the Python Package Index (PyPI), which is a huge repository of community-built Python code.

How to check if pip is installed:

In the Terminal, type:

pip3 --version

If you see a version number, you're good to go. If you get an error like pip3: command not found, you may need to install it. On modern macOS, it usually comes with Python, but if not, you can install it with:

python3 -m ensurepip --default-pip

How to use pip to install a package:

Let's install a popular package called requests, which is used for making web requests.

  1. Open the Terminal.

  2. Type the following command and press Enter:

    pip3 install requests

You will see pip download and install the package and its dependencies. That's it! The package is now available for you to use in your Python scripts.

How to use a package in your code:

  1. Create a new Python file using IDLE or another editor (e.g., test_requests.py).

  2. Add the following code:

    import requests
    # Make a GET request to a public API
    response = requests.get('https://api.github.com')
    # Print the status code of the response
    print(f"Status Code: {response.status_code}")
    # Print the first 100 characters of the response content
    print(f"Response content: {response.text[:100]}...")
  3. Run the file from your Terminal:

    python3 test_requests.py

You should see output indicating that you successfully connected to the GitHub API.


Part 4: The Modern Alternative: venv and pipenv

For any real project, you should never install packages globally using pip3 install <package>. This can lead to version conflicts.

The standard practice is to create a virtual environment for each project. A virtual environment is an isolated space where you can install packages for that specific project without affecting your system's global Python installation.

Method 1: Using the built-in venv (Recommended for beginners)

  1. Create a project folder:

    mkdir my_project
    cd my_project
  2. Create the virtual environment:

    python3 -m venv venv
    • This creates a new folder named venv inside your project.
  3. Activate the environment:

    source venv/bin/activate
    • Your Terminal prompt will change to show (venv) at the beginning. This means the environment is active.
    • Now, when you use pip, it will automatically use the pip inside your venv, not the system one.
  4. Install packages inside the environment:

    pip install requests
  5. Deactivate the environment when you're done:

    deactivate

    Your prompt will return to normal.

Method 2: Using pipenv (Easier and more powerful)

pipenv combines pip and venv into one easy-to-use tool and manages dependencies automatically.

  1. Install pipenv (globally, this is one of the few times it's okay):

    pip3 install pipenv
  2. Create a project folder and navigate into it:

    mkdir my_pipenv_project
    cd my_pipenv_project
  3. Install a package using pipenv:

    pipenv install requests
    • pipenv will automatically create and activate a virtual environment for you.
    • It will also create a Pipfile and a Pipfile.lock to track your project's dependencies.
  4. To work in the environment, just run:

    pipenv shell
  5. To exit the environment, type exit or close the terminal.


Summary and Best Practices

Tool What it is How to use it on macOS
Python The programming language. Use python3 in the Terminal. Your Mac comes with it.
IDLE A simple, built-in code editor. Launch from Terminal with python3 -m idlelib. Good for absolute beginners, but consider VS Code for serious work.
pip The package manager for Python. Use pip3 install <package_name> in the Terminal. Always use it inside a virtual environment for projects.
venv A tool to create isolated project environments. python3 -m venv venv, then source venv/bin/activate. The standard, built-in way.
pipenv A modern tool that combines pip and venv. pipenv install <package_name>. Easier dependency management. Highly recommended.

Key Takeaway: For learning, python3 and IDLE are fine. For any real project, use pipenv or venv to manage your packages and keep your system clean.

分享:
扫描分享到社交APP
上一篇
下一篇