通过Gunicorn在Heroku上部署应用程序时〈module '__main__'上出现AttributeError

5w9g7ksd  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(107)

我是Django/Heroku的新手,但我正在尝试用这些工具来启动我的第一个项目,其中包括一个预先训练好的SK模型和自定义管道。我在本地的一切都运行得很好,但当我尝试推到Heroku时,我一直收到500个错误,我的日志尾巴在下面。我似乎无法解决它。
我编写的自定义转换器管道有一个名为FeatureSelector的类以及CategoricalTransformer,我已经将其粘贴到manage.py文件中(正如有人建议的那样,我不知道它们可能会去哪里)。Heroku或gunicorn或其他东西似乎对此有问题。

AttributeError: Can't get attribute 'FeatureSelector' on <module '__main__' from '/app/.heroku/python/bin/gunicorn'>

我尝试过重命名apps.py文件,将管道放在WSGI中,移动joblib等,但似乎没有什么对我有效。
感谢任何能够帮助我们的人。同时我已经把整个日志文件贴在了最下面,以方便任何需要的人。
文件结构:

project
       --app
           -- ml_models
              -- model.joblib
              -- transformer.joblib
           -- apps.py
           -- views.py

apps.py

from django.apps import AppConfig
from django.conf import settings
import os
from joblib import load
import warnings
warnings.filterwarnings("ignore")

class PredictorConfig(AppConfig):
    # create path to models
    predictor_path = os.path.join(settings.MODELS, 'model.joblib')
    transformer_path = os.path.join(settings.MODELS,'transformer.joblib')

    # load models into separate variables
    # these will be accessible via this class
    predictor = load(predictor_path)
    pipeline = load(transformer_path)

views.py

...
from .apps import PredictorConfig

    ...

    prediction = PredictorConfig.predict(data)
    ...

过程文件:

web: gunicorn diamonds.wsgi --log-file -

wsgi.py

from dotenv import load_dotenv
load_dotenv()
import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'diamonds.settings')

application = get_wsgi_application()

最后是settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app',
    'django_tables2',
    'django_filters',
]

整个heroku日志

