question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model imports shown, but giving NameError on usage.

See original GitHub issue

Version:

  • django-extensions==2.2.8
  • Django==3.0.2
  • ipython==7.13.0

Sample output:

 (2004)-> ./manage.py shell_plus --ipython
[TerminalIPythonApp] WARNING | File not found: '/home/ash/.pythonrc'
# Shell Plus Model Imports
from allauth.account.models import EmailAddress, EmailConfirmation
from allauth.socialaccount.models import SocialAccount, SocialApp, SocialToken
from catalogue.models import Brand, Category, Product, ProductImage
from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.contrib.sessions.models import Session
from django.contrib.sites.models import Site
from orders.models import LineItem, Order, Review
from rest_framework.authtoken.models import Token
from users.models import User
# Shell Plus Django Imports
from django.core.cache import cache
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models import Avg, Case, Count, F, Max, Min, Prefetch, Q, Sum, When, Exists, OuterRef, Subquery
from django.utils import timezone
from django.urls import reverse
Python 3.8.2 (default, Feb 26 2020, 04:23:39) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
[TerminalIPythonApp] WARNING | File not found: '/home/ash/.pythonrc'

In [1]: User                                                                                                                    
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-1974857568df> in <module>
----> 1 User

NameError: name 'User' is not defined

Also, in my settings I already have this:

    # Custom user app
    AUTH_USER_MODEL = 'users.User'
# settings/local.py
    INSTALLED_APPS += ('django_nose', 'django_extensions',)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
washeckcommented, Apr 20, 2020

FYI, the problem is gone in our project since we removed django_nose. I tried to reproduced the issue in a minimal project with django_extensions, ipython and django_nose but it works as expected so there’s something more needed to reproduce the issue.

0reactions
trbscommented, May 11, 2020
...
Successfully installed CairoSVG-2.4.2 Django-2.2.12 O365-2.0.8 Pillow-7.0.0 Pygments-2.5.2 Pyphen-0.9.5 WeasyPrint-51 appdirs-1.4.3 appnope-0.1.0 attrs-19.3.0 backcall-0.1.0 beautifulsoup4-4.9.0 black-19.10b0 boto3-1.12.8 botocore-1.15.8 cairocffi-1.1.0 certifi-2019.11.28 cffi-1.14.0 chardet-3.0.4 click-7.1.1 colorama-0.4.3 cryptography-2.8 cssselect2-0.2.2 cycler-0.10.0 decorator-4.4.1 defusedxml-0.6.0 dj-database-url-0.5.0 django-dotenv-1.4.2 django-extensions-2.2.8 django-mysql-3.3.0 django-rest-knox-4.1.0 django-rest-passwordreset-1.1.0 djangorestframework-3.11.0 docutils-0.15.2 dropbox-9.4.0 et-xmlfile-1.0.1 gunicorn-20.0.4 html5lib-1.0.1 idna-2.9 ipdb-0.12.3 ipython-7.13.0 ipython-genutils-0.2.0 jdcal-1.4.1 jedi-0.17.0 jmespath-0.9.5 kiwisolver-1.1.0 matplotlib-3.1.3 mysqlclient-1.4.6 numpy-1.18.1 oauthlib-3.1.0 openpyxl-3.0.3 pandas-0.25.3 parso-0.7.0 pathspec-0.7.0 pexpect-4.8.0 pickleshare-0.7.5 prompt-toolkit-3.0.3 psutil-5.7.0 ptyprocess-0.6.0 pycparser-2.19 pymsteams-0.1.12 pyparsing-2.4.6 python-dateutil-2.8.1 pytz-2019.3 regex-2020.2.20 requests-2.23.0 requests-oauthlib-1.3.0 s3transfer-0.3.3 sentry-sdk-0.14.2 six-1.14.0 soupsieve-2.0 sqlparse-0.3.0 stringcase-1.2.0 stripe-2.43.0 tinycss2-1.0.2 toml-0.10.0 traitlets-4.3.3 typed-ast-1.4.1 tzlocal-2.0.0 urllib3-1.25.8 wcwidth-0.1.8 webencodings-0.5.1 xlrd-1.2.0
$ django-admin startproject abz
$ cd abz
$ vi abz/settings.py
$ ./manage.py shell_plus
# Shell Plus Model Imports
from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import Group, Permission, User
from django.contrib.contenttypes.models import ContentType
from django.contrib.sessions.models import Session
# Shell Plus Django Imports
from django.core.cache import cache
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models import Avg, Case, Count, F, Max, Min, Prefetch, Q, Sum, When, Exists, OuterRef, Subquery
from django.utils import timezone
from django.urls import reverse
Python 3.7.7 (default, Mar 10 2020, 15:43:33)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: User
Out[1]: django.contrib.auth.models.User

In [2]:

Works fine for me.

Please make sure to test this stuff and verify it’s reproducible. There tons of things that might be “different” or missing in configuration in my sample project making it not fail for me.

Alternatively, if somebody (eg; you are a company who is having this issue in your project) is willing to sponsor this ticket, I can take a look at the project to fix it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NameError in Django model? Why Cant I reference ...
It is the order in which the classes are defined. Python interpreter does not do a forward lookup This should fix your issue....
Read more >
NameError: Name plot_cases_simple is Not Defined
In Python, the NameError occurs when you try to use a variable, function, or module that doesn't exist or wasn't used in a...
Read more >
How to Fix: NameError name 'np' is not defined
This error occurs when you import the python library NumPy, but fail to give it the alias of np when importing it. The...
Read more >
NameError: name 'pandas' is not defined – How To Fix
Pandas NameError can be annoying. It generally means you have not imported pandas, or if you did, you're not calling the right name...
Read more >
ModuleNotFoundError: No module named x
tl;dr. Use absolute imports; Append your project's root directory to PYTHONPATH — In any environment you wish to run your Python application ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found