- How to Run a Python Script in Windows
- Your First Python Script
- Run Python Code from the Command Prompt
- Run Python Code from an IDE
- Run a Python Script from the File Manager
- Running Python Scripts in Windows: Next Steps
- How to Open and Run Python Files in the Terminal
- How to Create Python Files in the Terminal
- How to Open Python Files in the Terminal
- How to Run Python Files in the Terminal
- Create, Open, and Run Python Files in the Terminal
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.
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 Open and Run Python Files in the Terminal
A Python file or script is written in the Python language and saved with a «.py» extension. In this article, we focus on how to interact with Python files. We will learn how to create and open Python files in the terminal. We will also demonstrate how to run Python files in the terminal and redirect the output of the script to a file.
A Python file may contain a few up to several thousand lines of code that perform one or more tasks. For instance, we may have a Python file that reads data files from different locations, performs some manipulation on them, and writes the resulting data file to another location.
However, how to write Python code or scripts is a separate topic. Thankfully, Python is an easy-to-learn language that is popular for its simplicity.
LearnPython.com takes it one step further and makes the Python learning experience smooth and efficient. It offers several well-organized courses and tracks. Furthermore, the interactive console of LearnPython.com allows for practicing, a must in learning a programming language.
If you are new to Python, start learning with Python Basics. Part 1. By completing this course, you learn concepts such as variables, lists, conditional statements, loops, and functions, which are fundamental topics for programming and software development.
Let’s go back to our main topic of working with Python files in the terminal. We will use the text editor Vim to create a Python file. Vim comes pre-installed on macOS, but there are many different alternatives to choose from such as Sublime and Atom.
How to Create Python Files in the Terminal
Let’s start with opening a terminal and creating a project directory. Then, we change the working directory to the project folder using the «cd» command.
We create a Python file by typing «vim» along with the file name. My Python file is called «today.py.» You may name yours anything you’d like. Make sure it ends with the extension «.py».
After typing this command, hit enter. Vim opens up the following screen:
This is an empty Python file. To modify it, we need to hit «i», the command for switching to the insert or edit mode. When you hit «i», you see the following screen, and you can insert text into the file.
This Python file performs a very simple task. As its name suggests, it prints today’s date when executed. There are many different ways of doing this simple task; here, we use the built-in datetime module of Python.
In this first line, we import the datetime module. Modules and libraries make development processes easier because we can import and use their functionalities right away. Python also has numerous third-party libraries created by an active open-source community. Here is a list of top Python libraries if you’d like to discover more.
The today method of the datetime module returns a timestamp that contains the current date and time. Since we only need the date, we may also use the date method. The str constructor converts the date object to a string. Finally, we print today’s date using an f-string.
Our script is complete now. To exit the edit mode, hit ESC. Do not close the terminal yet because the file has not been saved. The command to save the file and close the editor is «:wq». The «w» is for «write», meaning we are writing to the file «today.py». The «q» is for «quit».
Once you type the «:wq» command, hit enter. The editor closes, and you see the following screen.
The file «today.py» has been saved in the «project-1» directory.
How to Open Python Files in the Terminal
We can view the content of existing files with a text editor. Let’s see an example using the file we just created.
The command «vim today.py» opens this Python file in the terminal. This command is the same as the one used for creating the file. Since the text editor knows the file exists, it opens the file instead of creating a new one.
Type the command «vim today.py» in the terminal and hit enter. Any other text editor, such as Sublime and Atom, works similarly. Then, you see the following screen:
How to Run Python Files in the Terminal
We have learned how to create Python files and how to open Python files in the terminal to view their contents. How about executing the file to see what it does? We can run Python files in the terminal as well.
First, we need to make sure the current working directory is the one in which the file is located. We open a terminal and change the directory to «project-1» since the «today.py» file was saved in that directory.
The command to execute a Python file is «python» or «python3» depending on how Python is installed on your computer. We type that along with the name of the file to be executed.
As we see in the output above, our script prints today’s date in the terminal.
Python scripts may be executed with arguments. Let’s write another script that gives us the date that is n number of days from today. The value of n is determined using a command-line argument.
Let’s name this script «from_today.py».
The next step is to open the Python file in the terminal using the command «vim from_today.py» and write the script that performs the task. To use command-line arguments, we need the sys module.
Once you write the Python code above in the file «from_today.py», exit the edit mode and save it.
We can now execute the file. Let’s ask the script to find the date 10 days from now:
Great! We have managed to execute a Python script with a command-line argument.
In the examples we have seen so far, the output of the file is displayed in the terminal. Let’s say we also want to write the output of this script to another file. Every time it is executed, the output is recorded in a text file. This is a very useful practice for keeping logs.
We can create a text file using Vim as follows:
- Open a terminal window and change the directory to «project-1» using the command «cd project-1».
- Type «vim logs.txt» and hit enter.
- The Vim editor opens a file. Press «i» and then hit ESC.
- Finally, type «:wq» to save the file in the directory «project-1».
In our Python Basics. Part 2 course, you learn much more about how to work with text files. In addition, this course covers two fundamental data structures in Python: lists and dictionaries.
The next step is to modify the «today.py» file so that it writes its output to the «logs.txt» file when it is executed. In the same terminal window, we open the file «today.py» by typing «vim today.py» and hitting «Enter». After the file is opened, hit «i» to switch to the edit mode and modify the script as shown below:
The last two lines open the «logs.txt» file and then write the value of the variable today in it. The «\n» is for writing a new line, so every output is in a separate line. I have also made a small change in the second line. The output is a datetime now instead of just the date. After making these changes, hit ESC and then type «:wq» to save them.
To learn more about date and time in Python, take our Python Basics. Part 3 course. It also covers two other important data structures in Python: sets and tuples.
We are not done yet! Always test your code. I execute the file «today.py» a couple of times.
I then check the content of the file «logs.txt» to see if the output has been written there. Let’s open the file now by typing the command «vim logs.txt».
As we see in the screenshot above, the output has been written in the «logs.txt» file as well.
Create, Open, and Run Python Files in the Terminal
We have seen how to create, open, and run Python files in the terminal. The «today.py» file we created is essentially a Python program, even though it is just 5 lines of code. It imports a built-in Python module to create a date object, then prints the value of the object in the console and writes it to another file. Learn more about how to work with files and directories in Python in this article.
What we have done in this article is only a very small part of what you can do with Python. It is one of the most popular programming languages and is used in a variety of areas, such as data science, web development, and mobile game development. Python is a great choice for a career in one of these fields.
Start with the Learn Programming with Python track. It introduces you to the fundamentals of programming. You do not need to have any prior experience with IT. This track consists of 5 fully interactive Python courses, carefully organized and presented for beginners.
There is a high demand for people with Python skills. Once you get comfortable with Python, there will be lots of jobs waiting for you!