在Django中导入自定义模块“No Module Named Custom\u Module”E

2024-04-29 12:50:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个简单的模块,需要将其导入到我的Django设置文件中。在

导入发生在libraries字典的以下部分:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),

            #re-route the search for templates into this custom template directory
            os.path.join(os.path.join(BASE_DIR, 'templates'), 'tshirt-theme'),

            #Uncomment the line below to restore the original Oscar template
            #OSCAR_MAIN_TEMPLATE_DIR,
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.i18n',
                'django.contrib.messages.context_processors.messages',

                'oscar.apps.search.context_processors.search_form',
                'oscar.apps.promotions.context_processors.promotions',
                'oscar.apps.checkout.context_processors.checkout',
                'oscar.apps.customer.notifications.context_processors.notifications',
                'oscar.core.context_processors.metadata',
            ],
            'libraries': {
                'promotion_tags': 'custom_oscar.templatetags.promotion_tags'
            }
        },
    },
]

问题是我遇到了一个错误。这里是:

InvalidTemplateLibrary at /

Invalid template library specified. ImportError raised when trying to load 'custom_oscar.templatetags.promotion_tags': No module named custom_oscar.templatetags.promotion_tags

代码可以在本地机器上运行,但当我将其迁移到生产环境(Ubuntu 16.01)时就失败了。在

我的项目结构如下:

^{pr2}$

最初我怀疑问题可能出在路径上,所以我检查了跟踪,正确的路径似乎包括在内:

Python路径:

['/var/www/vhosts/book/book-site',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/var/www/vhosts/book/book-env/lib/python3.5/site-packages']

有人能告诉我为什么会发生这个错误吗?我怎样才能修好它?在

如果有帮助的话,我可以在手动激活virtualenv时从命令行成功导入这个模块。在


Tags: appspathdjangooslibusrcustomcontext