---
title: Why You Shouldn’t Import Functions and Classes Directly in Python
description: Today I was trying to finish up an old project that was 80% done, but I never finished it. Why? I discovered a valuable lesson you should know.
---

[Michael Washburn Jr's Blog ](https://michaelwashburnjr.com/blog)

# [Why You Shouldn’t Import Functions and Classes Directly in Python](https://michaelwashburnjr.com/blog/shouldnt-import-functions-classes-directly-python)

 Written by [Michael Washburn Jr](https://michaelwashburnjr.com/blog/author/michael-washburn-jr) | Sep 23, 2017 4:52:24 PM

Today I was doing some programming, trying to finish up an old project that was 80% done, but I never finished. The project is an API for sharing beer ingredient data, being the avid homebrewer that I am. I was in the middle of writing some pretty simple authentication logic, when I found a weird bug in my code. The code, featured below, was pretty straightforward, or so I thought.

```
from django.shortcuts import render
from rest_framework.authentication import SessionAuthentication
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer
from django.contrib.auth import authenticate, login
from rest_framework import status
from rest_framework.decorators import api_view

@api_view(['POST'])
def login(request):
    username = request.data['username']
    password = request.data['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        login(request, user)
        # I haven't gotten this far yet
    else:
        content = {'error': 'Invalid Credentials'}
        return Response(content, status=status.HTTP_401_UNAUTHORIZED)
```

I was reminded of a lesson I’ve learned before the hard way today when I got a mysterious error in my terminal.

```
TypeError: login() takes 1 positional argument but 2 were given.
```

The Django docs (which are awesome, by the way) [clearly state the login function takes two positional arguments](https://docs.djangoproject.com/en/1.11/topics/auth/default/#django.contrib.auth.login). So what the heck is wrong with this code?

Well, this is a friendly reminder from me to never directly import functions and classes from packages. Import the parent package, not the function. Otherwise, you might end up importing a function with the same name as a function you’ve already defined…

To be clear, the error I was getting was because I had already defined a “login” function. Which means when I tried calling Django’s login function, I was actually making a recursive call to my own function, unsuccessfully albeit.

Had I only imported django.contrib.auth, I would have saved myself 10 minutes of time that I spent staring dumbfounded at my screen.

[View full post](https://michaelwashburnjr.com/blog/shouldnt-import-functions-classes-directly-python)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Michael Washburn Jr"
  },
  "dateModified" : "2021-04-23T17:29:12.101Z",
  "datePublished" : "2017-09-23T16:52:24Z",
  "headline" : "Why You Shouldn’t Import Functions and Classes Directly in Python",
  "image" : {
    "@type" : "ImageObject",
    "height" : 439,
    "url" : "https://f.hubspotusercontent20.net/hubfs/4674423/Imported_Blog_Media/capture-2.jpg",
    "width" : 995
  },
  "mainEntityOfPage" : "https://michaelwashburnjr.com/blog/shouldnt-import-functions-classes-directly-python",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60,
      "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
      "width" : 60
    },
    "name" : "michaelwashburnjr.com"
  }
}
```