欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TypeError: join() argument must be str or bytes, not ‘PosixPath‘

程序员文章站 2022-07-15 15:46:05
...

在使用python3 manage.py migrate进行数据库迁移时报错如下:
(进行操作python3 manage.py runserver或者python3 manage.py migrate等一系列的围绕manage.py的操作时,可能会出现下面的错误。)

[email protected]:/var/www/djangoProject11# python3 manage.py migrate
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 308, in execute
    settings.INSTALLED_APPS
  File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 110, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/var/www/djangoProject11/djangoProject11/settings.py", line 132, in <module>
    os.path.join(BASE_DIR,'static'),
  File "/usr/lib/python3.5/posixpath.py", line 89, in join
    genericpath._check_arg_types('join', a, *p)
  File "/usr/lib/python3.5/genericpath.py", line 143, in _check_arg_types
    (funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'PosixPath'

这是因为setting.py里面的问题,从而导致其他文件出现错误。


在运行代码前
我的setting.py里面的导致错误的内容如下:

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
......
......


STATIC_URL =/static/’
STATIC_ROOT=os.path.join(BASE_DIR,"/static/")

STATICFILES_DIRS = (
	os.path.join(BASE_DIR,static),
)

对其进行修改

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
......
......
STATIC_URL =/static/’
STATIC_ROOT=BASE_DIR.joinpath(static)

STATICFILES_DIRS = (
BASE_DIR.joinpath(static),
)

这个错误就解决了。
如果你的setting.py里面存在

STATIC_ROOT = ...
MEDIA_ROOT = ...

的话,把os.path.join(…,’…’)修改为BASE_DIR.joinpath(’…’)
就行了。



如果你是用的是python3.5的话(在命令行输入python -V 就可以查看python的版本),
你可能还需要看看这一篇博客:

AttributeError: ‘PosixPath‘ object has no attribute ‘startswith‘