AttributeError:“PosixPath”对象没有属性“read”

问题描述 投票:0回答:1

我想进行迁移,但出现错误 我在virturalenv中的python版本是Python 3.12.3 我还没有找到对 stackoverflow 的合适回应。

Traceback (most recent call last):
  File "/home/anton/Documents/project2/backend/my_backend/manage.py", line 22, in <module>
    main()
  File "/home/anton/Documents/project2/backend/my_backend/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/anton/Documents/project2/backend/pr2_venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/home/anton/Documents/project2/backend/pr2_venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 382, in execute
    settings.INSTALLED_APPS
  File "/home/anton/Documents/project2/backend/pr2_venv/lib/python3.12/site-packages/django/conf/__init__.py", line 81, in __getattr__
    self._setup(name)
  File "/home/anton/Documents/project2/backend/pr2_venv/lib/python3.12/site-packages/django/conf/__init__.py", line 68, in _setup
    self._wrapped = Settings(settings_module)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/anton/Documents/project2/backend/pr2_venv/lib/python3.12/site-packages/django/conf/__init__.py", line 166, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/home/anton/Documents/project2/backend/my_backend/my_backend/settings.py", line 12, in <module>
    environ.Env.read_env(BASE_DIR / '.env')
  File "/home/anton/Documents/project2/backend/pr2_venv/lib/python3.12/site-packages/environ/environ.py", line 646, in read_env
    content = f.read()
              ^^^^^^
AttributeError: 'PosixPath' object has no attribute 'read'

当我重新连接postgres数据库然后再次连接sqlite3时,我遇到了同样的错误。

python django sqlite django-models django-rest-framework
1个回答
0
投票

先将其转换为字符串:

nviron.Env.read_env(str(BASE_DIR / '.env'))

.read_env(…)
 [readthedocs.io]可能无法与
Path
对象一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.