杰瑞科技汇

如何在Windows系统安装Python3?

Of course! Here is a comprehensive guide to getting started with Python 3 on Windows, broken down into easy-to-follow steps.

如何在Windows系统安装Python3?-图1
(图片来源网络,侵删)

Part 1: Installing Python 3 on Windows

This is the most crucial step. The official installer is the best and safest way to get Python.

Step 1: Download the Python Installer

  1. Go to the official Python website: https://www.python.org/downloads/windows/
  2. The page will automatically detect your Windows version (32-bit or 64-bit) and suggest the latest stable release. Click the "Download Python X.X.X" button.

Step 2: Run the Installer and Crucial Settings

  1. Open the downloaded .exe file. You will see the Python installer window.

  2. CHECK THE BOX THAT SAYS "Add Python to PATH". This is the most important step. It allows you to run Python from the Command Prompt without having to type the full file path. If you miss this, you'll have a much harder time later.

  3. Click "Install Now". This will install Python with default settings, which is perfect for beginners.

    如何在Windows系统安装Python3?-图2
    (图片来源网络,侵删)
  4. Wait for the installation to complete. It should take less than a minute.

  5. Once done, check the box "Disable path length limit" and click "Close". This prevents issues with long file paths in modern Windows versions.

Step 3: Verify the Installation

  1. Open the Command Prompt. You can do this by pressing the Windows Key, typing cmd, and pressing Enter.

  2. In the Command Prompt, type the following commands and press Enter after each:

    如何在Windows系统安装Python3?-图3
    (图片来源网络,侵删)
    python --version

    You should see the installed Python version, for example: Python 3.12.0

    pip --version

    pip is Python's package installer and comes with Python. You should see its version number.

If both commands work without errors, your Python installation is successful!


Part 2: Writing and Running Your First Python Script

There are two main ways to write Python code: the Interactive Interpreter and a Script File.

Method 1: Using the Interactive Interpreter (IDLE)

The interactive interpreter lets you run Python code line by line.

  1. Open the Start Menu.

  2. Search for "IDLE" and open it. You'll see a window titled "Python 3.x Shell".

  3. You can type Python commands directly and press Enter to see the result.

    >>> print("Hello, Windows!")
    Hello, Windows!
    >>> 2 + 2
    4
    >>> "Python is fun"
    'Python is fun'
  4. To exit the interpreter, type exit() or press Ctrl+D.

Method 2: Creating and Running a Python Script File (Recommended)

This is the standard way to write programs.

  1. Create the File:

    • Open any simple text editor like Notepad.
    • Type the following code:
      # This is my first Python script
      print("Hello from a script file!")
      print("I am learning Python on Windows.")
    • Save the file. Important: Change the "Save as type" to "All Files" and name the file with a .py extension, for example, hello.py. If you don't, it might save as hello.py.txt.
  2. Run the Script from Command Prompt:

    • Open the Command Prompt.
    • Navigate to the directory where you saved your file. For example, if you saved it on your Desktop:
      cd C:\Users\YourUsername\Desktop

      (Replace YourUsername with your actual Windows username).

    • Run the script by typing python followed by the filename:
      python hello.py
    • You should see the output in the Command Prompt:
      Hello from a script file!
      I am learning Python on Windows.

Part 3: Choosing a Code Editor (Highly Recommended)

While Notepad works, a proper code editor will make your life much easier. They provide features like syntax highlighting (coloring code), auto-completion, and error checking.

Here are the top free choices for Windows:

Visual Studio Code (VS Code) - Top Recommendation

VS Code is a free, powerful, and extremely popular editor from Microsoft.

  • How to Install:
    1. Go to https://code.visualstudio.com/
    2. Click the "Download for Windows" button and run the installer.
  • How to Set it up for Python:
    1. Open VS Code.
    2. Go to the Extensions tab (the icon with four squares on the left sidebar).
    3. Search for "Python" by Microsoft and click "Install". This is the official extension that provides all the great features.
    4. Now you can open your folder (e.g., your Desktop), create a .py file, and start coding with full support.

PyCharm Community Edition

PyCharm is a dedicated Python IDE (Integrated Development Environment). It's more powerful than VS Code for pure Python development.

IDLE

IDLE also comes with Python and has its own built-in editor. It's simpler than VS Code or PyCharm, making it a good starting point for absolute beginners.


Part 4: Managing Packages with pip

Almost everything useful in Python comes from packages (libraries). You install them using pip.

  1. Install a Package: Let's install the requests library, which is used for making web requests.

    • Open the Command Prompt.
    • Type the following command:
      pip install requests
    • pip will download and install the package and its dependencies.
  2. Use the Package in a Script:

    • Create a new file named test_requests.py.

    • Add the following code:

      import requests
      # Make a request to a public API
      response = requests.get('https://api.github.com')
      # Check if the request was successful (status code 200)
      if response.status_code == 200:
          print("Successfully fetched data from GitHub!")
          print(f"Response headers: {response.headers['Server']}")
      else:
          print(f"Error: Failed to fetch data. Status code: {response.status_code}")
    • Save the file and run it from the Command Prompt: python test_requests.py.

  3. Update a Package:

    pip install --upgrade requests
  4. Uninstall a Package:

    pip uninstall requests
  5. List Installed Packages:

    pip list

Part 5: Best Practices for Windows Users

  • Virtual Environments: For different projects, it's best practice to use virtual environments. This isolates the packages for each project, preventing conflicts.
    • To create one: python -m venv my_project_env
    • To activate it: my_project_env\Scripts\activate
    • To deactivate it: deactivate
  • Use or \\ for Paths: In Python, you can use forward slashes () in file paths, which often works across all operating systems. For Windows-specific paths, you can use double backslashes (\\).
    • Good: file_path = "C:/Users/YourUsername/Desktop/data.txt"
    • Also Good: file_path = "C:\\Users\\YourUsername\\Desktop\\data.txt"
  • Watch for python vs. python3: On Linux and macOS, you often need to type python3 to ensure you're using Python 3. On Windows, the installer sets up python to point to Python 3 by default, so you just use python.

Troubleshooting

  • Problem: 'python' is not recognized as an internal or external command...
    • Solution: This means the "Add Python to PATH" step was missed. You have two options:
      1. Re-run the installer and make sure to check the "Add Python to PATH" box.
      2. Manually add it: Find your Python installation path (e.g., C:\Users\YourName\AppData\Local\Programs\Python\Python312\) and add it to your system's PATH environment variable. (Search for "Edit the system environment variables" in the Start Menu).
  • Problem: My .py file opens in Notepad or another editor when I double-click it.
    • Solution: Right-click the file -> "Open with" -> "Choose another app". Select "Python" from the list. If it's not there, click "Look for another app on this PC" and navigate to your Python installation's python.exe file (e.g., C:\...\Python312\python.exe). Check the box "Always use this app to open .py files".
分享:
扫描分享到社交APP
上一篇
下一篇