How to Run a Python Script in Windows (2024)

If you want to learn Python, you’ll need to know how to run Python scripts or .py files in Windows. Find out what you need to do in this article!

When it comes to operating systems, there are three main ones for personal computers: Microsoft Windows, Apple’s macOS, and Linux. You can use any of these operating systems to develop and run your Python programs. However, there are subtle differences you need to keep in mind while developing your projects. In this article, we’ll outline how to run a Python script in Windows.

In general, we recommend you work with Python 3, which is the latest version. If you need some help getting Python installed, check out our article How to Install Python on Windows. To make sure you’re up to speed with the terminology we use in this article, take a look at Python Terms Beginners Should Know – Part 1.

If you’re new to programming, a good place to start is our Python Basics track, which contains three useful courses to teach you the fundamentals. We have more course suggestions in the article How to Learn Python Online for Free.

Your First Python Script

To follow along with this article, you’ll need to create your own Python script. This script doesn’t have to be fancy – a ‘Hello, World!’ program will do. Simply open a text editor (such as Notepad) and type:

print('Hello, World!')

Just make sure there aren’t any spaces before the print() statement. Then save it as ‘script.py’.

Run Python Code from the Command Prompt

The first way to work with Python in Windows is through an interactive session. To get an interactive session started, just open the Command Prompt. Search for cmd on the toolbar, then hit Enter. Once the command prompt is open, simply type python and hit Enter again.

When you’re in an interactive session, every Python statement is executed immediately and any output is displayed directly beneath. Try typing:

>>> print('Hello, World!')Hello, World!

From here, you can write any Python code you want. To exit the interactive session, type quit() or exit().

To run the script that we created above, you can call the Python program from the Command Prompt and tell it which file to execute. From the Command Prompt, simply type:

python script.py

You’ll see the Hello, World! output printed directly to the screen. For more details on this method, check out How to Open and Run Python Files in the Terminal.

This is the most basic method of executing scripts that you should be familiar with. You could develop your whole project in Notepad and run the script in this way. This is totally legit, but for many applications it’s not the easiest method.

Run Python Code from an IDE

A much more useful way to develop and run your Python projects is using an Integrated Development Environment (IDE). For some background reading, check out our article 4 Best Python IDE and Code Editors. My personal recommendation is to use Spyder (which has a nice mix of features and is user-friendly) but any IDE will do. You can download Spyder here.

Once you’ve downloaded and installed it, open Spyder to get started. Since you already have your script written, you just need to open it in the editor. To run the script, simply click the play button in the top toolbar. You should see the output displayed in the interactive console.

Using an IDE to develop and run your Python projects is the most convenient way to work.

Run a Python Script from the File Manager

The final way of running your Python script that we’ll discuss is double-clicking the .py file. This is possibly the least useful option, but it could be convenient once you have fully developed and tested your project. It could also require a little bit of configuration.

You need to ensure your .py file is associated with python.exe. Right click the .py file, select Open with > Python. Now try double-clicking the .py file. You’ll see the Command Prompt flash briefly, then close again. To avoid this, you can add the following line to the end of your script:

input('Press Enter to Continue')

Now the script will stop when you hit the Enter key.

Running Python Scripts in Windows: Next Steps

In this article, we discussed three ways to run your Python scripts in Windows. This is something you’ll need to do often to develop and test your programs.

Learning anything new can be challenging. For some tips on the best ways to learn Python, check out our article 5 Tips for Learning Python from Scratch. With a little bit of consistent effort, you’ll become a Python master in no time!

How to Run a Python Script in Windows (2024)

FAQs

How do I run a Python file in Windows? ›

To run a Python file, type “Python File.py” where “File” is your file's name. For example, if your Python file is named “Script,” type “Python script.py” instead. Press enter to run the command and open the file.

How do I run a Python .py script? ›

Running Python Scripts involves utilising the Python interpreter to execute the code written in the script. To run Python Scripts, you can open a command prompt or terminal, navigate to the directory containing the script, and use the command "python script_name.py" (replace "script_name" with the actual filename).

