Photo by Joshua Reddekopp on Unsplash

Posting to Instagram with Python in 2021 — three simple steps

Posting to Instagram using Python is really simple, and opens so many doors for automation

Phil Hather
1 min readFeb 22, 2021

--

The two best ways to organically grow your Instagram account are engaging with the community, and frequently posting. The latter can be hard to keep up with — but thankfully, we can automate this with Python.

Step 1

Install instabot — this is a fantastic Python library for doing pretty much everything a human can do on Instagram.

pip install instabot

Step 2

Define your function

from instabot import Botdef post_to_ig(image, caption):
bot = Bot()
bot.login(username = 'your_username',
password = 'your_password')
bot.upload_photo(image, caption)

Replace the placeholders in the script above with your username and password — it goes without saying that you should exercise caution if you’re storing credentials in a script.

Step 3

Call your function and run your script

post_to_ig('photo_filepath.jpeg','your caption #example')

Replace the placeholders above with the filepath to the photo you’d like to post, and your chosen caption. You can add in hashtags, \n for line breaks, and even emojis using the Python emoji library.

That’s it!

Now how about scheduling the code using crontab, or triggering it from somewhere else?

Enjoy!

--

--