Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AttributeError: 'PosixPath' object has no attribute 'read'

I wanted to do a migration, but I got an error
My python version in the virturalenv is Python 3.12.3
I haven’t found a single appropriate response to stackoverflow yet.

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'

I had the same error when I reconnected the postgres database and then sqlite3 again.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Convert it to a string first:

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

read_env probably cannot work with Path objects.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading