Django ImportError: cannot import from models - python

I have a utils.py file in an app directory at the same level as models.py. I am trying to import a class (a model) in utils.py:
from models import TeamConstraint
My IDE does not report any conflict, however when running the server I am getting an ImportError:
ImportError: cannot import name TeamConstraint
I tried checking for circular dependencies, there is a file tasks.py also in the same level as the rest that imports from utils.py:
from scheduler.utils import current_indie_teams, matchcount_by_week
Although I don't understand why here scheduler needs to be included explicitly, while when trying to import a model it is not required (or so says PyCharm at least).
I am trying to figure out how to solve this.

When you say from models import TeamConstraint it's not clear what models is.
It could be a different app with the name models or it could be the file in the same folder. It's better to be explicit than implicit.
Try using an absolute import from scheduler.models import TeamConstraint or if you really want to use a relative import do so as such from .models import TeamConstraint (Notice the . before models)
Also it seems like you are using Python3 but your PyCharm is configured for Python2, else it would warn you.

whenever you get import errors, try delaying one of the imports.
here place 'from models import TeamConstraint' in class or function instead.

Related

How can I debug my Django models to see what the properties contain?

I created a model in a Django application. Let's say I have a model class Car in models.py. This class has a method called refuel() that interacts with the class' properties.
I want to write a Python script that imports models.py, instanciates the class and calls refuel() so to then debug the class using Breakpoints with my IDE.
However, for some reason, I cannot import the model file in another script. When I use this syntax to import the file:
from .models import Car
I get this error:
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package
Note that the script I import the model in is located in the same folder as models.py.
What's the way of solving this problem, or, more broadly, what's the best way of debugging my model file ?
Thanks.

Search path for Pyramid modules

I have a Pyramid app, I split models AND views into separate files in following manner:
How do I split models.py into different files for different models in Pyramid?
One small consequence for views is that since I have them in separate view files in a "views" package is that they cannot find models.py, now models package, since it resides in parent directory.
That is, it used to be:
models.py
views.py
Now it's:
views/__init__.py
views/view1.py
views/view2.py
models/__init__.py
models/model1.py
models/model2.py
Therefore, importing from models in a view results in:
from models import (
ImportError: No module named models
Now, I can work around this by adding following path search module extension in views/__init__.py:
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
But that's kind of kludgy. Is there a better, maybe Pyramid-specific solution?
This is not really pyramid specific, it is just python.
See https://docs.python.org/2/tutorial/modules.html#packages
for a detailed explanation.
You want to
import models.model1
Or
from models.model2 import (...)
I'm late, but it can help some other person.
For solve this, you need to specify your project name. In your case, if the project name is projet_name import your models like this.
from project_name.models.model1 import *
or
import project_name.models.model1
I suppose is not the best way, but it work fine for me.

Model imports failing between apps in django (circular imports?)

I am having problems importing models between my apps in my current django project. When trying to run the dev server or sync the database, I receive the error:
File "/path/to/Project/../Project/app1/models.py", line 3, in <module>
class SomeModel(Thing):
NameError: name 'Thing' is not defined
Thing is defined in Project/main/models.py which I import into Project/app1/models.py via:
from project.main.models import Thing
I am aware that this sort of thing can be the result of python preventing circular imports.
Currently I have three django apps: main, app1, app2
The imports for each app are like so:
main:
from project.app1.models import AnotherThing
app1:
from project.main.models import Thing
app2:
from project.main.models import Thing
from project.app1 import Something
I suspect that importing models from app1 into app2 is causing the problem, since both of those apps import models from main, which in turn imports models from app1, etc.
If this is the case - what other methods can I use to achieve the same effect as these imports?
Is there a more accepted way of organizing things so that I won't run into this?
If you want to subclass those models, you need to remove circular dependency. Refactor common stuff into another module, and make those two import that instead of each other. If the model is only used as a foreign key, you can use string instead of an object to defer the import (e.g. ForeignKey('app.Model') — see documentation for details).

How can I import a specific module with an ambiguous name?

This happens in Django context, but the question is not Django specific.
I'm trying to split my big urls.py into smaller and more manageable chunks (app includes, third-party modules, search, admin, development and so on). So instead of urls.py, I'm using urls/ directory now, with urls/__init__.py containing code like this:
import admin
urlpatterns += admin.urlpatterns
Now I've got into trouble with third-party modules. For example, I'm overriding a bunch of URLs and views for django-messages, and so I've put all of it into urls/messages.py. It turns out, I can't import django-messages' views then, since
from messages.views import inbox
inside urls/messages.py tries to import from local directory first, finds a messages module, but can't import messages.views then, since urls/messages/views.py is obviously absent. I wouldn't like having to rename my modules to something like urls.messages_urls, but I haven't found a way to explicitely tell Python to import the "other" messages.views. Neither would I like to specify my imported views via strings, even though I can do that in Django.
How can I solve this situation cleanly?
This is the wrong way to do it.
Django has a method for splitting urls.py into separate files, and that is to use include(). Keep the urls for each application in its own urls.py, and include them into the main one.
For those who could not find it, I was hitting ambiguous import errors. For example, in a django project, let's say I have this tree :
tax module
models module
init.py
tax.py
foo.py
In init.py, I want to import tax.models.tax.Tax.
So I write :
from tax.models.tax import Tax
But python does not find the correct import (it looks for models inside tax.models) and throws
ImportError: No module named models
You have indeed understood that it's all about relative imports. python first looks in the current module. If the first part of your import exists in the current module, it fails.
This behaviour has been fixed in Python 2.5 (and may have become default in Python 3.0), you just have to add :
from __future__ import absolute_import
before your absolute imports.
Have a look at Python 2.5 introduces absolute imports for more thorough information.
Have you tried:
from django.contrib.messages.views import inbox
Untested, but I'd think the full import would work as expected.

Django signals file, cannot import model names

I have such file order:
project/
app/
models.py
signals.py
I am keeping signals inside signals.py as it should be. and at the top of the signals.py file, I include myapp models as I do queries in these signals with
from myproject.myapp.models import Foo
However it doesnt seem to find it, as I run the server or validate from manage.py, it gives this error:
from myproject.myapp.models import Foo
ImportError: cannot import name Foo
I am using Django 1.2.1.
Most likely you have a circular dependency. Does your models.py import the signals? If so, this can't work as both modules now depend on each other. You may need to import the models within a function in the signals file, rather than at the top level.

Categories

Resources