Introduction
For many developers, being able to run Python scripts directly from the Command Prompt is an essential skill. It enables seamless execution of scripts, testing, and integration into automated workflows. This tutorial will guide you through configuring your Windows 7 environment to recognize and execute Python programs from the Command Prompt.
Prerequisites
- Ensure that Python is installed on your system.
- Basic familiarity with using the Command Prompt.
Step-by-step Configuration
1. Locate Your Python Installation Path
When Python is installed, it typically resides in a directory like C:\Python27
or another version-specific folder. Verify this path by checking the installation directory during setup.
2. Add Python to the System PATH
The PATH environment variable is crucial for running executables from any command line without specifying their full paths. Here’s how you can add your Python executable to it:
-
Open System Properties:
- Right-click on "Computer" in the Start Menu and select "Properties".
- Click on "Advanced system settings".
-
Environment Variables:
- In the System Properties dialog, click the "Environment Variables…" button.
-
Edit PATH Variable:
- Under "System variables", find and select the "Path" variable.
- Click "Edit…".
- Append
;C:\Python27
to the end of the existing value (replace with your Python directory if different). The semicolon (;
) acts as a delimiter.
-
Apply Changes:
- Click "OK" on all open dialog boxes to save changes.
- Close and reopen any Command Prompt windows for the changes to take effect.
3. Verify Your Configuration
After modifying the PATH, verify your setup by opening a new Command Prompt window and typing:
python --version
If correctly configured, this should display the Python version installed on your system. Alternatively, you can start the interactive Python shell by simply typing python
:
C:\> python
Python 3.x.x (default, Month Day Year, Time) [MSC v.xxx] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Running a Python Script
To run a script from the Command Prompt:
-
Navigate to the directory containing your Python file using the
cd
command:C:\> cd path\to\your\script
-
Execute the script by typing:
C:\path\to\your\script> python your_script.py
Advanced: Using Scripts and Batch Files
For frequently used scripts, consider creating a batch file to streamline execution:
-
Create a
.bat
file with content like this:@echo off python "C:\path\to\your\script\your_script.py" pause
-
Double-click the batch file or run it from the Command Prompt to execute your script.
Additional Tips
-
File Associations: Ensure
.py
files are associated with Python by right-clicking a.py
file, selecting "Open With", choosing Python, and checking "Always use this app to open .py files". -
Using Shebang Lines: For scripts meant to be executed directly, include a shebang line at the top of your script:
#!/usr/bin/env python3
This specifies which interpreter should run the script.
Conclusion
By following these steps, you can efficiently set up and use Python from the Command Prompt on Windows 7. This setup not only aids in running scripts but also facilitates integrating Python into larger development environments and workflows.