Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django cannot import name x [duplicate]

People also ask

How do you resolve ImportError Cannot import name?

Solution 1Check the import class in the python file. If this is not available, create a class in the python file. The python interpreter loads the class from the python file. This will resolve the “ImportError: Can not Import Name” error.

Can not import python?

In Python "ImportError: cannot import name" error generally occurs when the imported class is not accessible, or the imported class is in a circular dependency. The following are the major reasons for the occurrence of "ImportError: cannot import name": The imported class is in a circular dependency.

How do you fix ImportError Cannot import name in Python?

The Python "ImportError: cannot import name" occurs when we have circular imports (importing members between the same files). To solve the error, move the objects to a third file and import them from a central location in other files, or nest one of the imports in a function.


There is a circular import in your code, that's why the Item can't be imported in action.

You can solve the problem by removing the import of a class in one of your files, and replacing it with a string containing the name of the class, as explained in the documentation. For example :

effects = models.ManyToManyField('effects.Effect',through='ItemEffect',blank=True)

Like madjar suggested, there is likely a circular import in your code. If you're having trouble finding out where the circle is (which modules and imports are involved), you can use the traceback option to get an idea of where the problem lies:

python manage.py validate --traceback

Edit - Validate is deprecated from django 1.7. So please run the following command -

python manage.py check --traceback

Try to import Locally your model instead of as public one, Example

def sample_function():
    from effects.models import Effect # import within function or class

or import model as String -> 'APP_NAME.MODEL_NAME'

pay_methods = models.ManyToManyField('payment_app.AllowedPayMethod')

This was the first post that came up on Google so I will post this alternative cause of error.

In my code there was no circular imports, I solved this problem by manually deleting all .pyc files in my project. Apparently restarting the application wasn't recompiling my code.