在部署到Heroku时出现了"ImportError: No module named 'decouple'"的错误。

9

我正在尝试使用Heroku CLI将我的Django项目部署到Heroku上。我创建了一个应用程序,然后从项目目录中运行git push heroku master命令。但是,我收到了以下错误信息:

remote: -----> $ python manage.py collectstatic --noinput 
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.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line 
remote:            utility.execute() 
remote:          File "/app/.heroku/python/lib/python3.5/site-packages/django/core/management/__init__.py", line 317, in execute 
remote:            settings.INSTALLED_APPS 
remote:          File "/app/.heroku/python/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__ 
remote:            self._setup(name) 
remote:   File "/app/.heroku/python/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup remote:            self._wrapped = Settings(settings_module) 
remote:          File "/app/.heroku/python/lib/python3.5/site-packages/django/conf/__init__.py", line 106, in __init__ 
remote:            mod = importlib.import_module(self.SETTINGS_MODULE) 
remote:          File "/app/.heroku/python/lib/python3.5/importlib/__init__.py", line 126, in import_module 
remote:            return _bootstrap._gcd_import(name[level:], package, level) 
remote:          File "<frozen importlib._bootstrap>", line 986, in _gcd_import 
remote: File "<frozen importlib._bootstrap>", line 969, in _find_and_load 
remote:          File "<frozen importlib._bootstrap>", line 958, in
_find_and_load_unlocked 
remote:          File "<frozen importlib._bootstrap>", line 673, in _load_unlocked 
remote:          File "<frozen importlib._bootstrap_external>", line 665, in exec_module 
remote:          File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed 
remote:          File "/tmp/build_93c58c906371cd0110470b6bb3ccecc1/funderpreneur/settings.py", line 15, in <module> 
remote:            from decouple import Csv, config 
remote:        ImportError: No module named 'decouple' 
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

所以,如前所述,我运行了heroku config:set DISABLE_COLLECTSTATIC=1。然后将代码推送到存储库中。然后我运行了heroku run python3 manage.py migrate。但是仍然出现相同的错误,提示:

文件“/app/funderpreneur/settings.py”的第15行

from decouple import Csv, config ImportError: No module named 'decouple'

但是我已经在本地安装了python-decouple,用于python 2和3。整个项目正在运行django版本2和python版本3.5.2。但是在runtime.txt中,我设置了python-3.6.4。我的requirements.txt中已经有了decouple

在我的settings.py文件中,我有:

import django_heroku
django_heroku.settings(locals())

但我一直在遇到错误,所以我将它们注释掉了,但仍然出现错误。

是哪里出了问题或者我做错了什么?

编辑

我的requirements.txt文件:

dj-database-url
django
gunicorn
psycopg2
whitenoise
dj-static
Unipath
python-decouple
Pillow
Markdown
bleach
python-decouple
django-material
raven
django-debug-toolbar
django-tables2
django-phonenumber-field
django-guardian
django-notifications-hq
djangorestframework
matplotlib
numpy
seaborn
pandas
django-heroku

结构如下:

.
├── core
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-35.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-35.pyc
│   │   ├── __init__.cpython-35.pyc
│   │   ├── models.cpython-35.pyc
│   │   └── views.cpython-35.pyc
│   ├── templates
│   │   └── core
│   │       ├── base.html
│   │       ├── footer.html
│   │       ├── index.html
│   │       ├── login.html
│   │       ├── navbar.html
│   │       ├── nav.html
│   │       ├── partial_settings_menu.html
│   │       ├── password.html
│   │       ├── picture.html
│   │       ├── profile.html
│   │       └── registration.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── funderpreneur
│   ├── fonts
│   │   ├── material-design-icons
│   │   │   ├── Material-Design-Iconsd41d.eot
│   │   │   ├── Material-Design-Icons.svg
│   │   │   ├── Material-Design-Icons.ttf
│   │   │   └── Material-Design-Icons.woff
│   │   └── roboto
│   │       ├── Roboto-Bold.ttf
│   │       ├── Roboto-Bold.woff
│   │       ├── Roboto-Light.ttf
│   │       ├── Roboto-Light.woff
│   │       ├── Roboto-Medium.ttf
│   │       ├── Roboto-Medium.woff
│   │       ├── Roboto-Regular.ttf
│   │       ├── Roboto-Regular.woff
│   │       ├── Roboto-Thin.ttf
│   │       └── Roboto-Thin.woff
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   ├── settings.cpython-35.pyc
│   │   ├── urls.cpython-35.pyc
│   │   └── wsgi.cpython-35.pyc
│   ├── settings.py
│   ├── static
│   │   ├── css
│   │   │   ├── account-settings.css
│   │   │   ├── bootstrap.min.css
│   │   │   ├── custom-materialize.css
│   │   │   ├── footer.css
│   │   │   ├── high-rise-building.jpg
│   │   │   ├── materialize.css
│   │   │   ├── navbar.css
│   │   │   ├── navbar-top-fixed.css
│   │   │   ├── style2.css
│   │   │   └── style.css
│   │   ├── humans.txt
│   │   ├── img
│   │   │   └── buet_logo.png
│   │   └── js
│   │       ├── bootstrap.min.js
│   │       ├── circular-progress-jquery.js
│   │       ├── jquery-1.11.2.min.js
│   │       ├── jquery-slim.min.js
│   │       ├── jquery.waypoints.min.js
│   │       ├── materialize.js
│   │       ├── popper.min.js
│   │       ├── profile-validation.js
│   │       └── registration-validation.js
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── media
├── Pipfile
├── Procfile
├── README.md
├── requirements.txt
├── runtime.txt
└── staticfiles

请展示实际的requirements.txt文件和您的项目布局。 - Daniel Roseman
我已经编辑了我的问题。 - sphoenix
7个回答

8
我也遇到了这个问题,但是通过在 requirements.txt 文件中添加 python-decouple==3.1,我成功解决了它!

6

您需要在设备上安装decouple模块。该事件与版本更改无关。

尝试:

pip install python-decouple

没有 requirements.txt 文件


3

我认为你还没有在下面的 [packages] 部分将 requirements.txt 中的包名写入到 Pipfile 文件中。你需要按照以下格式添加这些包 -

package_name = "version"

1
我遇到了与 OP 相同的问题,您能否详细说明一下这个答案?[packages] 部分是否应包含 requirements.txt 中的所有软件包?我尝试在 [packages] 中包含 python-decouple = "==3.1",但没有成功。目前,我的 [packages] 只包含 django = "==2.1.2" - Darcy

2

我曾经遇到过同样的问题,我是django的新手。在更新了requirements.txt之后,上述步骤并没有起作用,直到我切换了env

source env/Scripts/activate
pip install -r requirements.txt
py manage.py runserver

1

我之前遇到了这个问题,是因为我错误地使用了pip install decouple而不是pip install python-decouple

所以只需要运行pip uninstall decouple,并确保从您的requirements.txtPipfile/Pipfile.lock文件中删除了decouple包。

然后运行pip install python-decouple


1
我遇到了类似的错误,原来是必须在虚拟环境中安装模块。首先启动虚拟环境,然后在其中安装模块。现在尝试启动服务器,问题应该已经解决了。 在某些情况下,可能已经安装了"decouple"模块,在启动服务器之前卸载该模块,否则会导致错误。

1
我也遇到了这个问题,通过从本地和环境中删除所有包(删除了2个python-decouple和decouple),然后重新安装python-decouple(3.3)解决了问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接