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.
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