Skip to content

Latest commit

 

History

History
225 lines (124 loc) · 8.39 KB

File metadata and controls

225 lines (124 loc) · 8.39 KB

Installing Python on Windows Using Command Prompt

Document Version: 1.0
Document Type: Software Installation Guide
Target Audience: Beginners, Students, and Aspiring Python Developers
Operating System: Windows 10 and Windows 11
Installation Method: Windows Command Prompt (CMD) using WinGet

1. Introduction

Python is a widely used, high-level programming language known for its simple syntax and versatility. It is commonly used in web development, automation, data analysis, artificial intelligence, and software development.

This guide explains how to install Python on Windows using Windows Command Prompt (CMD). It uses Windows Package Manager (WinGet) to install the Python install manager, followed by the installation of a Python runtime.

By following this guide, users will learn how to install Python, verify the installation, execute their first Python command, and create a basic Python program.

2. Prerequisites

Before starting the installation, ensure that the following requirements are met:

  • A computer running Windows 10 or Windows 11.
  • An active internet connection.
  • Windows Package Manager (WinGet) installed and accessible.
  • Access to Windows Command Prompt.
  • Sufficient permissions to install applications on the computer.

Note: The commands in this guide are intended for Command Prompt. They can also be executed in Windows PowerShell, but this guide uses CMD throughout.

3. Checking WinGet Availability

WinGet is a command-line package manager for Windows. It allows users to search for, install, upgrade, and manage software applications from supported package sources.

Before installing Python, verify that WinGet is available.

Step 1: Press Windows + R on your keyboard.

Step 2: Type cmd in the Run dialog box and press Enter.

Step 3: Execute the following command:

winget --version

Step 4: Check the command output.

If WinGet is installed, Command Prompt displays its version number.

If Windows displays an error stating that 'winget' is not recognized, WinGet may not be installed or available in the system PATH. Update or install the App Installer application through Microsoft Store, then reopen Command Prompt and try again.

4. Installing the Python Install Manager

The Python install manager provides commands for installing and managing Python runtimes on Windows.

Step 1: Open Command Prompt.

Step 2: Execute the following command:

winget install 9NQ7512CXL7T -e --accept-package-agreements --disable-interactivity

Step 3: Allow WinGet to download and install the Python install manager.

Wait for the installation process to finish.

Step 4: Close Command Prompt and open a new Command Prompt window.

This refreshes the terminal session so that Windows can recognise the newly installed commands.

5. Installing the Python Runtime

Installing the Python install manager does not necessarily mean that a Python runtime has already been installed.

Use the following command to install the default Python runtime.

Step 1: Execute:

py install default

The Python install manager downloads and installs the current default stable Python runtime.

Step 2: Wait until the installation process completes.

Step 3: Check the installed Python runtimes:

py list

The command displays the Python runtimes detected by the install manager.

6. Verifying the Python Installation

After installing Python, verify that the command-line tools are working correctly.

Step 1: Check the Python version:

python --version

Expected output format:

Python 3.x.x

The exact version number depends on the runtime installed.

Step 2: Verify the Python launcher:

py --version

Step 3: Check whether pip is available:

python -m pip --version

Pip is Python's package installation tool. It allows users to install and manage additional Python libraries.

If these commands return version information without errors, the installation is working.

7. Running Your First Python Command

After confirming the installation, execute a simple Python command.

Enter the following command in Command Prompt:

python -c "print('Hello, Python!')"

Expected output:

Hello, Python!

This command uses Python to execute a single instruction without creating a separate program file.

8. Creating and Running Your First Python File

You can also create and execute a basic Python script.

Step 1: Create a directory for your Python practice files:

mkdir "%USERPROFILE%\Documents\PythonPractice"

Step 2: Navigate to the newly created directory:

cd /d "%USERPROFILE%\Documents\PythonPractice"

Step 3: Open Notepad to create a Python file:

notepad hello.py

If Windows asks whether you want to create a new file, select Yes.

Step 4: Enter the following Python code:

print ("Hello, World!")

print ("Python installation completed successfully.")

Step 5: Save the file as hello.py and close Notepad.

Step 6: Run the Python script:

python hello.py

Expected output:

Hello, World!

Python installation completed successfully.

You have successfully created and executed your first Python program.

9. Common Installation Issues and Troubleshooting

The following table describes some common problems users may encounter during installation.

Problem Possible Cause Recommended Solution
'winget' is not recognized WinGet is unavailable or App Installer is outdated. Install or update App Installer and reopen CMD.
'python' is not recognized The Python command alias or PATH configuration is unavailable. Reopen CMD, check py --version, and review Windows app execution aliases.
'py' is not recognized The Python install manager is not accessible. Check the installation and command availability, then reinstall or repair the install manager if necessary.
Python runtime is missing The install manager is installed, but no runtime is available. Execute py install default.
'pip' is not recognized The pip executable is unavailable in PATH. Use python -m pip instead of calling pip directly.

Additional troubleshooting tip:

If the Python commands remain unavailable after installation, run the following command:

py install --configure -y

This runs the install manager's configuration process. Review the output for any additional configuration requirements.

For further assistance, consult the official Python Windows troubleshooting documentation.

10. Best Practices

Follow these recommendations when working with Python on Windows:

  • Keep Python and its installation tools updated.
  • Use python -m pip to manage Python packages.
  • Create a separate virtual environment for each development project.
  • Avoid installing unnecessary packages globally.
  • Verify the Python version before running scripts.
  • Store project files in clearly organised directories.

To create a virtual environment, execute:

python -m venv .venv

To activate it in Command Prompt:

.venv\Scripts\activate.bat

To deactivate the environment:

deactivate

Virtual environments help keep project dependencies separate.

11. Conclusion

Python can be installed and configured on Windows without manually downloading and running a traditional standalone installer.

By using WinGet and the Python install manager, users can install a Python runtime, verify the installation, execute basic commands, and create their first Python program.

Following this guide provides a starting point for learning Python and building software development projects.

12. References

  1. Python Software Foundation. https://docs.python.org/3/using/windows.html
  2. Python Software Foundation. https://www.python.org/downloads/
  3. Microsoft Learn. https://learn.microsoft.com/en-us/windows/dev-environment/python?tabs=winget
  4. Python Software Foundation. https://docs.python.org/3/library/venv.html