How to Run a Python Script- A Step by Step Guide

How to Run a Python Script- A Step by Step Guide

25 May 2024
Beginner
2.54K Views
9 min read

How to Run a Python Script

Hey there! So, you’ve got a Python script and you're ready to see it running, but you don't know exactly where to start? No need to worry, you have come to the right place! Running a Python script is easier than you might think, even if you're new to programming.

Through this Python Tutorial, I'll tell you the simple steps on How to run a Python script, How to run a Python script in terminal, and much more. If you want to learn more about various concepts of Python, consider enrolling in our Python Certification Training.

Read More: Top 50 Python Interview Questions and Answers

What Are Scripts and Modules?

Before we start running Python scripts, let's understand what exactly we're working with. In Python Programming, 

  • A script is a file containing a series of instructions that tells Python what steps to follow to execute a specific outcome you want. These instructions can be anything from simple arithmetic operations to complex algorithms. 
  • Modules in Python are like toolkits that have pre-made functions and variables that can be used in your scripts. They help in organizing the code and make it easier to reuse certain functions across different scripts.

Running Python Scripts in Terminal: The Basics

Now, let's talk about how to actually run those Python scripts using the terminal. Think of the terminal as a command center for your computer – it's where you can type in commands to make things happen. Running a Python script in the terminal is like giving your computer a set of instructions written in Python code and asking it to execute them. Below are the steps discussed to run your Python script in a Terminal:

  1. To start, open up your terminal window. If you're using Windows, you can find it by searching for "Command Prompt" or "PowerShell" in the Start menu. If you're on macOS or Linux, you can usually find it by searching for "Terminal" in your applications.
  2. Once you've got your terminal open, navigate to the directory where your Python script is located using the cd command. For example, if your script is in a folder called "my_scripts" on your desktop, you'd type cd Desktop/my_scripts and press Enter.
  3. Now that you're in the right directory, you can run your Python script by typing python script_name.py and pressing Enter. Replace "script_name.py" with the name of your actual Python script file.

And that's it! Your Python script should now run in the terminal, and you'll see any output or results right there in front of you. Easy, right?

How to run a Python script in Linux?

If you're using Linux, running a Python script in Python 3 is quite straightforward. First, open your terminal. Navigate to the directory where your script is located using the cd command. For example:

cd path/to/your/script
Once you're in the correct directory, type the following command:
python3 script_name.py
script_name.py can be replaced with your file name.

Running Python Scripts as Executables

You can also run Python scripts as executables on Unix-like systems. First, add the shebang line at the top of your script:
#!/usr/bin/env python3
Then, with the following command, you can make the script executable:
chmod +x script_name.py
Now simply, type the below command to run your script directly.
./script_name.py

How to Run Python on Windows

On Windows, Command Prompt can be used to run the Python scripts with ease. Just open Command Prompt and use the cd command to go to your script's directory:
cd path\to\your\script
Then,  run your script with:
python script_name.py
Ensure Python is added to your system PATH to run it from any directory.

How to run a Python file in vscode?

Visual Studio Code (VSCode) is a popular editor for running Python scripts. First, open your script in VSCode. Then, you can run the script by clicking the "Run" button at the top right or by using the integrated terminal:
  1. Open the terminal in VSCode by pressing `Ctrl + ``.
  2. Navigate to your script's directory.
  3. Run the script with python script_name.py.
Read More: Python Developer Salary

Running Python command line argument

Sometimes, you'll need to run scripts with command line arguments. Here's how you can do it:

In your script, you can handle arguments using the sys module:

import sys

print(sys.argv)
To run the script with arguments, use the terminal:
python script_name.py arg1 arg2
Replace arg1 and arg2 with your actual arguments.

Running Python Scripts from a File Manager

On many systems, you can run Python scripts directly from the file manager. 
  1. Right-click the Python file.
  2. Select "Open with Python" or an equivalent option. 

Ensure your system is configured to recognize .py files with the Python interpreter.

How to run Python script directly in Kivy file?

Kivy is an open-source framework for Python. It is used for developing applications that are multi-platform. To run a Python script within a Kivy application, integrate the script with your Kivy .kv file. Here's a basic example:
# main.py
from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):
    def build(self):
        return Label(text="Hello, Kivy!")

if __name__ == "__main__":
    MyApp().run()
Run the Kivy application with:
python main.py

How to Run a Python Script using Docker?

With the help of Docker, you can run your applications in isolated containers. To run a Python script using Docker, follow these steps:
1. Create a Dockerfile in your script's directory.
FROM python:3.8
COPY script_name.py /
CMD ["python", "./script_name.py"]
2. Build the Docker image.
docker build -t my-python-script .
3. Run the Docker container.
docker run my-python-script

Running Python Scripts using an IDE or a Text Editor

IDEs like PyCharm, and text editors like Sublime Text, make running Python scripts easy. In PyCharm:
  1. Open your project.
  2. Right-click your Python script and select "Run".

In Sublime Text, ensure you have the SublimeREPL package installed. Open your script and use Ctrl + B to run it.

Conclusion
I hope now, you understand how to run your Python scripts in various environments and setups. If you want to learn more about the different concepts of Python, get your hands on the best Python certification Course right now!

FAQs

To run a .py file:
  1. Open your terminal and go to the directory where the file is using the cd command.
  2. Type 'python script_name.py' and press Enter.

Yes, you can run an application on Docker by creating a Dockerfile for your application and run it using Docker commands.

To run a .py file in Windows:
  1. Open Command Prompt and go to the directory where your script is using the cd command.
  2. Type 'python script_name.py' and your file will run.

To run Python in cmd:
  1. Open Command Prompt and go to your script's directory using the cd command.
  2. Type 'python script_name.py' and press Enter.
Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this