Reputation: 188
I have a project whith multiple apps. So when I want some code from another app I use
from app.pyfile import *
or
from app.pyfile import specific_function
To be more specific:
I have an app called 'commonapp' where I have some common stuff that I use in all the other apps. In this app I have a common.py file where I have some functions, including a function called my_response(request,template,context)
,which is the one causes a NameError. Now this particular function is always being called inside other functions.
for example:
from commonapp.common import *
def myInfo(request):
context = {}
data = ''
data = SomeModel.objects.all()
template = 'path/to/info.html'
context['data'] = data
a = my_response(request,template,context)
return a
This raises a NameError "global name my_response is not defined"
I know what a NameError is, but why here? I would expect an ImportError if something could not be imported or even "global name a is not defined" .
What am I missing here?
UPDATE:
Here is a screenshot showing where the my_response(request,template,context)
is and the file structure of the app.
Upvotes: 0
Views: 2069
Reputation: 22679
When you do
from commonapp.common import *
everything that can be imported from the package is imported into current global namespace. Apparently my_response
is not imported (for some reason).
So, you get NameError
, because my_response
is resolved at point of it's execution, i.e. in
# Python tries to lookup `my_response` in local and then global context
# But, it can't find it, thus NameError exception occurs.
a = my_response(request,template,context)
And ImportError
s are generally raised by importing routines e.g from package import non_existing_function
and are not encountered in other context.
Upvotes: 2