8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Django " TypeError: argument of type 'PosixPath' is not iterable" のエラー対処

Last updated at Posted at 2021-07-03

Django をいじっていたときに出てきたエラー。
どういう経緯でエラーになったのかはわからないが一応直せたので備忘録として残す。

エラーはアプリ作成時やmigrateの際に出てきてた。
以下、エラー文

$ python manage.py startapp reviews
Traceback (most recent call last):
  File "/home/taka/Dropbox/user/django_project/beginners_book/project1/manage.py", line 22, in <module>
    main()
  File "/home/taka/Dropbox/user/django_project/beginners_book/project1/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/taka/anaconda3/envs/django_env/lib/python3.9/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/taka/anaconda3/envs/django_env/lib/python3.9/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/taka/anaconda3/envs/django_env/lib/python3.9/site-packages/django/core/management/base.py", line 336, in run_from_argv
    connections.close_all()
  File "/home/taka/anaconda3/envs/django_env/lib/python3.9/site-packages/django/db/utils.py", line 224, in close_all
    connection.close()
  File "/home/taka/anaconda3/envs/django_env/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 248, in close
    if not self.is_in_memory_db():
  File "/home/taka/anaconda3/envs/django_env/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 367, in is_in_memory_db
    return self.creation.is_in_memory_db(self.settings_dict['NAME'])
  File "/home/taka/anaconda3/envs/django_env/lib/python3.9/site-packages/django/db/backends/sqlite3/creation.py", line 12, in is_in_memory_db
    return database_name == ':memory:' or 'mode=memory' in database_name
TypeError: argument of type 'PosixPath' is not iterable

PosixPathはPathlibというライブラリを使用したときに返される型らしく、そのタイプエラーになっていた。

このpathオブジェクトをstr型に変換するといいらしい。

setting.py
#変更前
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3'
    }
}

#変更後
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': str(BASE_DIR / 'db.sqlite3'),
    }
}

参考

8
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?