Tuesday, November 19, 2024
Google search engine
HomeLanguagesRun function from the command line In Python

Run function from the command line In Python

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 Command

Python Command

Using 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 filename

using filename

Running 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 option

using -m option

Here 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 Output

File Output

Access 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,

Access Class Method From Command Line

Class Example

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments