【问题标题】:ImportError: Could not import settings when python manage.py collectstaticImportError:python manage.py collectstatic 时无法导入设置
【发布时间】:2016-12-23 23:44:39
【问题描述】:

我正在开发我的第一个 django 网站(基于 tango 和 django 教程),并且在 Debug = False 时遇到了提供静态文件的问题。 (当我有 Debug = True 时一切正常)

我的文件夹结构如下:

|___401.shtml
|___500.shtml
|___requirements.txt
|___400.shtml
|___populate_rango.py
|___manage.py
|___templates
| |___rango
| | |___index.html
| | |___category.html
| | |___cats.html
| | |___category_list.html
| | |___add_category.html
| | |___base.html
| | |___register.html
| | |___restricted.html
| | |___about.html
| | |___login.html
| | |___add_page.html
| |___registration
| | |___logout.html
| | |___registration_complete.html
| | |___registration_form.html
| | |___login.html
|___media
| |___photo.png
| |___.htaccess
| |___profile_images
| | |___Screenshot_from_2016-07-18_145936.png
| | |___Screenshot_from_2016-07-21_112910.png
|___cgi-bin
| |___.htaccess
|___rango
| |___admin.py
| |___migrations
| | |___0003_category_slug.py
| | |_____init__.pyc
| | |___0003_category_slug.pyc
| | |___0001_initial.pyc
| | |___0001_initial.py
| | |___0004_userprofile.pyc
| | |___0002_auto_20160804_1857.py
| | |___0002_auto_20160804_1857.pyc
| | |___0004_userprofile.py
| | |_____init__.py
| |___models.py
| |___tests.py
| |_____init__.pyc
| |___models.pyc
| |___templatetags
| | |_____init__.pyc
| | |___rango_extras.pyc
| | |___rango_extras.py
| | |_____init__.py
| |___forms.py
| |___views.pyc
| |___urls.py
| |___admin.pyc
| |___views.py
| |___urls.pyc
| |___forms.pyc
| |_____init__.py
|___tango_with_django_project
| |___settings.pyc
| |___wsgi.pyc
| |_____init__.pyc
| |___urls.py
| |___settings.py
| |___urls.pyc
| |___wsgi.py
| |_____init__.py
|___static
| |___images
| | |___photo.png
| | |___rango.png
| |___.htaccess
| |___js
| | |___rango-jquery.js
| | |___jquery.js
| | |___rango-ajax.js
|___db.sqlite3
|___test.py
|___404.shtml
|___403.shtml

我在 settings.py 路径中添加了 STATIC_ROOT 如下:

import os
import uwsgi
from uwsgidecorators import timer
from django.utils import autoreload

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

(...)

ROOT_URLCONF = 'tango_with_django_project.urls'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    STATIC_ROOT,
)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static')

现在我想从 ssh 终端运行

python manage.py collectstatic

但是我遇到了一个错误

Traceback (most recent call last):
  File "manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "/home/mrklap/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/mrklap/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    settings.INSTALLED_APPS
  File "/home/mrklap/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "/home/mrklap/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/mrklap/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
    % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'tango_with_django_project.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named uwsgi

我在这里阅读帖子和 schwärzl 答案:

ImportError: Could not import settings

并将 wsgi.py 文件修改为:

import os
import sys
sys.path.append('home/mrklap/domains/tredo.net/public_html/tango_with_django_project')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tango_with_django_project.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

但当我在 ssh 中进入(或退出)virtualenv 并运行时:

>>> import sys
>>> for path in sys.path: print path

...

我没有添加路径

/home/mrklap/venv/lib/python2.7
/home/mrklap/venv/lib/python2.7/plat-x86_64-linux-gnu
/home/mrklap/venv/lib/python2.7/lib-tk
/home/mrklap/venv/lib/python2.7/lib-old
/home/mrklap/venv/lib/python2.7/lib-dynload
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/home/mrklap/venv/local/lib/python2.7/site-packages
/home/mrklap/venv/lib/python2.7/site-packages
>>> 

为什么我在下面的列表中看不到该路径?

【问题讨论】:

  • 查看报错'No module named uwsgi',好像没有安装uwsgi
  • 请注意,collectstatic 命令会收集STATICFILES_DIRS 中的文件,并将它们复制到STATIC_ROOT 您从中提供它们的位置。因此,在STATICFILES_DIRS 设置中包含STATIC_ROOT 是没有意义的。
  • Alasdair,感谢您的帮助,但我看到(当我在虚拟环境中)安装了 uwsgi)(venv) [mrklap@s34:: ~/domains/tredo.net/‌​public_html ]:$ pip freeze Django==1.7 django-bootstrap-too‌​lkit==2.15.0 django-registration-‌​redux==1.4 Pillow==3.3.0 uWSGI==2.0.13.1您还有其他想法吗?
  • 实际上,我认为您的设置文件中根本不应该有import uwsgi。它仅在 uwsgi 服务器运行您的项目时起作用,它不适用于像 collectstatic 这样的管理命令。您在设置中使用它做什么?尝试删除它。
  • @Alasdair 你是对的。我没有把整个设置文件放在这里是我的错误。问题是由 settings.py 中的以下几行引起的:import uwsgi 和函数正在搜索我在 link 中找到的代码 @timer(3) def change_code_gracefull_reload(sig): if autoreload.code_changed(): uwsgi.reload() 的更改。但是,目前我每次更改代码时都需要重新启动服务器,有什么办法可以解决吗?

标签: django python-2.7 deployment


【解决方案1】:

试试sudo pip install uwsgi 因为你已经在设置文件中导入了这个

【讨论】:

  • Alasdair, Aamish 感谢您的帮助,但我看到(当我在虚拟环境中时)安装了 uwsgi)(venv) [mrklap@s34:: ~/domains/tredo.net/public_html ]:$ pip freeze Django==1.7 django-bootstrap-toolkit==2.15.0 django-registration-redux==1.4 Pillow==3.3.0 uWSGI==2.0.13.1 您还有其他想法吗?
  • 我是通过source ~/venv/bin/activate激活的,对吗?
  • 是的,没关系。尝试卸载并重新安装 Uwsgi。您可能会在安装时遇到一些问题,这将有助于我们解决问题。
  • (in venv) 制作了pip uninstall uwsgi(完成成功)和pip install uwsgi(成功安装了uwsgi-2.0.13.1),当collectstatic得到与上面相同的错误。
  • 那么我必须更详细地讨论这个问题。我会尽快回复你。
猜你喜欢
  • 2020-12-05
  • 1970-01-01
  • 2018-06-05
  • 2014-02-24
  • 2016-02-01
  • 2020-10-17
  • 2018-11-02
  • 2019-01-18
  • 1970-01-01
相关资源
最近更新 更多