Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running the python script in command line does not print any output

I want to make a script in python and then run it from the command line. The script is called test.py and the command to run it is:

python3 test.py John Jackson

Here is the expected output:

John Jackson

And here is the python script I made:

class person:

    def __init__(self, first, last):
        self.firstname = first
        self.lastname = last

    def get_first(self):
        return self.firstname

    def get_last(self):
        return self.lastname

import argparse

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('first')
    parser.add_argument('last')
    args = parser.parse_args()
    c1 = person(args.first, args.last)
    print(c1)

But the problem is when I run the script from the command line using the mentioned command, it returns nothing. Do you know how to fix it and get the expected output?

like image 850
John Avatar asked May 18 '19 12:05

John


People also ask

Why is my output not showing in Python?

One of the reasons of why your Python script does not show any output is because it buffers stdout and stderr steams instead of printing them. This also can happen if you execute the Python script from a CI/CD pipeline (e.g. using Jenkins, Gitlab-CI, TeamCity, etc.) or if you run it using a Dockerfile .

Why is my Python not working in command prompt?

The “Python is not recognized as an internal or external command” error is encountered in the command prompt of Windows. The error is caused when Python's executable file is not found in an environment variable as a result of the Python command in the Windows command prompt.

Why does py command work but not Python?

py is itself located in C:\Windows (which is always part of the PATH ), which is why you find it. When you installed Python, you didn't check the box to add it to your PATH , which is why it isn't there. In general, it's best to use the Windows Python Launcher, py.exe anyway, so this is no big deal.


2 Answers

You defined your main() function but did not call it.

Add this at the end of your script:

if __name__ == "__main__":
    main()

See What does if __name__ == “__main__”: do?.

Then, c1 is the person instance, which would print:

$ python3 test.py John Jackson
<__main__.person object at 0x104907ef0>

You need to call get_first() and get_last() to get the correct output:

print(c1.get_first(), c1.get_last())
like image 178
Gino Mempin Avatar answered Nov 15 '22 05:11

Gino Mempin


I am not sure if you are calling your main function while running the script.

Add

if __name__ == "__main__":
    main()

And override __str__ function of the person class to print first name and last name while passing class as argument to print.

def __str__(self):
    return self.firstname + ' ' + self.lastname

The whole code goes like this

import argparse

class person:

    def __init__(self, first, last):
        self.firstname = first
        self.lastname = last

    def get_first(self):
        return self.firstname

    def get_last(self):
        return self.lastname

    def __str__(self):
        return self.firstname + ' ' + self.lastname

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('first')
    parser.add_argument('last')
    args = parser.parse_args()
    c1 = person(args.first, args.last)
    print(c1)

if __name__ == '__main__':
    main()
like image 42
Akhil Batra Avatar answered Nov 15 '22 03:11

Akhil Batra