Python manage.py collectstatic TypeError: an integer is required (got type str)

Posted

技术标签:

【中文标题】Python manage.py collectstatic TypeError: an integer is required (got type str)【英文标题】: 【发布时间】:2019-08-27 12:29:48 【问题描述】:

我正在使用 Django 2.2 版。当我运行命令时 python manage.py collectstatic

我在 bash 终端上得到以下日志。

You have requested to collect static files at the destination
location as specified in your settings:

    /home/djapp/poorface/staticfiles

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 188, in handle
    collected = self.collect()
  File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
    handler(path, prefixed_path, storage)
  File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 353, in copy_file
    self.storage.save(prefixed_path, source_file)
  File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/files/storage.py", line 49, in save
    return self._save(name, content)
  File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/files/storage.py", line 288, in _save
    os.chmod(full_path, self.file_permissions_mode)
TypeError: an integer is required (got type str)

我将静态文件的权限更改为 -rwxrwxrwx 。然后输出也没有变化。

manage.py 看起来像

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Poorface.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

【问题讨论】:

当你运行ls -al /home 时,shell 对权限有何看法?它可能与特定于平台的某些东西有关......例如OSX 具有扩展的权限属性。路径 /home/djapp/home/amit 看起来有点粗略......您确定要将静态数据发送到不同的“主”目录吗? ls -al /home drwxr-xr-x 8 amit amit 4096 3 月 13 日 15:29 amit drwxr-xr-x 5 djapp djapp 4096 3 月 13 日 17:23 djapp 如果它有效,那么将静态数据发送到不同的“主”目录没有问题。 您可以尝试使用--dry-run 标志运行,以便查看引用的文件路径。然后,您可以检查给定文件夹的权限。您也可以尝试将 STATIC_ROOT 设置为可以非常确定可以访问的位置...例如/tmp 位于文件系统的根目录。 --dry-run flag 帮助我解决了问题。但是,我仍然不明白为什么会发生此错误。我正在传递 yes 参数,这是要传递的参数之一,对文件和文件夹的权限也很好。 【参考方案1】:

这是一个 hack - 不是正确的修复!

我在 OSX 和 Ubuntu 上都遇到过这个问题。似乎某些包最终以字符串形式获得文件权限,而大多数都是整数。

我破解了 django 来解决这个问题,并确定了导致问​​题的软件包 - django-filebrowser - 最好不要破解 django,但如果绝望,这里是 core/files/storage.py 第 289 行中的更改::

    if self.file_permissions_mode is not None:

        if type(self.file_permissions_mode) == type("duck"):
              print(full_path, self.file_permissions_mode, type(self.file_permissions_mode))
              self.file_permissions_mode = int(self.file_permissions_mode)
        os.chmod(full_path, self.file_permissions_mode)

这报道了::

  /home/django/teamup/shared_static/filebrowser/css/filebrowser.css 0644 <class 'str'>

我尝试在 virtualenv 中更改文件浏览器的文件权限并重新运行,但这并没有解决问题,所以我已经解决了这个问题。

【讨论】:

【参考方案2】:

是的,这个问题很老了,但我现在又遇到了这个问题,发现了错误源和在一些白发后的正确修复,篡改了各种文件访问权限和一个简单的调试运行“ manage.py collectstatic”:

在过去或者在某些不同的情况下(不记得这是如何进入我的项目开发设置的 - 在我的生产设置中它是正确的)在settings.py 是这样的:

FILE_UPLOAD_PERMISSIONS = "0644"

根据文档至少从Django 1.8 开始,正确的设置是:

FILE_UPLOAD_PERMISSIONS = 0o644

在我知道解决方案的原因后,我还发现了一些提示,即“现在使用 python 3 ... 八进制 ... 等等。”但与这个令人沮丧的模糊错误消息无关:

…  os.chmod(full_path, self.file_permissions_mode)
TypeError: an integer is required (got type str)

在我使用“allways™”(我知道)的命令 python manage.py collectstatic 之后。

所以我希望你不会因为像我这样的一些有点陈旧的项目而让你白发这么多!

【讨论】:

以上是关于Python manage.py collectstatic TypeError: an integer is required (got type str)的主要内容,如果未能解决你的问题,请参考以下文章

初试django---python manage.py makemigrations以及python manage.py migrate

运行python manage.py 出现mportError: No module named django.core.management when using manage.py

python 自定义python manage.py命令

Mac下python manage.py runsever 报错

django 和 python ./manage.py makemigrations 执行错误

python manage.py migrate --fake <appname> vs python manage.py migrate --fake <appname> 零