How to Call a Python Function from Command Line

If you don’t want to call an entire Python script but just a specific function, you can use this method. And you don’t even have to change your script for it.

Better Everything
4 min readAug 19, 2024
Learn how to call Python functions directly from command line. — Image by catalyststuff on Freepik

Running Python scripts

Running a script from a command line is pretty straightforward.

If your script is named create_invoices.py you can use one of these commands:

py create_invoices.py
python create_invoices.py
python3 create_invoices.py

But you have to make sure the computer knows where the file is located, for which you have 2 options:

Option 1: Specify the filepath in the command:

If the create_invoices.py script is located in the directory C:\Users\Projects\Invoices

You can use a command like:

py C:\Users\Projects\Invoices\create_invoices.py

This works when there are no spaces in the filepath.

Option 2: Change the working directory to the directory where the script is located with the cd command:

cd C:\Users\Projects\Invoices
py create_invoices.py

Passing data into the…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Better Everything

📖 My E-Books: amazon.com/author/better-everything ✅Programming, Data & Business ✅Automation & Optimization ✅Knowledge & Inspiration