杰瑞科技汇

winsows python cmd

Of course! The Windows Command Prompt (cmd.exe) is a fundamental tool for running Python scripts and managing your Python environment. Here’s a comprehensive guide covering everything from basic checks to advanced usage.

winsows python cmd-图1
(图片来源网络,侵删)

Verifying Your Python Installation

Before you can run Python, you need to make sure it's installed and that Windows knows where to find it.

Step 1: Check if Python is Installed

Open the Command Prompt. You can do this by:

  • Pressing the Windows Key, typing cmd, and pressing Enter.
  • Pressing Win + R, typing cmd, and pressing Enter.

Once the black command window is open, type the following command and press Enter:

python --version

or the more modern py command (which is recommended as it's version-agnostic):

winsows python cmd-图2
(图片来源网络,侵删)
py --version

Possible Outcomes:

  • ✅ Success: You'll see a version number, like Python 3.11.4.

    If this works, skip to the next section.

  • ❌ Error 'python' is not recognized... or 'py' is not recognized...: This means Python is either not installed or its location is not in your system's PATH.

Step 2: Add Python to Your PATH (If Needed)

The "PATH" is an environment variable that tells Windows where to look for executable files.

How to Fix It:

winsows python cmd-图3
(图片来源网络,侵删)
  1. Install/Re-install Python correctly: The easiest way is to download the latest Python from python.org. During installation, make sure to check the box that says:

    Add Python to PATH

  2. If Python is already installed: You'll need to manually add it.

    • Find your Python installation path. It's usually something like C:\Users\YourUsername\AppData\Local\Programs\Python\Python311 or C:\Python311.
    • Open the Start Menu, type "Environment Variables", and select "Edit the system environment variables".
    • In the System Properties window, click the "Environment Variables..." button.
    • In the "System variables" section (the bottom half), find and select the Path variable, then click "Edit...".
    • Click "New" and paste your Python installation path (e.g., C:\Python311).
    • Also, click "New" and paste the path to your Python Scripts folder (e.g., C:\Python311\Scripts). This is crucial for tools like pip.
    • Click OK on all windows to save.
    • Close and re-open your Command Prompt for the changes to take effect.

Running Python Code

There are two main ways to run Python code from the command prompt.

Method 1: Interactive Mode (The REPL)

This is great for quick tests, experimenting, and learning. You type a command, and Python executes it immediately.

  1. Open cmd.

  2. Type python or py and press Enter. You'll see the Python >>> prompt.

    C:\Users\YourName> py
    Python 3.11.4 (main, Jun  7 2025, 12:45:35) [MSC v.1934 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
  3. Now you can type Python code directly.

    >>> print("Hello from Python!")
    Hello from Python!
    >>> 2 + 2
    4
    >>> my_name = "Alice"
    >>> print(f"Hello, {my_name}!")
    Hello, Alice!
  4. To exit the interactive mode, type exit() or press Ctrl + Z followed by Enter.

Method 2: Running a Script File (Most Common)

This is how you run your actual Python programs (.py files).

  1. Create a Python file:

    • Open a text editor (like Notepad, VS Code, or Sublime Text).

    • Type some code:

      # hello.py
      import time
      import sys
      print("Hello, World!")
      print("Script name:", sys.argv[0])
      for i in range(5):
          print(f"Counting: {i}")
          time.sleep(1)
      print("Done!")
    • Save the file as hello.py on your Desktop for easy access.

  2. Navigate to the file's location in cmd:

    • In your Command Prompt, you need to go to the directory where you saved the file. Use the cd (change directory) command.
    • If you saved it on your Desktop, your command might look like this (replace YourUsername with your actual username):
      cd C:\Users\YourUsername\Desktop
    • Pro Tip: You can type dir and press Enter to list the files in the current directory. You should see hello.py in the list to confirm you are in the right place.
  3. Run the script:

    • Now, type python or py, followed by the name of your file, and press Enter.
      C:\Users\YourUsername\Desktop> python hello.py
    • Output:
      Hello, World!
      Script name: hello.py
      Counting: 0
      Counting: 1
      Counting: 2
      Counting: 3
      Counting: 4
      Done!

Essential pip Commands (Installing Packages)

pip is Python's package installer. You'll use it all the time. The py command is often better than python here to avoid version conflicts.

  • Install a package:

    # Install the 'requests' library
    py -m pip install requests
  • Upgrade a package:

    # Upgrade 'pip' itself
    py -m pip install --upgrade pip
  • Uninstall a package:

    py -m pip uninstall requests
  • List installed packages:

    py -m pip list
  • Show information about a package:

    py -m pip show requests

Useful Command Prompt Tips for Python

  • Clear the Screen: If your terminal is getting messy, type cls and press Enter to clear it.
  • Auto-completion: Press the Tab key to auto-complete file or directory names. For example, if you type py hello.py and then press Tab, it might complete to python hello.py.
  • Get Help: Type py --help or python --help to see a list of command-line options for the Python interpreter. For example, you can use python -c "print('hello')" to run a single line of code from the command line without a script file.

Modern Alternative: PowerShell

Windows also comes with PowerShell, which is more powerful and modern than the traditional cmd. Most Python commands work identically, but there are a few differences in syntax.

  • How to open: Search for "PowerShell" in the Start Menu.
  • Basic Commands:
    • Get current location: Get-Location (alias: pwd)
    • List items: Get-ChildItem (alias: ls)
    • Change directory: Set-Location -Path "C:\Path\To\Dir" (alias: cd)
    • Run Python: python .\hello.py or py .\hello.py (the .\ is good practice in PowerShell).

For most day-to-day Python work, cmd is perfectly fine, but knowing PowerShell is a valuable skill.

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