Register for our webinar

How to Nail your next Technical Interview

1 hour
Enter details
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
You have registered for our webinar
Oops! Something went wrong while submitting the form.
Enter details
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
You are scheduled with Interview Kickstart.
Oops! Something went wrong while submitting the form.
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career

How to Run a Python File in Terminal?(In Easy 3 Steps)

Running Python scripts in the terminal is a basic skill that every Python programmer must know for implementing and checking tasks dealing with tests or program execution. This knowledge is essential for both beginners and advanced programmers. The three-step step-wise guide for running Python files in the terminal is discussed below.

What is a Python Script?

A Python script is a ".py" file containing instructions in the Python programming language. Ranging from concise one-liners to intricate programs, it is executed by a Python interpreter. File size determination methods vary, showcasing the flexibility of Python in accommodating scripts of diverse complexities.

How Python Scripts are Executed?

Executing a Python script involves the interpreter reading and processing the code sequentially. The interpreter converts the code into bytecode, a lower-level language. The Python Virtual Machine (PVM) then executes this bytecode, generating the intended output. This step-by-step process ensures the proper functioning of Python scripts, demonstrating the role of the interpreter and PVM in code execution.

Why run a Python file in a terminal?

Running a Python file in a terminal or command-line interface offers fundamental insights into program execution. Essential for understanding processes, it forms the basis for automation, a crucial skill. Terminals aid in debugging by providing comprehensive error outputs and addressing confusion caused by limited IDE displays. Moreover, terminal usage offers flexibility and control, enabling easy modification of script arguments and execution from any location.

How to Run a Python File in Terminal: Easy 3-Step Guide

A key skill for any Python developer is running a Python script in the terminal. It doesn't matter if you are a beginner or an expert coder; knowing how to run the Python scripts that call through the terminal can help improve your workflow.

In this 3-step guide that will turn out to be quite easy, we'll help you through the process by which anyone can run their Python files.

Step 1: Setting Up the EnvironmentIn

order to run the Python script properly without any error in the terminal, you should have Python installed on your system. You can check this by opening the terminal and entering the command:


This command should start an interactive Python shell, indicating information about the installed version of Python. If it does not indicate that Python may have been installed incorrectly, you should consider reinstalling them.

To exit the interactive Python environment, use the following command:


In addition, ensure that you have a text editor installed for creating and editing Python files. The most common ones are Sublime Text, Atom, Visual Studio Code, and Vim.

Step 2: Creating and Opening Python Files

Once your environment is set up, you can create and open Python files. If you prefer using Vim, a powerful text editor, you can create a Python file by navigating to the desired directory in the terminal and using the command:


Press Enter, and Vim will open an empty file for you to start entering your Python code. In order to insert a text, press 'i' on the keyboard for you to enter into your input mode.

To open an existing Python file, navigate to its directory in the terminal and use the command:


Step 3: Running Python Files in the Terminal

Now, let's move on to running your Python files in the terminal. Follow these steps:

  • Open the terminal and go into a folder that has your Python file.
  • Use the following command to execute the script:


Use the title of your Python file instead of "". Press Enter, and the Python interpreter runs this script, showing you results in the terminal.

Passing Command Line Arguments:If

your script requires command line arguments, append them after the script name:

python arg1 arg2

For instance, "arg1" and " arg2" are the placeholders for your specific command line arguments.

Automating a Task through the Terminal

To illustrate running Python scripts in the terminal, consider a simple script that opens Gmail every morning at 10:00 AM:

 import schedule
import webbrowser
def open_gmail():
    url = ''
While True:

Run this script using the following command:


This script will continuously run until the terminal is closed or the system is shut down, automating the task of opening Gmail at the specified time.

Handling Issues

Permission Issues:

If you encounter permission issues, use the following command to grant execute permission:

 chmod +x

Handling Dependencies:

Make sure that any reference libraries or modules used by your script are installed. Dependencies like pip or conda can be installed by package managers. The practice of having a requirements.txt file for the project is also meant to help manage dependencies.

Following these simple steps, you'll learn how to run a Python file in the terminal effectively for greater ease of development and automation.

Best Practices for Python Development

The simplicity and diversity of Python is the reason for its use in software development. But when your projects increase in size and intricacy, it is essential to keep a coherent and effective development procedure. Follow these best practices to ensure a smoother development experience:

Organizing Your Scripts

Module and Package Organization:

  • Divide your code into logical modules based on functionality.
  • Group related modules into packages, creating a structured hierarchy.
  • Use meaningful and descriptive names for modules and packages to convey their purpose clearly.

├── utils/
│ ├──
│ ├──
│ └──
└── tests/

Code Readability:

  • Follow PEP 8 guidelines for code styling to enhance readability.
  • Include comments and docstrings to explain complex functionalities.
  • Adopt a consistent naming convention for variables, functions, and classes.


    def calculate_area(radius):
    Calculate the area of a circle.
    - radius (float): The radius of the circle.
    Float: The area of the circle.
    return 3.14 * radius ** 2

Using Virtual Environments

Isolate Dependencies:

  • Utilize virtual environments to isolate project-specific dependencies.
  • This prevents conflicts with the system-wide Python installation or other projects.
  • Create a requirements.txt file to document and share project dependencies.


pip freeze > requirements.txt

Virtual Environment Tools:

Use tools like virtualenv or conda to create and manage virtual environments.

Activate the virtual environment before working on your project to ensure a consistent environment.


  # Using virtualenv
virtualenv venv
source venv/bin/activate

# Using conda
conda create --name myenv
conda activate myenv


Version Control Integration:

Include the virtual environment directory in your project's .gitignore file to avoid versioning.

This ensures that collaborators can create their virtual environments.

Example .gitignore:

 # Virtual Environment

Implementing these best practices not only makes your code easier to read and maintain but also helps promote better working relationships. Organised scripts and good practice of virtual environments play an important role in the development process with less disturbance.

Get Ready for Your Upcoming Python Coding Interview

Are you getting ready for your next Python interview? Join our Technical Interview Webinar to discover top strategies for acing Python coding interviews.

At Interview Kickstart, we've successfully coached over 10,000 engineers to secure offers from leading tech firms. Our instructors, having worked with FAANG++hiring managers, thoroughly understand the elements that determine success in some of the world's most difficult tech interviews.

Reserve your seat for our FREE webinar and take away the best of our tips. Don't miss this chance to improve your interviewing skills.

FAQs about Running a Python File in Terminal

1.What are the steps for opening a file in the terminal?

In the terminal, one should use a text editor such as Vim to open files. For example:


Change the command according to your choice of text editor.

2. How do I execute Python code?

Using a terminal or command prompt provided by your operating system, it is possible to run Python code. Open the terminal, navigate to the file's directory, and use the:


Rename "" with your Python file's name

3. How do you run a .py file in the Ubuntu terminal?

Navigate to the file's directory and execute:


Ensure that you have Python installed on your desktop.

4. How does one run a code in a terminal?

Navigate to the file's directory in the terminal and use:


This executes your Python code and displays the results.

5. How do I open a .py file?

For opening a Python file, use any text editor or integrated development environment (IDE). 

For example:


Last updated on: 
February 12, 2024

Abhishek Som

Senior Content Specialist at Interview Kickstart

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

Thank you! Your subscription has been successfully submitted!
Oops! Something went wrong while submitting the form.

Recommended Posts

All Posts