How to Run Python from Windows CMD
If you are new to programming or just starting with Python, you might be wondering how to run Python code on your Windows computer using the Command Prompt (CMD). In this article, we will guide you through the process of running Python from Windows CMD step by step.
Step 1: Install Python
The first step is to install Python on your Windows computer. Python is a popular programming language, and it can be easily downloaded and installed from the official Python website. Follow these steps to install Python:
- Open your web browser and go to the Python Downloads page.
- Choose the latest version of Python for Windows. Make sure to select the correct installer based on your system architecture (32-bit or 64-bit).
- Click on the download link to start the download.
- Once the download is complete, run the installer.
- On the installation wizard, make sure to check the box that says "Add Python to PATH". This will enable you to run Python from the Command Prompt.
- Follow the instructions on the wizard to complete the installation.
Step 2: Open the Command Prompt
After installing Python, you need to open the Command Prompt to run Python code. Here's how you can open the Command Prompt:
- Press the Windows key on your keyboard or click on the Start button.
- Type "cmd" in the search bar and press Enter.
- The Command Prompt window will open.
Step 3: Verify Python Installation
Before running Python code, it's important to verify that Python is installed correctly. Follow these steps to verify the installation:
- In the Command Prompt window, type
python --versionand press Enter. - If Python is installed correctly, it will display the version number. For example, "Python 3.9.2".
- If you see an error message or Python is not recognized, make sure you have added Python to the PATH during the installation process. You may need to reinstall Python and ensure the "Add Python to PATH" option is selected.
Step 4: Running Python Code
Now that Python is installed and verified, you can start running Python code from the Command Prompt. Here's how:
- In the Command Prompt window, type
pythonand press Enter. - The Python interpreter will start, and you will see the Python prompt, represented by
>>>. - You can now start typing Python code. For example, you can type
print("Hello, World!")and press Enter. - The Python interpreter will execute the code and display the output. In this case, it will print "Hello, World!" on the next line.
- To exit the Python interpreter, type
exit()or press Ctrl + Z followed by Enter.
Step 5: Running Python Scripts
While running Python code interactively in the Python interpreter is useful for testing and experimenting, you will often need to run Python scripts stored in files. Here's how you can run a Python script from the Command Prompt:
- Create a new text file using a text editor, such as Notepad.
- Type your Python code in the text file. For example, you can type
print("Hello, World!"). - Save the file with a
.pyextension. For example, you can save it ashello.py. - In the Command Prompt window, navigate to the directory where the Python script is saved. You can use the
cdcommand to change directories. - Type
python hello.pyand press Enter. Replacehello.pywith the name of your Python script. - The Python interpreter will execute the script, and you will see the output in the Command Prompt window.
Conclusion
Running Python from the Windows Command Prompt is a straightforward process. By following the steps outlined in this article, you can install Python, open the Command Prompt, verify the installation, and run Python code interactively or from scripts. Python is a versatile and powerful programming language, and being able to run it from the Command Prompt gives you greater control and flexibility in your programming journey.
References
| Reference | Link |
|---|---|
| Python Downloads | https://www.python.org/downloads/ |