Menu Close

How to run function from the command line with Python?

Sometimes, we want to run function from the command line with Python.

In this article, we’ll look at how to run function from the command line with Python.

How to run function from the command line with Python?

To run function from the command line with Python, we can run python with the -c flag.

For instance, we run

$ python -c 'import foo; print(foo.hello())'

to run python -c with a string with the code we want to run.

Conclusion

To run function from the command line with Python, we can run python with the -c flag.

Posted in Python, Python Answers