Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

How to Run a Python Script

While this topic might seem like a beginner’s level concept, it is not just for a novice Python developer who has written their first code and is wondering how to run the script. This article is about giving you, a Software Engineer, several methods to run a Python script. So if one method fails for any reason, you can fall back on one of the others and run your script without any hassle!

If you are preparing for a tech interview, check out our technical interview checklist, interview questions page, and salary negotiation e-book to get interview-ready! Also, read Python String join() Method, Sum Function in Python, and How to Read and Write Files in Python for more specific insights and guidance on Python concepts and coding interview preparation.

Having trained over 9,000 software engineers, we know what it takes to crack the toughest tech interviews. Since 2014, Interview Kickstart alums have been landing lucrative offers from FAANG and Tier-1 tech companies, with an average salary hike of 49%. The highest ever offer received by an IK alum is a whopping $933,000!

At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.

Want to nail your next tech interview? Sign up for our FREE Webinar.

In this article, we’ll discuss:

  • Code, Script, and Module
  • How to Run a Python Code Interactively
  • How to Run a Script From a Text Editor
  • How to Run Python Script From Command-Line or Terminal
  • How to Run a Python Script Using Python Interpreter
  • How to Run a Python Script Using a Batch File

Code, Script, and Module

Here’s what each of these terms means:

How to Run a Python Code Interactively

A commonly used method of running Python code is via an interactive session. We can run the Python interpreter in interactive mode. In an interactive session, you can test every piece of code as you go, making this a useful development tool. Statements written when working with an interactive session are evaluated and executed immediately. When the interactive session ends, the code ceases to exist. 

You can initiate a Python interactive session by typing Python in the command-line or terminal and pressing Enter. On Windows terminal and UNIX terminal, >>> represent the standard prompt for the interactive mode. A lack of these characters means that you need to re-install Python. But once you get them, you can start typing individual lines of code in Python, which the interactive session remembers, and test them individually.

How to Run a Script from a Text Editor

Most Python programmers prefer using text editors and directly run their Python script file or code from there. One reason for this is that in a Python interactive session, the code is lost after the session ends, but that’s not a limitation for text editors. 

Text editors essentially point the Python interpreter to the Python code. Some widely used text editors are:

  • Sublime Text
  • Vim
  • VSCode
  • Notepad++

When working on Windows or UNIX OS, the Python files must be saved with .py as the extension. The steps to use a text editor to run Python Script are:

  1. Open text editor.
  2. Write your code. A simple example of a Python script is a hello world statement like print('Hello World!').
  3. Save your file with a .py extension.
  4. Run the file in your editor. For example, Sublime has Ctrl + B as the shortcut for running the Python code inside it.

How to Run Python Script from Command-Line or Terminal

Instead of writing code line by line in a terminal, if you write all your code in your text editor and save it with a .py extension, you can run that Python script in the terminal in the following way:

  1. Open the terminal
  2. Go to the directory where the .py file exists.
  3. Run the script using the keyword python along with the file name:

python3 file_name.py

The process for running Python script using command line or terminal is simple but will look different for different operating systems. Let us discuss how that works in the three most popular operating systems. 

How to Run a Python Script on Windows

On Windows, the command line is called the command prompt and can be accessed in the following ways:

  • Click on the Start menu and search for the command prompt. 
  • Go to the Start menu, open the Run dialog box, type cmd, and press Enter. 

Once you’ve opened the command prompt:

  1. Type the path to python.exe and press Enter 
  2. Use the command py to reference the Python interpreter.
  3. If you want to check the version of Python, type py -V and press Enter. If you want to use a specific version of python, you can specify it as py -3.5 -V to run version 3.5.
  4. Finally, to run a script, mention the py command followed by the .py file name. If your command prompt and your Python script file are in the same directory, you only need to use the filename.

    For example, py -3.0 file_name.py. But if your command prompt and your Python script file are not in the same directory, you have to give the full path of your python file.

How to Run a Python Script on Mac

