You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django跨应用导入模型失败:找不到project.subscription模块

Fixing "no module named project.subscription" in Django

Hey John, let's work through this import issue together—this is a common gotcha when setting up Django app dependencies. Here are the key steps to resolve it:

1. Ensure subscription is registered in INSTALLED_APPS

First, double-check your project's settings.py file (in the root project directory). The subscription app needs to be listed in INSTALLED_APPS so Django recognizes it as a valid app:

INSTALLED_APPS = [
    # Default Django apps (like 'django.contrib.admin', etc.)
    'accounts',
    'subscription',  # Make sure this line exists!
    'PROJECTAPP',
    'basiclinks',
]

Don't use project.subscription here—just the app name is enough.

2. Verify __init__.py exists in the subscription directory

Django relies on empty __init__.py files to identify directories as Python packages. Check if your subscription folder has this file. If not, create a blank __init__.py file inside it—this tells Python to treat the directory as a module.

3. Adjust your import statement

Your original import uses project.subscription.models, but this is likely unnecessary (and causing the error). Try one of these more reliable options:

Since subscription is registered in INSTALLED_APPS, Django adds the app directory to Python's path automatically. You can import directly from the app name:

from subscription.models import Package

Option B: Relative Import (For intra-project files)

If you're importing from a file inside the accounts app, you can use a relative path (note: this only works within module files, not standalone scripts):

from ..subscription.models import Package

The two dots (..) mean "go up one directory from the current file's location".

4. Confirm you're running commands from the correct directory

Make sure you're executing Django commands (like manage.py runserver or manage.py shell) from the root project directory—the one that contains manage.py. If you run commands from a subdirectory, Python might not be able to locate your app modules.

To test this, open a Django shell and run:

import sys
print(sys.path)

Your root project directory should be listed in the output. If it's not, you can add it manually (though fixing your working directory is better):

import os
sys.path.append(os.path.abspath('.'))

5. Check for case sensitivity issues

If you're on a Linux or macOS system, Python is case-sensitive. Double-check that your import uses the exact lowercase subscription (matching your directory name)—avoid typos like Subscription or SUBscription.


内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 17:57:44