How to run Python script in command prompt? ›

The most basic and easy way to run a Python script is by using the python command. You need to open a command line and type the word python followed by the path to your script file like this: python first_script.py Hello World! Then you hit the ENTER button from the keyboard, and that's it.

How do I compile a Python script in Windows? ›

Compile Python on Windows
  1. Step 1: Add Python to Windows Path. ...
  2. Step 2: Open a Windows Prompt. ...
  3. Step 3: Install the Nuitka Python Compiler package. ...
  4. Step 4: Run your Program. ...
  5. Step 5: Create the Executable using Nuitka. ...
  6. Step 6: Run the Executable.

Where can I run Python code? ›

Popular Online Platforms for Running Python Code
  • Link: Google Colab. Replit. ...
  • Link: Replit. PythonAnywhere. ...
  • Link: PythonAnywhere. OnlineGDB. ...
  • Link: OnlineGDB. IDEOne. ...
  • Link: IDEOne. Jupyter Notebooks. ...
  • Link: Jupyter Notebooks. Trinket. ...
  • Link: Trinket. Paiza.IO. ...
  • Link: Paiza.IO. CoCalc.
Jul 26, 2024

How do I run a Python environment in Windows? ›

WINDOWS
  1. Install Python. ...
  2. Add Python to PATH. ...
  3. Open a new CMD prompt (Windows Key + R, cmd.exe)
  4. Install virtualenv through the command pip install virtualenv. ...
  5. Check that virtualenv is installed through the command pip --version.
  6. Install virtualenvwrapper-win through the command pip install virtualenvwrapper-win.

How to use Python in Windows? ›

Go to your Start menu (lower left Windows icon), type "Microsoft Store", select the link to open the store. Once the store is open, select Search from the upper-right menu and enter "Python". Select which version of Python you would like to use from the results under Apps.

How do I make a .py script executable? ›

Ways to Convert Python Scripts to . Exe Files
  1. Step 1: Install PyInstaller. Open a command prompt or terminal and run the following command to install PyInstaller: pip install pyinstaller.
  2. Step 2: Navigate to your script's directory. ...
  3. Step 3: Run PyInstaller. ...
  4. Step 4: Locate the executable.
Sep 5, 2024

How do I run a .py file in Notepad? ›

Steps to Use Notepad to Run Python Program
  1. Click on the notepad icon on your PC and start typing the Python code you want to write. ...
  2. Save your Python script. ...
  3. Open Command Prompt (CMD).
  4. Navigate to the Script's Directory:
  5. Use the cd command to navigate to where you have stored your Python script by changing the directory.
Mar 13, 2024

How do I run a Python script from the command line path? ›

Just type"python <full path to the script>" and you'll see the "print" output appear below the command. Try that now for the script you just created. You should see the "Hello World" appear in the window. You've now run a Python script from the command line.

How to run code in Command Prompt? ›

How to run a file in command prompt
  1. Open command prompt. There are several ways to open the command prompt app. ...
  2. Open the file pathway. To open the correct file, direct the command prompt app to the correct file path in your Windows by using this command template: cd [file path]. ...
  3. Execute the file. ...
  4. Launch and use your file.
Jul 2, 2024

How do I make a Python script into a command? ›

Easy—there are three things you need to do:
  1. Step 1: Mark your Python file as executable. The first thing you'll need to do is mark your Python script as executable in the file system, like so: ...
  2. Step 2: Add an interpreter “shebang” ...
  3. Step 3: Make sure your program is on the PATH.

How do I run a Python script directly in Windows? ›

To run Python scripts with the python command, you need to open a command-line window and type in the word python followed by the path to your target script: Windows. Linux + macOS.

How to run .py file? ›

To run a Python script in Terminal from the command line, navigate to the script's directory and use the python script_name.py command. Redirecting output involves using the > symbol followed by a file name to capture the script's output in a file.