The process to run a script in a Mac system is pretty simple:

  1. Open Launchpad.
  2. Search for terminal.
  3. Type Python --version in the terminal and press Enter, and you’ll get an output stating the version of Python.
  4. Type python3 and press Enter to start coding in Python3 version.
  5. Write your code statement and press Enter to run it.

You can also state python3 file_name.py and press Enter to run a python script stored in file_name.py

How to Run a Python Script on Linux

To run a script on Linux:

  1. Right-click on your desktop and click on Terminal to open the terminal. You can also open the terminal by pressing Ctrl+Alt+T. 
  2. Type python and press Enter to start writing your code. If you want to get the version, type python --version.
  3. If you want to run a script, then in the terminal, go to the directory where your python script is present using the cd (change directory) command.
  4. Type python file_name.py and Enter, to run the script. If you don’t go to the directory where your script is present, you will need to give the full path of file_name.py, not just the name in this command.
  5. If you want to avoid using the term python every time, you can make your script executable by:
    a. Prepending #! /usr/bin/python in the script.
    b. Typing chmod +x file_name.py and pressing Enter
  6. Once you’ve made your script executable, just type ./file_name.py.

How to Run a Python Script Using Python Interpreter

The interpreter serves as the Python code runner. You need it to run your code no matter how you do it. The process to run Python script by the interpreter is called the Python Execution Model, which consists of the following steps:

  1. The interpreter processes the expressions/statements in the script sequentially.
  2. The code is compiled and converted into instructions in a low-level language that’s intermediate and machine-independent called bytecode. Bytecode optimizes the execution process, and the interpreter can then skip compiling the next time the code needs to be executed.
  3. The interpreter transfers the code to go ahead with the execution.
  4. The Python Virtual Machine or PVM, a part of your Python environment, loads the bytecode at runtime, reads every operation, and executes it as specified. PVM is the part of the Python environment in your machine that truly runs your script.

How to Run a Python Script Using a Batch File

Instead of writing your desired version preference and options every time you run a python file, you can store the command (for example, py -3.5 mycode.py) in a file with the extension mycode.bat. The file will then be called a batch file, and it just runs your command as preferred. 

You can also include @echo off optionally in your batch file to keep the python command from being echoed on screen when it’s executed. 

To run your Python script written in mycode.py, you can just run the file mycode.bat, for which you just type mycode.bat on command prompt and press Enter.

Writing the Output of Python Script to a File

To write the output of Python script to a file:

  1. Create a new folder (say myscriptfolder) and move your script (say file_name.py) to that folder. Ensure no other text file is in that folder, or at least no text file with the same name as your desired output file name
  2. Type python3 command_line.py 10 > outputofscript.txt
  3. Check the folder you created; it will contain the file outputofscript.txt file

FAQs on How to Run a Python Script or File

1. How do I download and install Python?

You begin by downloading the current production version of Python from the Python Download site. Then you double-click the downloaded file icon and accept default settings (or customize if you know what you need), and then click on the finish button to complete your installation.

2. Where can I run Python code online?

You can run python code online at Python.org, which is the official website of Python. You can also use online compilers like Sphere Engine to easily run Python code in different Python versions.

3. In the terminal, when do we need to give the full path of the script, and when will giving only the file name work?

When you’re in the directory where your python script exists, you can only use the python file name. If you’re in any other directory, you’ll need to give the full path.

4. How do I import a Python script into another script?

You can import a file, say, file_name.py using import by writing: import file_name

5. How do I make a Python file executable?

First, in your script, at the top, add a shebang line #!/usr/bin/env python. To make your script executable, run the command chmod +x myscript.py.

Ready to Nail Your Next Coding Interview?

Whether you’re a Coding Engineer gunning for Software Developer or Software Engineer roles, a Tech Lead, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!

If you’re looking for guidance and help with getting started, sign up for our FREE webinar. As pioneers in the field of technical interview preparation, we have trained thousands of software engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

Attend our Free Webinar on How to Nail Your Next Technical Interview

Recommended Posts

All Posts