Run function from the command line In Python
Last Updated :
28 Apr, 2025
Python is a flexible programming language with a wide range of uses that one of Python’s most useful ones is its ability to execute functions from the command line. Especially this feature is very useful when it comes to automation and scripting etc. In this article, I’ll explain how to execute a Python function from the command line.
Defining a Function
As we have done creating a file for the Python script we can move forward to defining a function that we will be executing from the command line. Below is the script that defines a function that returns the current date and time and prints it on the command prompt.
Python3
# defining a function return the current time
import time
def show_time():
return time.ctime()
if __name__ == '__main__':
print(show_time())
Running Python Script Using Command Line
Now that you've defined your function show_time in this case, let's run the script from the command line. For that open up a terminal or command prompt and navigate to the directory where you saved your Python file.
Using the Python command
To run the script using the Python command, enter the following command:
python show_time.py
If the script did not raise any error which in this case you can see the output as below,
Output:
Python CommandUsing the Script Filename
First of all, the Python interpreter evaluates scripts before executing them, so it's impossible to run a Python script without using the Python command.
However, on UNIX-like operating systems, you can specify the path to the Python interpreter using a so-called "shebang" line. Add the "shebang" line to the beginning of the script you want to run as shown below.
Python3
#!/usr/bin/python
import time
def show_time():
return time.ctime()
if __name__ == '__main__':
print(show_time())
In the above code, the first line #!/usr/bin/python is called a shebang which specifies the Python interpreter location. We can execute the script using the filename as illustrated,
using filenameRunning Modules With the -m Option
Normally we run a Python script as python filename.py in this case we run the script as a standalone. Consider a case we want to run a script that is a part of a bigger project here when -m or executing a script as a module comes into the picture. To run a Python script as a module follow as illustrated,
python -m <module_name>
using -m optionHere you can see we haven't used the .py extension after the filename as show_time is treated as a module.
Redirecting the Output
Redirecting refer to saving the output generated by the script into a file for future purpose, for example when running the pip freeze we redirect the output that are packages used by the Python app to a .txt file as shown below,
pip freeze > requirements.txt
This .txt could be used by other developers to set up the development for the app. In the same way, we can save or redirect the output of a Python script to a file as shown below,
Python3
import time
def show_time():
return time.ctime()
if __name__ == '__main__':
print(show_time())
python filename.py > outputfile
File OutputAccess Class Method From Command Line
A Class Method is a function that operates on the class itself it does not require an instance. To run a class method from the command line first, we need to create a class method and for it, we need to create a class so the below script consists of a class with a class method that returns the current time,
Python3
#!/usr/bin/python
# defining a function return the current time
import time
class ShowTime:
def __init__(self):
pass
@classmethod
def show_time(self):
return time.ctime()
if __name__ == '__main__':
print(ShowTime.show_time())
In the above script, using the @classmethod decorator a class method show_time is declared which when executed from the command line gives the following output,
Class Example
Similar Reads
Run shell command from GUI using Python In this article, we are going to discuss how we can create a GUI window that will take Operating System commands as input and after executing them, displays the output in a pop-up window. Modules Required: PyAutoGui: It is a module in python which is used to automate the GUI and controls the mouse a
2 min read
How to Resolve Python Command Not Found Error in Linux Getting the "Python command not found" error in Linux usually means Python is missing or not properly set up on your system. This common issue can be caused by missing installations, incorrect PATH settings, or version mismatches. In this guide, youâll learn the exact steps to fix this error, so you
4 min read
Run python script from anywhere in linux In Linux, there is a way to execute python files from anywhere. This can be done by typing several commands in the terminal. Prerequisite: Basic Shell Commands in Linux Basics of python Steps: At first, open the terminal and go to the home directory. To go the home directory type the following comma
2 min read
Running Custom Django manage.py Commands in Tests Django's manage.py commands are powerful tools for performing various administrative tasks. Sometimes, you might want to call these custom commands directly from your test suite to test their functionality or to set up the environment for your tests. In this article, we will walk through the process
2 min read
Open files from the command line - PyCharm PyCharm is one of the most popular Python-IDE developed by JetBrains and used for performing scripting in Python language. PyCharm provides some very useful features like Code completion and inspection, Debugging process, etc. In this article, we will see how we can open files from the command line
2 min read