How do I run a Python script at startup in Windows? ›

Starting a Python file on Windows startup

You can go to the folder by pressing the Windows key (⊞) + R. Then a program called Run appears in which you type shell:startup and hit enter. This will open the Startup folder. You can then drag and drop or copy the Python file into the Startup folder.

How do I run a Python file as an executable? ›

Steps to Python Scripts to .Exe Files
  1. Step 1: Install PyInstaller. Type below command in the command prompt to create python to executable. ...
  2. Step 2: Navigate to Your Script's Directory. ...
  3. Step 3: Open PowerShell. ...
  4. Step 4: Run PyInstaller. ...
  5. Step 5: Locate the Executable. ...
  6. Step 6: Test the Executable.
Jul 26, 2024

How do I run a Python function from a file? ›

To use the functions written in one file inside another file include the import line, from filename import function_name . Note that although the file name must contain a . py extension, . py is not used as part of the filename during import.

How do I install a .py file in Windows? ›

Python Installation on Windows
  1. Step 1: Select Python Version. ...
  2. Step 2: Download Python Executable Installer. ...
  3. Step 3: Run Executable Installer. ...
  4. Step 4: Add Python to Path (Optional) ...
  5. Step 5: Verify Python Was Installed on Windows. ...
  6. Step 6: Verify PIP Was Installed. ...
  7. Step 7: Install virtualenv (Optional)
Dec 5, 2023

How do I run Python in Windows Notepad? ›

Steps to Use Notepad to Run Python Program
  1. Click on the notepad icon on your PC and start typing the Python code you want to write. ...
  2. Save your Python script. ...
  3. Open Command Prompt (CMD).
  4. Navigate to the Script's Directory:
  5. Use the cd command to navigate to where you have stored your Python script by changing the directory.
Mar 13, 2024

Top Articles
Używanie ciemnego motywu w aplikacjach - Google Play
What is EMV Bypass Cloning? Are Chip Cards Still Secure?
Johnjamesopp
K2 Spice Diablo
Uvalde Topic
Lpga Scores Espn
Mujeres Prepago Puerto Rico
Herbalism Guide Tbc
Costco Gas Prices Lansing
Clinton County Correctional Facility Housing Report
Funeral Homes in Grand Forks, North Dakota
Www.metaquest/Device Code
Authentic leadership: seeking out information from diverse sources | Brian Hovey posted on the topic | LinkedIn
Jail Roster Independence Ks
Memphis Cars For Sale Craigslist
Ics 200 Answers
Keyc Weather Forecast
Julie Ibrance Commercial Actress
19 Dollar Fortnite Card Copypasta
Summit University Ceu 360
Reiseland Brandenburg: Ihr Portal für Urlaub und Ausflüge
24 Hour Drive Thru Car Wash Near Me
18 Best Referral Programs To Make Money (Up to $175)
Vcuapi
Amerideck Cost
Days Calculator 📅 (Days Between Dates)
T33N Leak 5 17 Invite
Blue Is The Warmest Color Free Online Watch
Larry A.k.a Lvrd Pharaoh
Sites Like Av.nyuu
Creating a Numerical Table from Two Functions (TABLE)
Cole's Dockside Pre Fixe Menu
Will Byers X Male Reader
Fortune House Southbury Menu
Mike Matusow Wheelchair
7023594890
What Is The Best Center Build In 2K23
World of Warships: Aslains Modpack - Alle Mods in einem Paket
H&R Block Review 2024: Pricing, Features, Ease of Use - NerdWallet
Craigslist Oneonta New York
Where To Cook In Gerudo Town
Fleet Feet Sports Johnson City Tn
Pestweb Login
Winston Salem Nc Craigslist
Www Craigslist Denver Com
Va Zillow
Shauna's Art Studio Laurel Mississippi
Tucker Gott Jaclyn Weiss
AELF — Office des lectures — 1 janvier 2022
What Time Is First Light Tomorrow Morning
Spiraling Spirit Leak
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 5395

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.