Skip to content

error of collectstatic for AttributeError: 'Path' object has no attribute 'startswith' #86

@raccoonyy

Description

@raccoonyy

Question: Why I have to use root property of Path instance?

1. First try

# settings.py
STATIC_ROOT = root.path('collected_static/')
STATICFILES_DIRS = [
    root.path("web/static"),
    ("components", root.path("bower_components/")),
]

then, run collectstatic command print out:

Traceback (most recent call last):
  File "./manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/core/management/__init__.py", line 195, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/core/management/__init__.py", line 40, in load_command_class
    return module.Command()
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 32, in __init__
    self.storage.path('')
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/utils/functional.py", line 204, in inner
    self._setup()
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 394, in _setup
    self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)()
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 39, in __init__
    *args, **kwargs)
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/core/files/storage.py", line 185, in __init__
    self.location = abspathu(self.base_location)
  File "/Users/raccoony/.pyenv/versions/3.5.0/lib/python3.5/posixpath.py", line 357, in abspath
    if not isabs(path):
  File "/Users/raccoony/.pyenv/versions/3.5.0/lib/python3.5/posixpath.py", line 64, in isabs
    return s.startswith(sep)
AttributeError: 'Path' object has no attribute 'startswith'

when I print out STATICFILES_DIRS

[<Path:/Users/raccoony/smartstudy/box/web/static>, ('components', <Path:/Users/raccoony/smartstudy/box/bower_components>)]

not path strings but Path instaces.

2. so I tried root property

# settings.py (modeified)
STATIC_ROOT = root.path('collected_static/').root
STATICFILES_DIRS = [
    root.path("web/static").root,
    ("components", root.path("bower_components/").root),
]

then, run collectstatic will be:

Traceback (most recent call last):
  File "./manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/core/management/__init__.py", line 195, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/core/management/__init__.py", line 40, in load_command_class
    return module.Command()
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 32, in __init__
    self.storage.path('')
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/utils/functional.py", line 204, in inner
    self._setup()
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 394, in _setup
    self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)()
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 37, in __init__
    check_settings(base_url)
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/django/contrib/staticfiles/utils.py", line 58, in check_settings
    (settings.MEDIA_ROOT == settings.STATIC_ROOT)):
  File "/Users/raccoony/.pyenv/versions/3.5.0/envs/box/lib/python3.5/site-packages/environ/environ.py", line 670, in __eq__
    return self.__root__ == other.__root__
AttributeError: 'str' object has no attribute '__root__'

3. I used root property to MEDIA_ROOT too.

# settings.py (2nd modified)
STATIC_ROOT = root.path('collected_static/').root
STATICFILES_DIRS = [
    root.path("web/static").root,
    ("components", root.path("bower_components/").root),
]

MEDIA_ROOT = root.path("media").root

then, collectstatic runs without error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions