Run python programs on windows

How to Run a Python Script in Windows

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:

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:

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.

Читайте также:  Word wrap with css

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 Use Windows Command Prompt to Run a Python File

This article was co-authored by wikiHow staff writer, Jack Lloyd. Jack Lloyd is a Technology Writer and Editor for wikiHow. He has over two years of experience writing and editing technology-related articles. He is technology enthusiast and an English teacher.

Читайте также:  Найти среднее арифметическое чисел си шарп

The wikiHow Tech Team also followed the article’s instructions and verified that they work.

This article has been viewed 782,416 times.

Whether you’re writing Python code on your Windows PC or just want to use existing Python scripts, it’ll be helpful to learn how to run code from the Command Prompt. Running Python code is easy—you’ll just need to have Python installed. This wikiHow article will walk you through opening a Python file from Command Prompt, and teach you how to fix the common «python is not recognized as an internal or external command» error.

Finding the Python File Path

Image titled Use Windows Command Prompt to Run a Python File Step 1

Image titled Use Windows Command Prompt to Run a Python File Step 2

Image titled Use Windows Command Prompt to Run a Python File Step 3

Image titled Use Windows Command Prompt to Run a Python File Step 4

Image titled Use Windows Command Prompt to Run a Python File Step 5

  • You can copy the location by highlighting it (click and drag your mouse across the «Location» value) and then pressing Ctrl + C .

Running a Python File

Image titled Use Windows Command Prompt to Run a Python File Step 6

Windows Start

Image titled Use Windows Command Prompt to Run a Python File Step 7

Image titled Use Windows Command Prompt to Run a Python File Step 8

Windows cmd

Image titled Use Windows Command Prompt to Run a Python File Step 9

  • For example, to open a Python file in a folder named «Files» on your Desktop, you would enter cd desktop/Files here.
  • If you copied the path to the file, you can type in cd and a space and then press Ctrl + V to paste in the path.

Image titled Use Windows Command Prompt to Run a Python File Step 10

  • For example, if your Python file is named «script», you would type in python script.py here.
  • If your Python file has one or more spaces in its name, you’ll place quotation marks around the file name and extension (e.g., python «my script.py» ).

Image titled Use Windows Command Prompt to Run a Python File Step 11

  • If you encounter an error that says ‘python’ is not recognized as an internal or external command after pressing Enter, you’ll need to add Python to the PATH list before retrying this part.

Adding Python to the PATH List

Image titled Use Windows Command Prompt to Run a Python File Step 12

Windows File Explorer

Image titled Use Windows Command Prompt to Run a Python File Step 13

  • Click This PC on the left side of the File Explorer.
  • Double-click your hard drive in the «Devices and drives» section.
  • Scroll down and double-click the «Users» folder.
  • Double-click the folder with your username on it.
  • Scroll down and double-click «AppData».
  • Double-click «Local».
  • Scroll down and double-click «Programs».
  • Double-click the «Python» folder.
  • Double-click the Python folder with your preferred version number (e.g., «Python36»).

Image titled Use Windows Command Prompt to Run a Python File Step 14

Copy the path to the Python folder. Click once the address bar at the top of the File Explorer to highlight its contents, then press Ctrl + C to copy the highlighted address.

Image titled Use Windows Command Prompt to Run a Python File Step 15

Windows Start

Image titled Use Windows Command Prompt to Run a Python File Step 16

Image titled Use Windows Command Prompt to Run a Python File Step 17

Click System info . This is a link in the upper-right corner of the window. Doing so opens the System Information window.

Image titled Use Windows Command Prompt to Run a Python File Step 18

Click the Advanced system settings link. You’ll see this in the upper-left side of the System Information window. Yet another window will pop up.

Image titled Use Windows Command Prompt to Run a Python File Step 19

Image titled Use Windows Command Prompt to Run a Python File Step 20

  • You may have to scroll up or down with your mouse cursor hovering over the «User variables» pane to find the «Path» variable.

Image titled Use Windows Command Prompt to Run a Python File Step 21

Image titled Use Windows Command Prompt to Run a Python File Step 22

Click New . It’s on the right side of the window. A text field will open in the middle of the window.

Читайте также:  Api json example php

Image titled Use Windows Command Prompt to Run a Python File Step 23

Paste in your copied path. Press Ctrl + V to do so. Your copied path will appear in the text field in the middle of the window.

Image titled Use Windows Command Prompt to Run a Python File Step 24

Click OK on the three open windows. This will save your changes and close the «Path» window, the «Environmental Variables» window, and the «System Properties» window.

Community Q&A

I want to create a shortcut that executes the utility «ptpython,» running in the cmd prompt. The shortcut I have points to the directory containing «ptpython.exe» file but it does not execute it.

It sounds like ptpython.exe is a command-line utility, meaning it will only start if you execute it from a DOS window — you can’t create a shortcut for it directly. You can probably create a shortcut to cmd.exe, though (the DOS window) and pass it the ptpython.exe file as a parameter. Something like «cmd.exe /c ptpython.exe» should work, or if this disappears in the end, try with /k (instead of /c).

Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow

Yes. The directions to access the environment variables would be slightly different, as there is no «Power User» menu in Windows 7. Instead: 1. Press the Windows key and R to open the Run dialog. 2. Enter «sysdm.cpl». 3. Click the «Advanced» tab of the System Properties Window. 4. Click the «Environmental variables». Most everything else would work as described even on Windows 95 (if there’s a version of Python for Windows 95).

Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow

After opening the Command Prompt and navigation to the directory in which the py file exists and opening Python, not able to run the file using python file_name.py. It says that the syntax is wrong.

Источник

Оцените статью