Every year my accountant asks for a inventory run down of my online store that runs on Shopify. So I decided to make my life easier on June 30th and write this Python script to do all the hard work for me.
It takes the following arguments from the CLI.
- --out (Output filename. Default: inventory.csv)
- --suppress (Suppress products from the output filename that have zero inventory, makes for a tidier csv file. Default: True)
- --factor. (Sometimes it is helpful to increase/decrease the stock level to make projections. Default = 1)
- --location. (Only include stock that is handled by shopify. Default: shopify)
- --delay. (Unless you are on Shopify Plus, your API Rates will be limited, so a small delay is made inbetween calls. Default = 3)
We will be using:
- Progress to display the progress of the scrip to the user.
- python-dotenv to read and use the environment variables
- Shopify API to access the Shopify Admin API in Python.
To install these packages, you should use Python's package installer, pip3. On the MacOS CLI you can use these commands:
pip3 install progress
pip3 install python-dotenv
pip3 install ShopifyAPI
This project includes an example of a .env files that you need to create to allow you to acccess your private Shopify App. You need to fill out your API key and API secret KEY before copying it to .env.
cp .env.local .env
If this is the first time you are running requests using SSL, you might numerous errors on the CLI when running the Python file such as:
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED]
If this is the case, as it was for me, check out the following stackoverflow.com answer that helped solve the issue for me.