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

时间:2018-04-13 16:03:15

标签: python django heroku pythonpath

我正在尝试首次在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'

应用结构

enter image description here

我尝试了各种各样的方法,让它在<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/

有人可以指出我正确的方向吗?

2 个答案:

答案 0 :(得分:1)

为什么不在现有的PYTHONPATH中放置UsefulFunction,而不是编辑PYTHONPATH来包含文件? Django应用程序通常具有utils模块(utils.py)用于此类事情。如果它特定于您的用户应用,则可以将其放在users/utils.py,但如果您更需要它,则可以在应用之外,在项目的manage.py旁边。

那就是说,如果你想把它保留在项目根目录和app dirs之外,我注意到UsefulFunctions没有.py扩展名,所以你必须把它变成一个.py文件。至少

答案 1 :(得分:0)

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

from UsefulFunctions.dbUtils import *
由于我的UsefulFunctions变量已重置,因此Python无法找到PYTHONPATH。所以,我将其改为绝对导入:

from lib.UsefulFunctions.dbUtils import *

这篇文章帮助:

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