- How to Run Python Program?
- Introduction
- Script:
- Interpreter:
- Different Ways to Run Python Program
- 1. The Python Interactive Mode
- 2. Using the Python Command Line
- 3. Run Python on Text Editor
- 4. Run Python on IDLE
- 5. Run Python On IDE (PyCharm)
- Conclusion
- How can I open Python files in IDLE from Windows?
- 6 Answers 6
How to Run Python Program?
In this step by step tutorial we are going to get an in-depth view of how to run any Python Program. We will cover every possible way to run our code seamlessly depending on various factors such as our coding environment, platform and needs.
Introduction
Do you know what is the most important step after writing any code? Yes, obviously to compile, execute and test the code! It is the only way to be sure if our code works at all, and whether we get the desired output.But since Python is an interpreted language, we can skip the compilation part and run the python script/file directly.
To run a Python Script,first let’s understand what is a Python Script —
Script:
- It is a plain text file containing Python code.
- It has the extension .py or .pyw
- It can be executed directly.
- Python interpreter is responsible for executing the Python scripts.
So, what is an Interpreter? Let’s see —
Interpreter:
- It is a program which is needed to run Python Script or code.
- It is mandatory for the Python Interpreter to be installed into one’s system to run the code.
- It can run a Python code in two ways:
- As a Script or Module
- As a piece of code written in an interactive session
Different Ways to Run Python Program
There are various ways to run a Python Program, let us see them —
1. The Python Interactive Mode
This is the most frequently used way to run a Python code. Here basically we do the following:
- Open command prompt / terminal in your system
- Enter the command — python or python3 (based on python version installed in your system)
- If we see these : >>> then, we are into our python prompt. It will look something like this:
Now we can start writing our code here.
- Best for testing every single line of code written.
- In this approach, each line of code is evaluated and executed immediately as soon as we hit ↵ Enter.
- Great deveopment tool for experimentation of Python code on the way.
- The code is gone as soon as we close the terminal(or the interactive session)
- Not user friendly way to write long code.
To close the interactive session we can:
- Type quit() or exit()
- Press ctrl + z and hit ↵ Enter for windows. For linux, we can press ctrl+d .
2. Using the Python Command Line
Follow the below steps to run a Python Code through command line —
- Write your Python code into any text editor.
- Save it with the extension .py into a suitable directory
- Open the terminal or command prompt.
- Type python/python3 (based on installation) followed by the file name.py —
Example 1: Saved with hello.py
Example 2: Saved with factorial.py
Note: If it doesn’t runs, then re-check your Python installation path & the place you saved your Python file.
3. Run Python on Text Editor
If you want to run your Python code into a text editor — suppose VS Code, then follow the below steps:
- From extensions section, find and install ‘Python’ extension.
- Restart your VS code to make sure the extension is installed properly
- Create a new file, type your Python code and save it.
- Run it using the VS code’s terminal by typing the command «py hello.py»
- Or, Right Click -> Run Python file in terminal
Example 1: Factorial of a number:
OUTPUT:
4. Run Python on IDLE
Python installation comes with an Integrated Development and Learning Environment, which is popularly known as IDLE. Though Python IDLE are by-default included with Python installations on Windows and Mac, if you’re a Linux user, then you can easily download Python IDLE using your package manager.
Steps to run a python program on IDLE:
- Open the Python IDLE(navigate to the path where Python is installed, open IDLE(python version))
- The shell is the default mode of operation for Python IDLE. So, the first thing you will see is the Python Shell —
- You can write any code here and press enter to execute
- To run a Python script you can go to File -> New File
- Now, write your code and save the file —
6. Once saved, you can run your code from Run -> Run Module Or simply by pressing F5 —
OUTPUT:
5. Run Python On IDE (PyCharm)
- In the Project tool window, select the project root, right-click it, and select File -> New -> Python File
- Then you will see a prompt, select the option Python File from the menu, and then type the new filename.
- Write your code in the Python file you have just created.
- To run this file, Right click on the file name -> Run filename
- PyCharm executes your code in the Run tool window. Check your output there:
Example 1:
OUTPUT:
Conclusion
Congratulations on running your first script in Python! Let’s get an overview of what you learn throughout this article :
- Script & Interpreter
- Different ways to run Python Script through :
- The Python interactive mode
- Using the Python command line
- Run on Text Editor
- Run on IDLE
- Run On IDE (PyCharm)
- We also saw detailed examples for each of them.
How can I open Python files in IDLE from Windows?
I’m new to Python. I use Python 2.7 and I am running Windows Vista (64-bit). How do I make it when I click on .py scripts. that it opens up in IDLE so I can edit it a snap? Ya know. without having to open IDLE by itself. I got all the way to C:\Python27\Lib\idlelib but I don’t see the IDLE application. Then when I do right-click and «Default open with» and select the idle.bat file.. I get this:
6 Answers 6
At least for Python 3.1 (it shouldn’t be any different for 2.7), IDLE is located in C:\Python31\Lib\idlelib\idle.bat .
So to make it open Python scripts, right-click a script, go to Open with → Choose default program, click Browse, browse to C:\Python27\Lib\idlelib , and select idle.bat (the extension might not show depending on your Windows settings). Remember to check the Always use selected program to open this kind of file button if it isn’t checked already.
Choose «Select a program from a list of installed programs», and then click Browse and follow the rest of my instructions.
Windows 7 Pro 64bit after installing python-3.5.1-amd64.exe had the same issue no edit context after install.
To correct: Right click .py file, open with Choose default program . then assigned to: c:\users\myusername\AppData\Local\Programs\Python\Python35\pythonw.exe and selected «Always use the selected program to open this kind of file» & the .py files changed to the Python IDE icon & the edit context menu started showing up.
Don’t you have «Edit with IDLE» among your options when you right-click a .py file? that should be IDLE’s default behavior, maybe it can change according to some installation/system settings. I wouldn’t set IDLE as the default program to open .py files, that’s python’s task. Also that would imply you don’t get them executed when you double click them or type their extension-complete name in the command line.
If what i said above doesn’t work for you, you could add the IDLE path you mentioned to the Path system variable in Windows, so you can type in the command line:
idle file_you_want_to_open.py
or
idle.py file_you_want_to_open.py
Once you add the IDLE path to the Path system variable you can also lauch IDLE typing «idle» in the box you get from windowsbutton-R. As a plus, you could add the .py estension to the PATHEXT system variable to have python scripts execute «by name only» like executables, system commands, compiled programs or whatever you call it.
note: this started as a comment, then got somehow . lengthy. I believe you can find all i said, just better, in many python tutorials around the internet.
EDIT to fit your edited question: my previuos answer should solve your problem even if your system is not properly set to run .pyw files, anyway just choose «select a program from a list. «, if python is not there look for it in its folder. It seems a matter of having python «linked» with .pyw files, like it’s supposed to be. Just let me repeat: you don’t want to have .py files open in IDLE by default, look at my answer above.