[Fixed]-Unable to import custom module to other application (Django)


That is because you are trying to import UserM from user_profile module and not user_profile.models. You can either import everything to your user_profile module’s __init__.py like following:

# add this to user_profile/__init__.py
from .models import *

or use the import statement as follows:

from user_profile.models import UserM


user_profile is a directory and as python docs suggest:

The init.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, init.py can just be an empty file, but it can also execute initialization code for the package or set the all variable, described later.

So user_profile is a directory not a module. By adding an __init__.py file to it you let python know that this is a containing package. But in from user_profile import UserM you are not specifying the module that contains the UserM class so python is going to look for this in the containing packages __init__.py.

With above suggested solutions you either import your UserM class to the __init__.py file, where python is looking it for according to the from user_profile import .. statement or you change the import statement and specify the module that contains your class by modifying it to from user_profiles.modesl import UserM.


Leave a comment