One of the foundational skills in Django development is knowing how to import functions properly. Whether you’re building a simple blog or a complex web app, importing functions correctly keeps your code clean, maintainable, and scalable.
In this article, I’ll walk you through the essential ways to import functions in Python Django projects. I’ll share practical examples and tips from my own experience to help you avoid common pitfalls and write better Django code.
Let’s get in!
Methods to Import Functions in Django
Django projects can quickly grow large, with multiple apps and modules. Importing functions allows you to:
- Reuse code across different files and apps
- Keep your views, models, and utilities organized
- Avoid duplication and improve maintainability
If you’re working on a Django app for a U.S.-based business, for example, importing utility functions like data validators or custom email senders across apps can save you hours.
Read Create an Email Sender App in Django
Method 1: Import Functions Within the Same App
When your function is in the same Django app, importing is straightforward. Suppose you have a function in utils.py inside your app folder named sales.
Project structure:
myproject/
│
└── sales/
├── __init__.py
├── views.py
├── utils.pyutils.py:
# sales/utils.py
def calculate_discount(price, discount_percent):
"""Calculate discounted price."""
return price - (price * discount_percent / 100)views.py:
# sales/views.py
from django.http import HttpResponse
from .utils import calculate_discount
def discount_view(request):
original_price = 100
discount = 20
discounted_price = calculate_discount(original_price, discount)
return HttpResponse(f"Discounted price is ${discounted_price}")Here, the from .utils import calculate_discount line imports the function from the same app using a relative import (the dot . means current package). This method keeps your imports clean and explicit.
You can see the output in the screenshot below.

Check out Get HTML Code from Rich Text in Python Django
Method 2: Import Functions From Another App
In real-world Django projects, you often need to import functions from other apps. Let’s say you have another app called billing with a helper function you want to use in the sales app.
Project structure:
myproject/
├── sales/
│ ├── views.py
│ └── ...
└── billing/
├── utils.py
└── ...billing/utils.py:
def format_currency(amount):
"""Format number as USD currency."""
return f"${amount:,.2f}"sales/views.py:
from django.http import HttpResponse
from billing.utils import format_currency
def show_price(request):
price = 1234.56
formatted_price = format_currency(price)
return HttpResponse(f"The price is {formatted_price}")Notice here we use an absolute import with the full app name billing.utils. This approach is best practice in Django projects because it’s explicit and avoids confusion.
You can see the output in the screenshot below.

Method 3: Import Built-in and Third-Party Functions
Django projects also rely heavily on Python’s built-in functions and third-party libraries. Importing these is similar to standard Python.
For example, importing the datetime module and Django’s render shortcut:
from datetime import datetime
from django.shortcuts import render
def current_time_view(request):
now = datetime.now()
return render(request, 'time.html', {'current_time': now})Always import only what you need to keep your code clean and efficient.
Read To-Do List in Python Django
Method 4: Use__init__.py to Simplify Imports
If you want to simplify imports across your app, you can expose functions through your app’s __init__.py file.
For example, in billing/__init__.py:
from .utils import format_currencyThen, in other apps, you can import directly from billing:
from billing import format_currencyThis technique helps create a cleaner and more intuitive import structure, especially in larger projects.
Common Issues and Best Practices
- Avoid circular imports: If two modules import each other directly, it causes errors. Refactor your code to keep imports one-directional.
- Use relative imports inside apps: Use
from .module import functionfor internal app imports. - Be explicit: Avoid wildcard imports like
from utils import *as they make the code harder to read and debug. - Keep your project’s root folder in
PYTHONPATH: This ensures that absolute imports work smoothly. - Use virtual environments: They help manage dependencies cleanly and avoid conflicts.
Read Calculator App in Python Django
Full Example: Import Functions in a Simple Django Project
Here’s a minimal working example combining these concepts.
Project structure:
myproject/
├── billing/
│ ├── __init__.py
│ └── utils.py
├── sales/
│ ├── __init__.py
│ └── views.py
└── manage.pybilling/utils.py:
def format_currency(amount):
return f"${amount:,.2f}"billing/init.py:
from .utils import format_currencysales/views.py:
from django.http import HttpResponse
from billing import format_currency # Import from billing's __init__.py
def price_view(request):
price = 2599.99
formatted = format_currency(price)
return HttpResponse(f"The final price is {formatted}")This setup allows you to keep your utility functions organized and import them cleanly across your Django apps.
Mastering function imports in Django is a small but powerful step toward writing professional, maintainable web applications. With these techniques, you’ll be able to organize your code better and build scalable projects efficiently.
If you want to dive deeper into Django development, keep experimenting with your project structure and imports. Clean imports lead to clean code, which leads to successful projects.
You may read:
- Python Django “Module not found” error.
- Query in Descending and Ascending in Python Django
- Parse JSON in Python Django

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.