2021-03-23T18:13:28.092509+00:00 app[web.1]: [2021-03-23 18:13:28 +0000] [9] [ERROR] Error handling request /
2021-03-23T18:13:28.092549+00:00 app[web.1]: Traceback (most recent call last):
2021-03-23T18:13:28.092552+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 134, in handle
2021-03-23T18:13:28.092553+00:00 app[web.1]: self.handle_request(listener, req, client, addr)
2021-03-23T18:13:28.092553+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
2021-03-23T18:13:28.092554+00:00 app[web.1]: respiter = self.wsgi(environ, resp.start_response)
2021-03-23T18:13:28.092554+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 133, in __call__
2021-03-23T18:13:28.092554+00:00 app[web.1]: response = self.get_response(request)
2021-03-23T18:13:28.092555+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/base.py", line 130, in get_response
2021-03-23T18:13:28.092556+00:00 app[web.1]: response = self._middleware_chain(request)
2021-03-23T18:13:28.092556+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 49, in inner
2021-03-23T18:13:28.092556+00:00 app[web.1]: response = response_for_exception(request, exc)
2021-03-23T18:13:28.092557+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 103, in response_for_exception
2021-03-23T18:13:28.092558+00:00 app[web.1]: response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
2021-03-23T18:13:28.092559+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 141, in handle_uncaught_exception
2021-03-23T18:13:28.092559+00:00 app[web.1]: callback, param_dict = resolver.resolve_error_handler(500)
2021-03-23T18:13:28.092559+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/urls/resolvers.py", line 602, in resolve_error_handler
2021-03-23T18:13:28.092560+00:00 app[web.1]: callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
2021-03-23T18:13:28.092560+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__
2021-03-23T18:13:28.092560+00:00 app[web.1]: res = instance.__dict__[self.name] = self.func(instance)
2021-03-23T18:13:28.092561+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
2021-03-23T18:13:28.092561+00:00 app[web.1]: return import_module(self.urlconf_name)
2021-03-23T18:13:28.092562+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
2021-03-23T18:13:28.092562+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-03-23T18:13:28.092562+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
2021-03-23T18:13:28.092563+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
2021-03-23T18:13:28.092563+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
2021-03-23T18:13:28.092563+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
2021-03-23T18:13:28.092564+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 728, in exec_module
2021-03-23T18:13:28.092564+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-03-23T18:13:28.092564+00:00 app[web.1]: File "/app/diamonds/urls.py", line 18, in <module>
2021-03-23T18:13:28.092565+00:00 app[web.1]: from detail_page.views import detail_view
2021-03-23T18:13:28.092565+00:00 app[web.1]: File "/app/detail_page/views.py", line 4, in <module>
2021-03-23T18:13:28.092565+00:00 app[web.1]: from .apps import PredictorConfig
2021-03-23T18:13:28.092565+00:00 app[web.1]: File "/app/detail_page/apps.py", line 9, in <module>
2021-03-23T18:13:28.092566+00:00 app[web.1]: class PredictorConfig(AppConfig):
2021-03-23T18:13:28.092566+00:00 app[web.1]: File "/app/detail_page/apps.py", line 18, in PredictorConfig
2021-03-23T18:13:28.092566+00:00 app[web.1]: pipeline = load(transformer_path)
2021-03-23T18:13:28.092567+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 585, in load
2021-03-23T18:13:28.092567+00:00 app[web.1]: obj = _unpickle(fobj, filename, mmap_mode)
2021-03-23T18:13:28.092567+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 504, in _unpickle
2021-03-23T18:13:28.092568+00:00 app[web.1]: obj = unpickler.load()
2021-03-23T18:13:28.092568+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/pickle.py", line 1088, in load
2021-03-23T18:13:28.092568+00:00 app[web.1]: dispatch[key[0]](self)
2021-03-23T18:13:28.092569+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/pickle.py", line 1385, in load_stack_global
2021-03-23T18:13:28.092569+00:00 app[web.1]: self.append(self.find_class(module, name))
2021-03-23T18:13:28.092569+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/pickle.py", line 1428, in find_class
2021-03-23T18:13:28.092570+00:00 app[web.1]: return _getattribute(sys.modules[module], name)[0]
2021-03-23T18:13:28.092570+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/pickle.py", line 299, in _getattribute
2021-03-23T18:13:28.092570+00:00 app[web.1]: .format(name, obj)) from None
2021-03-23T18:13:28.092571+00:00 app[web.1]: AttributeError: Can't get attribute 'FeatureSelector' on <module '__main__' from '/app/.heroku/python/bin/gunicorn'>
2021-03-23T18:13:28.093025+00:00 app[web.1]: 10.79.236.200 - - [23/Mar/2021:18:13:28 +0000] "GET / HTTP/1.1" 500 0 "-" "-"
2021-03-23T18:13:30.688831+00:00 app[web.1]: [2021-03-23 18:13:30 +0000] [10] [ERROR] Error handling request /favicon.ico
2021-03-23T18:13:30.688874+00:00 app[web.1]: Traceback (most recent call last):
2021-03-23T18:13:30.688876+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 134, in handle
2021-03-23T18:13:30.688877+00:00 app[web.1]: self.handle_request(listener, req, client, addr)
2021-03-23T18:13:30.688877+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
2021-03-23T18:13:30.688877+00:00 app[web.1]: respiter = self.wsgi(environ, resp.start_response)
2021-03-23T18:13:30.688878+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 133, in __call__
2021-03-23T18:13:30.688878+00:00 app[web.1]: response = self.get_response(request)
2021-03-23T18:13:30.688879+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/base.py", line 130, in get_response
2021-03-23T18:13:30.688880+00:00 app[web.1]: response = self._middleware_chain(request)
2021-03-23T18:13:30.688880+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 49, in inner
2021-03-23T18:13:30.688881+00:00 app[web.1]: response = response_for_exception(request, exc)
2021-03-23T18:13:30.688881+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 103, in response_for_exception
2021-03-23T18:13:30.688882+00:00 app[web.1]: response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
2021-03-23T18:13:30.688883+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 141, in handle_uncaught_exception
2021-03-23T18:13:30.688883+00:00 app[web.1]: callback, param_dict = resolver.resolve_error_handler(500)
2021-03-23T18:13:30.688884+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/urls/resolvers.py", line 602, in resolve_error_handler
2021-03-23T18:13:30.688885+00:00 app[web.1]: callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
2021-03-23T18:13:30.688885+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__
2021-03-23T18:13:30.688886+00:00 app[web.1]: res = instance.__dict__[self.name] = self.func(instance)
2021-03-23T18:13:30.688886+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
2021-03-23T18:13:30.688887+00:00 app[web.1]: return import_module(self.urlconf_name)
2021-03-23T18:13:30.688887+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
2021-03-23T18:13:30.688887+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-03-23T18:13:30.688888+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
2021-03-23T18:13:30.688888+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
2021-03-23T18:13:30.688889+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
2021-03-23T18:13:30.688889+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
2021-03-23T18:13:30.688890+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 728, in exec_module
2021-03-23T18:13:30.688890+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-03-23T18:13:30.688890+00:00 app[web.1]: File "/app/diamonds/urls.py", line 18, in <module>
2021-03-23T18:13:30.688891+00:00 app[web.1]: from detail_page.views import detail_view
2021-03-23T18:13:30.688891+00:00 app[web.1]: File "/app/detail_page/views.py", line 4, in <module>
2021-03-23T18:13:30.688891+00:00 app[web.1]: from .apps import PredictorConfig
2021-03-23T18:13:30.688892+00:00 app[web.1]: File "/app/detail_page/apps.py", line 9, in <module>
2021-03-23T18:13:30.688892+00:00 app[web.1]: class PredictorConfig(AppConfig):
2021-03-23T18:13:30.688892+00:00 app[web.1]: File "/app/detail_page/apps.py", line 18, in PredictorConfig
2021-03-23T18:13:30.688893+00:00 app[web.1]: pipeline = load(transformer_path)
2021-03-23T18:13:30.688893+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 585, in load
2021-03-23T18:13:30.688894+00:00 app[web.1]: obj = _unpickle(fobj, filename, mmap_mode)
2021-03-23T18:13:30.688894+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 504, in _unpickle
2021-03-23T18:13:30.688895+00:00 app[web.1]: obj = unpickler.load()
2021-03-23T18:13:30.688895+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/pickle.py", line 1088, in load
2021-03-23T18:13:30.688896+00:00 app[web.1]: dispatch[key[0]](self)
2021-03-23T18:13:30.688896+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/pickle.py", line 1385, in load_stack_global
2021-03-23T18:13:30.688896+00:00 app[web.1]: self.append(self.find_class(module, name))
2021-03-23T18:13:30.688897+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/pickle.py", line 1428, in find_class
2021-03-23T18:13:30.688897+00:00 app[web.1]: return _getattribute(sys.modules[module], name)[0]
2021-03-23T18:13:30.688897+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/pickle.py", line 299, in _getattribute
2021-03-23T18:13:30.688898+00:00 app[web.1]: .format(name, obj)) from None
2021-03-23T18:13:30.688898+00:00 app[web.1]: AttributeError: Can't get attribute 'FeatureSelector' on <module '__main__' from '/app/.heroku/python/bin/gunicorn'>
juzqafwq

juzqafwq1#

您是否已先正确设定项目?方法如下:https://devcenter.heroku.com/articles/django-app-configuration此外,在问题中加入更多的错误或其他信息也会有所帮助。

相关问题