Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Statement Inconsistencies

Tags:

python

django

Why does the 2nd statement below not work even though it is functionally equivalent to the 1st?

from django.shortcuts import render # works
import django.shortcuts.render as render # doesn't work

like image 562
NoName Avatar asked May 22 '26 04:05

NoName


1 Answers

The error is telling you why: render isn't a module.

An example that might make it clearer - lets try to import a function from the math module:

>>> import math.pow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pow
>>> from math import pow
>>>

The pow function isn't a module, but the symbol (i.e. the function) can be imported from the module (math).

render() in django.shortcuts is a function. It's not a module.

like image 128
MatsLindh Avatar answered May 23 '26 18:05

MatsLindh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!