Heroku 上的 Django 应用程序 - PythonPath 问题

Django app on Heroku - PythonPath Issue

我第一次尝试在 Heroku 上配置我的 django 应用程序。我已经到了将代码推送到 "heroku master" 的地步,但我收到 ModuleNotFound 错误:

ModuleNotFoundError: No module named 'UsefulFunctions'

这是完整的跟踪:

$ git push heroku master
Counting objects: 390, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (251/251), done.
Writing objects: 100% (390/390), 56.56 KiB | 2.83 MiB/s, done.
Total 390 (delta 205), reused 262 (delta 122)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-3.6.4
remote: -----> Installing pip
remote: -----> Installing requirements with pip
remote:        Collecting django (from -r /tmp/build_9b6fcbcef79c70d29d429c430e653020/requirements.txt (line 1))
remote:          Downloading Django-2.0.4-py3-none-any.whl (7.1MB)
remote:        Collecting psycopg2 (from -r /tmp/build_9b6fcbcef79c70d29d429c430e653020/requirements.txt (line 2))
remote:          Downloading psycopg2-2.7.4-cp36-cp36m-manylinux1_x86_64.whl (2.7MB)
remote:        Collecting python-decouple (from -r /tmp/build_9b6fcbcef79c70d29d429c430e653020/requirements.txt (line 3))
remote:          Downloading python-decouple-3.1.tar.gz
remote:        Collecting django-debug-toolbar (from -r /tmp/build_9b6fcbcef79c70d29d429c430e653020/requirements.txt (line 4))
remote:          Downloading django_debug_toolbar-1.9.1-py2.py3-none-any.whl (206kB)
remote:        Collecting Pillow (from -r /tmp/build_9b6fcbcef79c70d29d429c430e653020/requirements.txt (line 5))
remote:          Downloading Pillow-5.1.0-cp36-cp36m-manylinux1_x86_64.whl (2.0MB)
remote:        Collecting pytz (from django->-r /tmp/build_9b6fcbcef79c70d29d429c430e653020/requirements.txt (line 1))
remote:          Downloading pytz-2018.4-py2.py3-none-any.whl (510kB)
remote:        Collecting sqlparse>=0.2.0 (from django-debug-toolbar->-r /tmp/build_9b6fcbcef79c70d29d429c430e653020/requirements.txt (line 4))
remote:          Downloading sqlparse-0.2.4-py2.py3-none-any.whl
remote:        Installing collected packages: pytz, django, psycopg2, python-decouple, sqlparse, django-debug-toolbar, Pillow
remote:          Running setup.py install for python-decouple: started
remote:            Running setup.py install for python-decouple: finished with status 'done'
remote:        Successfully installed Pillow-5.1.0 django-2.0.4 django-debug-toolbar-1.9.1 psycopg2-2.7.4 python-decouple-3.1 pytz-2018.4 sqlparse-0.2.4
remote:
remote: -----> $ python manage.py collectstatic --noinput
remote:        /app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
remote:          """)
remote:        Traceback (most recent call last):
remote:          File "manage.py", line 15, in <module>
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
remote:            django.setup()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
remote:            apps.populate(settings.INSTALLED_APPS)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
remote:            app_config.import_models()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
remote:            self.models_module = import_module(models_module_name)
remote:          File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
remote:            return _bootstrap._gcd_import(name[level:], package, level)
remote:          File "<frozen importlib._bootstrap>", line 994, in _gcd_import
remote:          File "<frozen importlib._bootstrap>", line 971, in _find_and_load
remote:          File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
remote:          File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
remote:          File "<frozen importlib._bootstrap_external>", line 678, in exec_module
remote:          File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
remote:          File "/tmp/build_9b6fcbcef79c70d29d429c430e653020/users/models.py", line 4, in <module>
remote:            from UsefulFunctions.dbUtils import *
remote:        ModuleNotFoundError: No module named 'UsefulFunctions'
remote:
remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.
remote:
remote:        You may need to update application code to resolve this error.
remote:        Or, you can disable collectstatic for this application:
remote:
remote:           $ heroku config:set DISABLE_COLLECTSTATIC=1
remote:
remote:        https://devcenter.heroku.com/articles/django-assets
remote:  !     Push rejected, failed to compile Python app.
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !       Push rejected to salty-savannah-12027.
remote:
To https://git.heroku.com/salty-savannah-12027.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/salty-savannah-12027.git'

应用程序结构

我尝试了各种方法让它在我的 <app>/lib 目录中识别我的 PYTHONPATH,但没有成功:

http://blog.sionide21.com/posts/2014/01/managing-pythonpath-on-heroku/ https://www.biggleszx.com/2012/03/your-first-heroku-django-app/

有人能给我指出正确的方向吗?

与其编辑 PYTHONPATH 以包含该文件,不如将 UsefulFunctions 放入现有的 PYTHONPATH 中? Django 应用通常通常有一个 utils 模块 (utils.py) 来处理这些事情。如果它特定于您的 Users 应用,您可以将其放在 users/utils.py,但如果您更普遍地需要它,它可以放在应用之外,在项目中的 manage.py 旁边。

就是说,如果您确实想将它保留在项目根目录和应用程序目录之外,我注意到 UsefulFunctions 没有 .py 扩展名,因此您必须将它设为 .py 文件最起码。

这原来是一个导入问题。我的原始导入行:

from UsefulFunctions.dbUtils import *

Python 找不到 UsefulFunctions 因为我的 PYTHONPATH 变量被重置了。因此,我将其更改为绝对导入:

from lib.UsefulFunctions.dbUtils import *

这篇文章帮助了:

https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html