René's URL Explorer Experiment


Title: How to Integrate Local LLMs With Ollama and Python – Real Python

Open Graph Title: How to Integrate Local LLMs With Ollama and Python – Real Python

Description: Learn how to integrate your Python projects with local models (LLMs) using Ollama for enhanced privacy and cost efficiency.

Open Graph Description: Learn how to integrate your Python projects with local models (LLMs) using Ollama for enhanced privacy and cost efficiency.

Mail addresses
?subject=Python article for you&body=How to Integrate Local LLMs With Ollama and Python on Real Python https://realpython.com/ollama-python/

Opengraph URL: https://realpython.com/ollama-python/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
{
  "@context": "http://schema.org",
  "@type": "Article",
  "headline": "How to Integrate Local LLMs With Ollama and Python",
  "image": {
    "@type": "ImageObject",
    "url": "https://files.realpython.com/media/How-to-Integrate-Local-LLMs-With-Ollama-and-Python_Watermarked.835ee5f2672d.jpg",
    "width": 1920,
    "height": 1080
  },
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": "https://realpython.com/ollama-python/",
    "lastReviewed": "2026-01-05",
    "author": {
      "@type": "Person",
      "name": "Leodanis Pozo Ramos",
      "image": "https://realpython.com/cdn-cgi/image/width=862,height=862,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Perfil_final1.9f896bc212f6.jpg",
      "url": "https://realpython.com/team/lpozoramos/",
      "affiliation": {
        "@type": "Organization",
        "@id": "https://realpython.com/#organization",
        "name": "Real Python",
        "url": "https://realpython.com",
        "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
      }
    },
    "reviewedBy": [
      {
        "@type": "Person",
        "name": "Aldren Santos",
        "image": "https://realpython.com/cdn-cgi/image/width=500,height=500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Aldren_Santos_Real_Python.6b0861d8b841.png",
        "url": "https://realpython.com/team/asantos/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Brenda Weleschuk",
        "image": "https://realpython.com/cdn-cgi/image/width=320,height=320,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/IMG_3324_1.50b309355fc1.jpg",
        "url": "https://realpython.com/team/bweleschuk/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Bartosz Zaczy\u0144ski",
        "image": "https://realpython.com/cdn-cgi/image/width=1694,height=1694,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/coders_lab_2109368.259b1599fbee.jpg",
        "url": "https://realpython.com/team/bzaczynski/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Martin Breuss",
        "image": "https://realpython.com/cdn-cgi/image/width=456,height=456,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/martin_breuss_python_square.efb2b07faf9f.jpg",
        "url": "https://realpython.com/team/mbreuss/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      }
    ]
  },
  "datePublished": "2026-01-21T14:00:00+00:00",
  "dateModified": "2026-01-05T15:28:18.337340+00:00",
  "publisher": {
    "@type": "Organization",
    "@id": "https://realpython.com/#organization",
    "name": "Real Python",
    "url": "https://realpython.com",
    "logo": {
      "@type": "ImageObject",
      "url": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png",
      "width": 512,
      "height": 512
    },
    "description": "Real Python is a leading provider of online Python education and one of the largest language-specific online communities for software developers. It publishes high-quality learning resources, such as tutorials, books, and courses to an audience of millions of developers, data scientists, and machine learning engineers each month.",
    "slogan": "Become a Python Expert",
    "email": "info@realpython.com",
    "sameAs": [
      "https://github.com/realpython",
      "https://www.youtube.com/realpython",
      "https://twitter.com/realpython",
      "https://x.com/realpython",
      "https://www.linkedin.com/company/realpython-com/",
      "https://www.facebook.com/learnrealpython",
      "https://www.instagram.com/realpython",
      "https://www.tiktok.com/@realpython.com"
    ]
  },
  "author": {
    "@type": "Person",
    "name": "Leodanis Pozo Ramos",
    "image": "https://realpython.com/cdn-cgi/image/width=862,height=862,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Perfil_final1.9f896bc212f6.jpg",
    "url": "https://realpython.com/team/lpozoramos/",
    "affiliation": {
      "@type": "Organization",
      "@id": "https://realpython.com/#organization",
      "name": "Real Python",
      "url": "https://realpython.com",
      "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
    }
  },
  "description": "Learn how to integrate your Python projects with local models (LLMs) using Ollama for enhanced privacy and cost efficiency.",
  "hasPart": {
    "@type": "FAQPage",
    "mainEntity": [
      {
        "@type": "Question",
        "name": "Can I use Ollama with Python?",
        "acceptedAnswer": {
          "@type": "Answer",
          "text": "

Yes, you can! Install the ollama package from PyPI, keep the Ollama service running, and call local models using chat() and generate() in your Python code.

" } }, { "@type": "Question", "name": "Is Ollama free?", "acceptedAnswer": { "@type": "Answer", "text": "

Ollama is open source and free to download and run locally. You still need to account for model licenses and local compute and storage costs, but there are no cloud per-token fees when running on your own machine.

" } }, { "@type": "Question", "name": "What are the benefits and downsides of using Ollama?", "acceptedAnswer": { "@type": "Answer", "text": "

Running locally improves privacy, reduces ongoing cloud spend, and enables offline work. The trade-offs are heavier hardware needs, sizable model downloads, and slower performance without a GPU.

" } }, { "@type": "Question", "name": "Do I need a GPU to run Ollama models?", "acceptedAnswer": { "@type": "Answer", "text": "

No, you don’t. Models can run on a CPU, though a GPU speeds things up considerably and makes larger models more accessible.

" } }, { "@type": "Question", "name": "When should I use ollama.chat() vs ollama.generate()?", "acceptedAnswer": { "@type": "Answer", "text": "

Use chat() for multi-turn, role-based conversations where you need to keep context and optionally stream the output. Use generate() for one-shot prompts that don’t require context, such as drafting, summarizing, or quick code generation.

" } } ] } }

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/How-to-Integrate-Local-LLMs-With-Ollama-and-Python_Watermarked.835ee5f2672d.jpg
og:imagehttps://files.realpython.com/media/How-to-Integrate-Local-LLMs-With-Ollama-and-Python_Watermarked.835ee5f2672d.jpg
twitter:creator@realpython
og:typearticle

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/ollama-python/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/ollama-python/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Follama-python%2F
Browse Topicshttps://realpython.com/tutorials/all/
Guided Learning Pathshttps://realpython.com/learning-paths/
Basicshttps://realpython.com/search?level=basics
Intermediatehttps://realpython.com/search?level=intermediate
Advancedhttps://realpython.com/search?level=advanced
aihttps://realpython.com/tutorials/ai/
algorithmshttps://realpython.com/tutorials/algorithms/
apihttps://realpython.com/tutorials/api/
best-practiceshttps://realpython.com/tutorials/best-practices/
careerhttps://realpython.com/tutorials/career/
communityhttps://realpython.com/tutorials/community/
databaseshttps://realpython.com/tutorials/databases/
data-sciencehttps://realpython.com/tutorials/data-science/
data-structureshttps://realpython.com/tutorials/data-structures/
data-vizhttps://realpython.com/tutorials/data-viz/
devopshttps://realpython.com/tutorials/devops/
djangohttps://realpython.com/tutorials/django/
dockerhttps://realpython.com/tutorials/docker/
editorshttps://realpython.com/tutorials/editors/
flaskhttps://realpython.com/tutorials/flask/
front-endhttps://realpython.com/tutorials/front-end/
gamedevhttps://realpython.com/tutorials/gamedev/
guihttps://realpython.com/tutorials/gui/
machine-learninghttps://realpython.com/tutorials/machine-learning/
newshttps://realpython.com/tutorials/news/
numpyhttps://realpython.com/tutorials/numpy/
projectshttps://realpython.com/tutorials/projects/
pythonhttps://realpython.com/tutorials/python/
stdlibhttps://realpython.com/tutorials/stdlib/
testinghttps://realpython.com/tutorials/testing/
toolshttps://realpython.com/tutorials/tools/
web-devhttps://realpython.com/tutorials/web-dev/
web-scrapinghttps://realpython.com/tutorials/web-scraping/
Table of Contentshttps://realpython.com/ollama-python/#toc
Prerequisiteshttps://realpython.com/ollama-python/#prerequisites
Step 1: Set Up Ollama, Models, and the Python SDKhttps://realpython.com/ollama-python/#step-1-set-up-ollama-models-and-the-python-sdk
Get Ollama Runninghttps://realpython.com/ollama-python/#get-ollama-running
Pull the Required Modelshttps://realpython.com/ollama-python/#pull-the-required-models
Install Ollama’s Python SDKhttps://realpython.com/ollama-python/#install-ollamas-python-sdk
Step 2: Generate Text and Code From Pythonhttps://realpython.com/ollama-python/#step-2-generate-text-and-code-from-python
Interact With Ollama’s Chat Interfacehttps://realpython.com/ollama-python/#interact-with-ollamas-chat-interface
Use Ollama’s Text Generation Interfacehttps://realpython.com/ollama-python/#use-ollamas-text-generation-interface
Step 3: Use Tool Calling for Augmented Answershttps://realpython.com/ollama-python/#step-3-use-tool-calling-for-augmented-answers
Understand the Tool Calling Workflowhttps://realpython.com/ollama-python/#understand-the-tool-calling-workflow
Implement Tool Calling in Pythonhttps://realpython.com/ollama-python/#implement-tool-calling-in-python
Conclusionhttps://realpython.com/ollama-python/#conclusion
Frequently Asked Questionshttps://realpython.com/ollama-python/#frequently-asked-questions
https://realpython.com/feedback/survey/article/ollama-python/liked/?from=article-sidebar
https://realpython.com/feedback/survey/article/ollama-python/disliked/?from=article-sidebar
Leodanis Pozo Ramoshttps://realpython.com/ollama-python/#author
https://realpython.com/ollama-python/#reader-comments
intermediatehttps://realpython.com/tutorials/intermediate/
aihttps://realpython.com/tutorials/ai/
toolshttps://realpython.com/tutorials/tools/
Prerequisiteshttps://realpython.com/ollama-python/#prerequisites
Step 1: Set Up Ollama, Models, and the Python SDKhttps://realpython.com/ollama-python/#step-1-set-up-ollama-models-and-the-python-sdk
Get Ollama Runninghttps://realpython.com/ollama-python/#get-ollama-running
Pull the Required Modelshttps://realpython.com/ollama-python/#pull-the-required-models
Install Ollama’s Python SDKhttps://realpython.com/ollama-python/#install-ollamas-python-sdk
Step 2: Generate Text and Code From Pythonhttps://realpython.com/ollama-python/#step-2-generate-text-and-code-from-python
Interact With Ollama’s Chat Interfacehttps://realpython.com/ollama-python/#interact-with-ollamas-chat-interface
Use Ollama’s Text Generation Interfacehttps://realpython.com/ollama-python/#use-ollamas-text-generation-interface
Step 3: Use Tool Calling for Augmented Answershttps://realpython.com/ollama-python/#step-3-use-tool-calling-for-augmented-answers
Understand the Tool Calling Workflowhttps://realpython.com/ollama-python/#understand-the-tool-calling-workflow
Implement Tool Calling in Pythonhttps://realpython.com/ollama-python/#implement-tool-calling-in-python
Conclusionhttps://realpython.com/ollama-python/#conclusion
Frequently Asked Questionshttps://realpython.com/ollama-python/#frequently-asked-questions
Remove adshttps://realpython.com/account/join/
Click here to download the free sample codehttps://realpython.com/bonus/ollama-python-code/
https://realpython.com/quizzes/ollama-python/
How to Integrate Local LLMs With Ollama and Pythonhttps://realpython.com/quizzes/ollama-python/
https://realpython.com/ollama-python/#prerequisites
Ollamahttps://realpython.com/ref/ai-coding-tools/ollama/
install Pythonhttps://realpython.com/installing-python/
Remove adshttps://realpython.com/account/join/
https://realpython.com/ollama-python/#step-1-set-up-ollama-models-and-the-python-sdk
https://realpython.com/ollama-python/#get-ollama-running
download pagehttps://ollama.com/download
https://realpython.com/ollama-python/#pull-the-required-models
llama3.2:latesthttps://ollama.com/library/llama3.2:latest
codellama:latesthttps://ollama.com/library/codellama:latest
Dockerhttps://realpython.com/docker-continuous-integration/
prompthttps://realpython.com/ref/ai-coding-glossary/prompt/
Remove adshttps://realpython.com/account/join/
https://realpython.com/ollama-python/#install-ollamas-python-sdk
PyPIhttps://realpython.com/ref/glossary/pypi/
virtual environmenthttps://realpython.com/ref/glossary/virtual-environment/
https://realpython.com/ollama-python/#step-2-generate-text-and-code-from-python
promptshttps://realpython.com/ref/ai-coding-glossary/prompt/
LLMshttps://realpython.com/ref/ai-coding-glossary/llm/
https://realpython.com/ollama-python/#interact-with-ollamas-chat-interface
listhttps://realpython.com/python-list/
dictionarieshttps://realpython.com/python-dicts/
argumentshttps://realpython.com/ref/glossary/argument/
attributehttps://realpython.com/ref/glossary/attribute/
list comprehensionshttps://realpython.com/list-comprehension-python/
numberhttps://realpython.com/python-numbers/
appendhttps://realpython.com/python-append/
CLIhttps://realpython.com/command-line-interfaces-python-argparse/
argumenthttps://realpython.com/ref/glossary/argument/
Truehttps://realpython.com/ref/keywords/true/
iteratorhttps://realpython.com/python-iterators-iterables/
printhttps://realpython.com/python-print/
run the scripthttps://realpython.com/run-python-scripts/
Remove adshttps://realpython.com/account/join/
https://realpython.com/ollama-python/#use-ollamas-text-generation-interface
functionhttps://realpython.com/ref/glossary/function/
custom functionhttps://realpython.com/defining-your-own-python-function/
FizzBuzzhttps://en.wikipedia.org/wiki/Fizz_buzz#Programming
REPLhttps://realpython.com/python-repl/
classeshttps://realpython.com/python-classes/
https://realpython.com/ollama-python/#step-3-use-tool-calling-for-augmented-answers
function callinghttps://realpython.com/ref/ai-coding-glossary/function-calling/
retrieval-augmented generation (RAG)https://realpython.com/ref/ai-coding-glossary/retrieval-augmented-generation/
https://realpython.com/ollama-python/#understand-the-tool-calling-workflow
https://realpython.com/ollama-python/#implement-tool-calling-in-python
docstringshttps://realpython.com/ref/glossary/docstring/
type hintshttps://realpython.com/ref/glossary/type-hint/
Remove adshttps://realpython.com/account/join/
https://realpython.com/ollama-python/#conclusion
Click here to download the free sample codehttps://realpython.com/bonus/ollama-python-code/
https://realpython.com/ollama-python/#frequently-asked-questions
https://realpython.com/quizzes/ollama-python/
How to Integrate Local LLMs With Ollama and Pythonhttps://realpython.com/quizzes/ollama-python/
https://realpython.com/feedback/survey/article/ollama-python/liked/?from=article-footer
https://realpython.com/feedback/survey/article/ollama-python/disliked/?from=article-footer
https://realpython.com/team/lpozoramos/
» More about Leodanishttps://realpython.com/team/lpozoramos/
https://realpython.com/team/asantos/
Aldrenhttps://realpython.com/team/asantos/
https://realpython.com/team/bweleschuk/
Brendahttps://realpython.com/team/bweleschuk/
https://realpython.com/team/bzaczynski/
Bartoszhttps://realpython.com/team/bzaczynski/
https://realpython.com/team/mbreuss/
Martinhttps://realpython.com/team/mbreuss/
Level Up Your Python Skills »https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=ollama-python
Level Up Your Python Skills »https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=ollama-python
https://realpython.com/feedback/survey/article/ollama-python/liked/?from=article-comments
https://realpython.com/feedback/survey/article/ollama-python/disliked/?from=article-comments
LinkedInhttps://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Frealpython.com%2Follama-python%2F
Twitterhttps://twitter.com/intent/tweet/?text=Interesting%20Python%20article%20on%20%40realpython%3A%20How%20to%20Integrate%20Local%20LLMs%20With%20Ollama%20and%20Python&url=https%3A%2F%2Frealpython.com%2Follama-python%2F
Blueskyhttps://bsky.app/intent/compose?text=Interesting%20Python%20article%20on%20%40realpython.com%3A%20How%20to%20Integrate%20Local%20LLMs%20With%20Ollama%20and%20Python%20https%3A%2F%2Frealpython.com%2Follama-python%2F
Facebookhttps://facebook.com/sharer/sharer.php?u=https%3A%2F%2Frealpython.com%2Follama-python%2F
Get tips for asking good questionshttps://realpython.com/python-beginner-tips/#tip-9-ask-good-questions
get answers to common questions in our support portalhttps://support.realpython.com
Real Python Community Chathttps://realpython.com/community/
“Office Hours” Live Q&A Sessionhttps://realpython.com/office-hours/
intermediatehttps://realpython.com/tutorials/intermediate/
aihttps://realpython.com/tutorials/ai/
toolshttps://realpython.com/tutorials/tools/
LlamaIndex in Python: A RAG Guide With Exampleshttps://realpython.com/llamaindex-examples/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=ollama-python
How to Integrate ChatGPT's API With Python Projectshttps://realpython.com/chatgpt-api-python/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=ollama-python
LangGraph: Build Stateful AI Agents in Pythonhttps://realpython.com/langgraph-python/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=ollama-python
Python GUI Programming: Your Tkinter Tutorialhttps://realpython.com/python-gui-tkinter/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=ollama-python
Primer on Python Decoratorshttps://realpython.com/primer-on-python-decorators/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=ollama-python
https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Follama-python%2F
Continue »https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Follama-python%2F
Sign-Inhttps://realpython.com/account/login/?next=/ollama-python/
Start Herehttps://realpython.com/start-here/
Learning Resourceshttps://realpython.com/search
Code Mentorhttps://realpython.com/mentor/
Python Referencehttps://realpython.com/ref/
Python Cheat Sheethttps://realpython.com/cheatsheets/python/
Support Centerhttps://support.realpython.com/
Learning Pathshttps://realpython.com/learning-paths/
Quizzes & Exerciseshttps://realpython.com/quizzes/
Browse Topicshttps://realpython.com/tutorials/all/
Live Courseshttps://realpython.com/live/
Bookshttps://realpython.com/books/
Podcasthttps://realpython.com/podcasts/rpp/
Newsletterhttps://realpython.com/newsletter/
Community Chathttps://realpython.com/community/
Office Hourshttps://realpython.com/office-hours/
Learner Storieshttps://realpython.com/learner-stories/
Plans & Pricinghttps://realpython.com/account/join/
Team Planshttps://realpython.com/account/join-team/
For Businesshttps://realpython.com/account/join-team/inquiry/
For Schoolshttps://realpython.com/account/join-team/education-inquiry/
Reviewshttps://realpython.com/learner-stories/
About Ushttps://realpython.com/about/
Teamhttps://realpython.com/team/
Mission & Valueshttps://realpython.com/mission/
Editorial Guidelineshttps://realpython.com/editorial-guidelines/
Sponsorshipshttps://realpython.com/sponsorships/
Careershttps://realpython.workable.com
Press Kithttps://realpython.com/media-kit/
Merchhttps://realpython.com/merch
https://www.youtube.com/realpython
https://x.com/realpython
https://www.linkedin.com/company/realpython-com
https://www.facebook.com/LearnRealPython
https://github.com/realpython/
Privacy Policyhttps://realpython.com/privacy-policy/
Terms of Usehttps://realpython.com/terms/
Securityhttps://realpython.com/security/
Contacthttps://realpython.com/contact/
https://realpython.com/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.