nginx 正在服务器上运行collectstatic:属性错误:"PosixPath"对象没有属性"startswith"

knsnq2tg  于 2023-03-01  发布在  Nginx
关注(0)|答案(3)|浏览(202)

在使用nginx、gunicorn、django和virtualenv在Digital Ocean的服务器上进行部署之后,我尝试使用collectstatic:

python manage.py collectstatic --settings=config.settings.production

正如你所看到的,我有多个设置文件。一个基本,一个本地和一个生产设置文件。下面是错误:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/tony/vp/vpenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/tony/vp/vpenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/tony/vp/vpenv/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/tony/vp/vpenv/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/tony/vp/vpenv/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 173, in handle
    if self.is_local_storage() and self.storage.location:
  File "/home/tony/vp/vpenv/lib/python3.5/site-packages/django/utils/functional.py", line 239, in inner
    return func(self._wrapped, *args)
  File "/home/tony/vp/vpenv/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/tony/vp/vpenv/lib/python3.5/site-packages/django/core/files/storage.py", line 283, in location
    return abspathu(self.base_location)
  File "/home/tony/vp/vpenv/lib/python3.5/posixpath.py", line 357, in abspath
    if not isabs(path):
  File "/home/tony/vp/vpenv/lib/python3.5/posixpath.py", line 64, in isabs
    return s.startswith(sep)
AttributeError: 'PosixPath' object has no attribute 'startswith'

我的production.py设置文件包含以下内容:

MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = 'media/'
STATIC_ROOT = BASE_DIR / 'static'

我的基本目录如下(从基本设置文件导入):

BASE_DIR = Path(__file__).resolve().parent.parent.parent

原因可能是什么?

g2ieeal7

g2ieeal71#

您使用的是Python 3.5,在Python 3.6中添加了对os模块中Path objects的支持,您可以:

  • 要么升级到Python 3.6;或
  • 避免使用Path对象:
BASE_DIR = os.path.abspath(os.path.join(__file__, '../../../'))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
hs1ihplo

hs1ihplo2#

这是我之前的设置,我使用的是Python 3.5,Django 2.1。

BASE_DIR = Path(__file__).resolve().parent.parent.parent
STATIC_ROOT = BASE_DIR / 'static'

我只改变了一件事:

STATIC_ROOT = str(BASE_DIR / 'static')

一切正常。

fsi0uk1n

fsi0uk1n3#

在我的例子中,这是因为我手动安装了(这是旧版本)。新的Python版本(〉= 3. 6)包含了pathlib库。
所以我只是简单地做了pip uninstall pathlib,这样它就可以使用已经捆绑了python的那个。当我再次运行代码时,它工作得很好。

相关问题