skip to Main Content

I am having issues importing a function from a module I created into my code. I am getting an error stating that the function is not defined, despite importing the module into my file.

The error message:

something = doohickey()
NameError: name 'doohickey' is not defined

get_random_tweet.py

import twitter

api = twitter.Api(consumer_key='',
                  consumer_secret='',
                  access_token_secret='')

timeline = api.GetUserTimeline(screen_name='realDonaldTrump',
                               include_rts=False,
                               trim_user=True,
                               exclude_replies=True,
                               count=6)

def doohickey():
    pprint(timeline)
    return {'index': "<i> something </i>"}

My views.py

from django.shortcuts import render
from django.http import HttpResponse
from hello.sampled_stream import okdood
import hello.get_random_tweet
from .models import Greeting

# Create your views here.
def index(request):
   # return HttpResponse('Hello from Python!')
   # okdood()
   something = doohickey()
   return render(request, "index.html")

I have also attempted the following:

 from django.shortcuts import render
 from django.http import HttpResponse
 from hello.sampled_stream import okdood
 from hello.get_random_tweet import doohickey
 from .models import Greeting

 # Create your views here.
 def index(request):
 # return HttpResponse('Hello from Python!')
 # okdood()
 something = doohickey()
 return render(request, "index.html")

Error message:

 something = doohickey()
 NameError: name 'doohickey' is not defined

and

from django.shortcuts import render
from django.http import HttpResponse
from hello.sampled_stream import okdood
import hello.get_random_tweet
from .models import Greeting

# Create your views here.
def index(request):
# return HttpResponse('Hello from Python!')
# okdood()
something = hello.get_random_tweet.doohickey()
return render(request, "index.html")

Error message:

 something = hello.get_random_tweet.doohickey()
 NameError: name 'doohickey' is not defined

2

Answers


  1. It looks like the issue is that you are not referring to the doohickey function as part of the hello.get_random_tweet namespace. You can do this in several ways:

    from django.shortcuts import render
    from django.http import HttpResponse
    from hello.sampled_stream import okdood
    from hello.get_random_tweet import doohickey
    from .models import Greeting
    
    # Create your views here.
    def index(request):
       # return HttpResponse('Hello from Python!')
       # okdood()
       something = doohickey()
       return render(request, "index.html")
    

    or

    from django.shortcuts import render
    from django.http import HttpResponse
    from hello.sampled_stream import okdood
    import hello.get_random_tweet
    from .models import Greeting
    
    # Create your views here.
    def index(request):
       # return HttpResponse('Hello from Python!')
       # okdood()
       something = hello.get_random_tweet.doohickey()
       return render(request, "index.html")
    

    As your code is currently structured, you import the hello.get_random_tweet module, but when you refer to doohickey Python is looking for it in the local namespace. However, it should be looking for it in the hello.get_random_tweet namespace. You can either import the function and add it to the local namespace, as shown in the first snippet, or refer to the function in the imported module’s namespace as shown in the second snippet.

    Login or Signup to reply.
  2. Might be a copy/paste error, but you’re missing endquotes on a few lines here, and the closing bracket:

    api = twitter.Api(consumer_key='',
                      consumer_secret='',
                      access_token_secret=''
    

    Should be:

    api = twitter.Api(consumer_key='',
                      consumer_secret=''
                      access_token_key=''
                      access_token_secret='')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search