Programming has an indispensable role in our world. You would not be reading this blog without it. Behind your favorite website, like Instagram, Facebook, YouTube, there is some sort of programming at work. A bit more obvious, and just in case you didn’t know: Your cellphone has been coded.
Everything you do with your cell phone, even an old-fashioned one, responds to a series of codes written by very proficient programmers.
Computers do not understand languages like English or Hindi. They have their own language called Machine Code, which tells them what to do, but it doesn’t make much sense to humans!
So innovative humans created an easier way of communicating with computers known as “Programming Languages.”
There are around 700 programming languages like C, C++, Java, C#, Python, PHP, Ada, etc., and studying programming fundamentals can set you apart from your equivalents, giving you a competitive edge in this technology-driven world.
Now, Suppose you plan to explore Python Programming but don’t know how to execute python code. In that case, it’s of no use, so the primary skill which you need to develop as a Python programmer is running Python scripts or code. It will be the only way of knowing if your code works as intended or not.
Hence, this article will explain numerous ways of executing a python code depending on the platform, environment, requirements, and expertise.
Above, you must have encountered two terms, code and scripts. Therefore, you need to know the primary differences between code and scripts before proceeding into other details.
Code Vs. Scripts
Coding Is a Genre; Scripting Is a Subgenre. It means coding is a bigger picture and includes more than just scripting. It seems confusing, right!! But the difference between them is a long debate that’s relatively hard to answer.
In simple terms, you can remember that:
- A code needs to be compiled, whereas a script is not compiled but interpreted.
- A code file has extensions like ‘.cpp’, ‘.java’. Whereas the file containing the script has the extension ‘.py’ , ‘.php’, ‘.js’.
To know in-depth the variations between coding and scripting languages, you can read the Outlining The Difference Between Programming & Scripting language.
We can conclude that all scripting languages are considered programming languages, but vice-versa is not correct, and scripting languages like Python run on an interpreter.
You must be questioning, What is an Interpreter?
The Interpreter refers to system software that can read and execute the program or interpret programs. This interpretation includes the source code of the high-level language, precompiled programs and scripts.
It should be noted that the Interpreter interprets the program line by line, i.e. translates an instruction in one go. This feature makes it easy for programmers to check a specific line while debugging, but it slows the overall execution time of the entire program.
It also does not generate any intermediate object code, which makes it more memory intensive.
However, if you can’t run Python scripts, there is no point in understanding all of this. Now let us continue to explore the method of running Python scripts.
Different Methods to Run Python Scripts
There are the following four methods to run python scripts:
- Using Command-Line prompt or terminal.
- Using an IDE or Text Editor
- Python Interactive Mode
- Using File Manager
RUN PYTHON SCRIPT USING COMMAND LINE
The easiest way to run Python scripts is to use the command prompt.
TO OPEN THE COMMAND PROMPT:
- On Windows, the command line is called the command prompt or MS-DOS console. A faster way to access it is to go to “Start” → “Run” and type cmd.
- On GNU/Linux, Multiple applications can access the command line such as xterm, Gnome terminal or console.
- In MAC OS X, call the system terminal through Application → Utilities → Terminal.
After successfully opening the command prompt, type the word cd followed by the path to your script file or python file and press Enter.
Then write filename.py now press Enter again, and You are Done!! The screen will display the output.
RUN PYTHON SCRIPTS USING AN IDE OR TEXT EDITOR
While developing more significant and complex applications, it’s suggested that you should use an Integrated Development Environment (IDE) or a text editor.
An IDE (or Integrated Development Environment) is a program committed to programming improvement. As the name infers, IDEs compose a few tools explicitly intended for software development.
These tools usually include an editor, run, build, execution, debugging and many more.
Python’s standard distribution includes IDLE as the default IDE to write, debug, modify, and run your code and scripts. Other IDEs such as Eclipse-PyDev, PyCharm, Eric, etc., also allow you to run Python scripts inside the environment.
If you want to run a Python script from your IDE:
- Launch PYTHON IDLE from the START MENU
- Create a new file and write your required script.
- Now save it (you can use CTRL+S ) with the .py extension.
The .py extension is used on all of these files to indicate Python executable files. In Windows, pyw is another extension for Python files.
- Finally, Run it;
You can use the default “Run” command or fast keys like Function + F5 or just F5 to run Python scripts.
- Bingo! Now you can see the output on your screen.
The formal definition is: “A text editor is a program that edits text files”.
Basically, a text editor is a program on your computer that allows you to create and edit files in various programming languages.
If you are starting with Python, you can use simple text editors like Sublime or Notepad++ or any other text editor.
You must now write a sample script. To do this:
- Launch your preferred text editor and write your script.
- Save the file to your desktop under the name samplefile.py or whatever you want.
- Remember that you only need to provide the .py extension.
- Click build in the toolbar.
- Subsequently, the screen will display the required output according to your script.
RUN PYTHON SCRIPT USING INTERACTIVE MODE
An Interactive mode is a great way to get started with Python since it allows you to test each piece of code while you’re working on it.
- All you have to do now is start the Python Interactive session.
- To do so, open a terminal or command line and type Python or Python3 depending on the Python version installed on your machine, then press Enter.
- The >>> represents the standard Interactive mode prompt on the terminal.
Note: If you don’t see these characters, your system has to be reinstalled with Python.
When working with an interactive session, the statements you type are evaluated and performed immediately, though the main drawback is that the code is lost when the interactive session ends.
But, the advantage is you don’t have to wait until your code is finished to test it. Instead, you can try it out on the spot.
So, whenever you’re working with Python and aren’t sure what a specific block of code does, just open the interactive session, paste the code, and watch what happens.
Once you’ve completed all of your testings, you can quit interactive mode by selecting one of the following alternatives:
- Exit() or quit() are built-in functions.
- For Windows, use the Ctrl+Z and Enter combination.
- Use Ctrl+D on Unix-like platforms.
RUN PYTHON SCRIPT USING FILE MANAGER
A Python script can also be started by double-clicking on its icon in the file manager.
In order to do so, you must satisfy specific requirements, which varies based on the operating system you’re using.
- Pythonw.exe and Python.exe are related to the extensions .pyw and .py, respectively, on Windows. This facilitates the scripts to be run by just double-clicking the icon.
- If the script has a command-line interface, a black window will appear on the screen for a second.
To prevent this, add input(‘Press Enter at Continue…’) to the script’s end, and as soon as you press Enter, the software will be terminated.
There are certain disadvantages to this strategy. Assume your script has a bug. Even before it reaches the statement mentioned above, the execution will be halted.
As a result, you will be unable to view the results.
- You need to set execution rights in the script if you are working on Unix-like systems and then run the scripts by simply double-clicking the icon in the file manager.
Note: This method should only be used for scripts that have already been debugged and are ready to go into production because double-clicking the scripts have restrictions and rely on various things such as the file manager, operating system, file association, execution rights, etc.
Check out this article - Quicksort Python
Also see, How to Check Python Version in CMD
Frequently Asked Questions
How do I run a .py file?
There are the following four methods to run python scripts:
1. Using Command-Line prompt.
2. Using an IDE or Text Editor
3. Python Interactive Mode
4. Using File Manager
Can you write Python in the terminal?
Yes, we can write Python scripts in the terminal.
How do I run python from the command line?
After successfully opening the command prompt, type the word cd followed by the path to your script file or python file and press Enter. Then write filename.py now press Enter again, and output will be displayed on the screen.
What is a Python terminal?
The Python interpreter (REPL) running inside a terminal window. In Windows, it is called Command Prompt. In macOS or Linux, it should be called Terminal.
Can you run Python online?
Yes, there are many online Python Interpreters like Jupyter, OnlineGDB etc.
How do I practice Python code?
At Coding Ninjas, you will get an adaptive and excelling Python Course and practice problems which will help you ease working in larger, more advanced Python environments.
Working with scripts offers its own set of benefits, including ease of learning and usage, speed of editing and execution, interactivity, and functionality. They’re also utilised to simplify complicated tasks by automating them.
Here, you gained the knowledge and skills about how to run Python in terminal using the following methods:
- The command-line prompt
- Your preferred integrated development environment (IDE) or text editor.
- Using Interactive Mode
- Using File Manager
After learning these fundamental ways, I would suggest you explore Coding Ninjas Python Course once because this provides you more ease at working in larger and more advanced Python environments, which will improve the development process and boost your efficiency.