import them import sys import django pathname = os.path.dirname(os.path.abspath(__file__)) sys.path.append(pathname) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MxShop.settings") django.setup() from apps.users.models import UserProfile if __name__ == "__main__": user = UserProfile.objects.all() for i in user: print(i.name,i.gender,i.email)
The key point is that the root directory of the project needs to be set to the module directory.