Skip to content

How to execute python module?

Published:

Watch as a video

Table of Contents

Open Table of Contents

Execute python script in terminal

A basic python looks something like the following.

# file: sample.py
print("Hello World!!!")

python sample.py is a command that tells the Python interpreter to execute a Python script named sample.py.

Here’s what each part means:

Execute python project in terminal

A sample project will look something like the following.

pwd
/path/to/project/

tree
.
├── README.md
├── sample_project
│   ├── __init__.py
│   ├── __main__.py

To trigger this sample_project, we can simply pass it as a parameter to python command.

# path: /path/to/project/
python sample_project

When the above command is triggered, python interpreter will look inside the sample_project folder and search for __main__.py file. the Python interpreter will execute the code contained within the __main__.py file.

This is called as Python package with an executable module

Advantages