How to run a python file

This recipe helps you run a python file

How to run a python scripts

Hello! In this tutorial we will be going through different ways in which we can run python scripts

There are a lot of ways in which we can run a python script. In this tutorial we will look into the most simple and commonly used ways. Lets get started with no further ado!

The different ways in which we can run a python script are as follows:

  • Running python scripts interactively
  • Running python scripts as .py file
  • Running a python script from the IDE

Running python scripts interactively

Once the installation process is complete on your system, then the next step is to open the terminal(In case of Mac/Linux) or the Command prompt(In case of Windows). If you have installed Anaconda, Anaconda navigator is the best choice.
Then, Enter the following command.

>>> python

This will open an interactive session like below. You can easily use any python functions and hit enter once you are done. The output will be visible right below.

You can quit the interactive shell using the quit() function or exit() function

Running python scripts as .py file

The other most common way of running python scripts is by running them as a .py file. The command to run a python file is fairly simple

>>> python filename.py

In order to do that, you have to create a file with a '.py' extension anywhere in your computer. For example, consider a file called 'sample.py' stored in E:\ProjectPro folder on a windows machine which contains the necessary python code.

Now if we open the Anaconda prompt on the windows machine, the default path will be C:\Users\Username. This is clearly not the folder where we have stored our sample.py file. In that case, I can run the following command

>>> python E:\PROJECTpro\sample.py

If the terminal/command prompt/anaconda prompt is open in the same directory, you can just replace the filepath by filename as shown below

>>> python sample.py

Running a python script from the IDE

There are a lot of python-friendly IDEs now a days that provide easy way of running python files. The default IDLE provided by Python during installation is an interactive shell by default.

We can also run python scripts from within the IDLE by clicking on File > New File from the menu. This will open a plain python file and we can easily run this python file by hitting Run > Run Module from the top menu or just by pressing F5 on the keyboard.

Hope this tutorial was helpful to all the pythoneers out there! Thanks for the read :)

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.