error of collectstatic for AttributeError: 'Path' object has no attribute 'startswith'
See original GitHub issueQuestion: 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.
Issue Analytics
- State:
- Created 7 years ago
- Comments:6
Top Results From Across the Web
AttributeError: 'PosixPath' object has no attribute 'startswith ...
I am using Python 3.5. Django 2.1. BASE_DIR = Path(__file__).resolve().parent.parent.parent STATIC_ROOT = BASE_DIR / 'static'.
Read more >Collectstatic failed : Forums - PythonAnywhere
I tried running collectstatic but only got the following error: ... AttributeError: 'list' object has no attribute 'startswith'. The paths ...
Read more >running collectstatic on server : attributeerror: 'posixpath' object has ...
running collectstatic on server : attributeerror: 'posixpath' object has no attribute 'startswith'. BASE_DIR = os.path.abspath(os.path.join(__file__, '../.
Read more >AttributeError: 'PosixPath' object has no attribute 'startswith'
Answer a question After deploying on a server on digital ocean using nginx, gunicorn, django, and virtualenv, I try to use collectstatic: ...
Read more >Django : Running collectstatic on server : AttributeError: 'PosixPath ...
Django : Running collectstatic on server : AttributeError: 'PosixPath' object has no attribute 'startswith' [ Beautify Your Computer ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is on my plans, but right now I don’t have enough time for that. I’ll try to return to this topic a bit later
I ran into this bug too. I haven’t read all of
environ
’s source code, but for us this happened because we had a setting configured usingenv.Path
, and then we had it overridden in a django test via a decorator, like so:Something about that combination made the built in
env.path
thing try to compare one of itsPath
objects with a str, which failed, as in the first comment.The solution, which you can see here was to not use
environ
’s path objects, and instead rely on pathlib.I’m not super familiar with this package and why it has its own pathlib-alike (guessing it pre-dates py3?), but I think I’d suggest removing it since it appears to have bugs and (probably?) isn’t necessary anymore. Instead, we could pretty easily, I think, migrate people to pathlib in their settings, though that’d be a backwards incompatible change.