How to open python 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.

Читайте также:  Генерация случайного флага python

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.

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.

Читайте также:  Links in row css

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 Open Python on Windows, Mac, Linux

How to open Python

You’ll now learn how to open Python on Linux, Windows, and MacOS. First of all, you should know that there are two ways of using Python:

  1. Start an interactive shell, also called a REPL, short for read-evaluate-print-loop.
  2. Start a Python program that you stored in one or more files with the .py extension.

In this tutorial, we’ll start with the interactive shell because it’s ideal for exploring the language. But at some point, using the REPL won’t cut it anymore and you’ll have to start creating Python files.

If you installed Python on your local machine, you first need to start a terminal or command prompt before you can start the Python interactive shell. On all platforms, you should be able to start Python 3 with the command python3 (or sometimes just python ). Just be sure you are running Python 3, not 2, because some systems can have both versions installed.

How to open Python on Windows

On Windows, you can start Python from a terminal. E.g., to start PowerShell simply hit the Windows key and start typing “PowerShell”. You can use the ‘Command Prompt’ program if you don’t have PowerShell. When in a shell or command prompt, enter one of the following commands (try them in the given order):

Читайте также:  Document

The first command ( py ) is a wrapper script that allows you to start the latest version of Python. If it works, great. Just remember that I’ll often refer to python or python3 in the tutorial. You will need to use py in those cases.

Python started from Windows PowerShell

How to open Python on Mac

On MacOS, search for a program called terminal. You can do so by pressing the command key (⌘) + space bar. This will open up the Spotlight search bar, in which you start typing the word ‘terminal’.

Once you started the terminal, enter python3 to open the Python REPL. If that doesn’t work, try entering python instead (without the 3).

How to open Python on Linux

On Linux, you first need to start a terminal. This can often be done with the shortcut ctrl + alt + T . Alternatively, you can search for the terminal program in your start menu. The name and where to find it differ from distribution to distribution. Once you have a terminal running, enter python3 to start the Python REPL. If that doesn’t work, try python instead (without the 3).

How to close the REPL

Now that you know how to open the REPL, it would be nice to properly close it too. If you simply close the terminal window, you will be able to close the REPL as well. However, it won’t be a clean exit and your terminal will usually warn you about that too. So how do you exit the REPL cleanly?

What works on every OS, is the following command: exit()

If you’re on Linux or MacOS, you can also use a little trick. By pressing control + d, the REPL will exit immediately. Control + d sends the ‘end of file’ character the to terminal, and the REPL interprets this as a request to exit Python. On Windows, you can do something similar by pressing control + z and then hitting enter.

Get certified with our courses

Learn Python properly through small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

Источник

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