19
votes

I am using the latest version of django django 2.2.12

I tried checking for errors before migration with the command

python3 manage.py check

but then it throws the error below

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/lib/python3/dist-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 336, in run_from_argv
    connections.close_all()
  File "/usr/lib/python3/dist-packages/django/db/utils.py", line 224, in close_all
    connection.close()
  File "/usr/lib/python3/dist-packages/django/db/backends/sqlite3/base.py", line 248, in close
    if not self.is_in_memory_db():
  File "/usr/lib/python3/dist-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 "/usr/lib/python3/dist-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
6
Sounds like you specified a database setting with a Path object, convert it to str(). - Alex Hall
I have done no such thing. I have seen some changes in the django 2.2.12 where they have replaced the os module with the pathlib module. I guess this is causing the issue. I am bringing up my settings.py file so that you help me with it - Freduah Gideon
@AlexHall I heeded to your solution and converted it to a string it worked for me. Thank You For Your Help - Freduah Gideon
OK, I've made an answer so that people can see that this has been solved. It might still be good to show the relevant part of your settings in case other people search for this error. - Alex Hall

6 Answers

30
votes

Make sure that you imported os in project-level settings.py:

import os

Add following in DATABASES within settings.py:

'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

In last versions of django, we can do the following settings using str():

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': str(BASE_DIR / "db.sqlite3"),
    }
}
19
votes

if you use Django 3.1’s startproject template. you must use str() function in your settings file.

find this code in settings file

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

change it to :

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

or you can use os.path.join(BASE_DIR, 'db.sqlite3'),

2
votes

Sounds like you specified a database setting with a Path object, convert it to str().

1
votes

I just encountered this by surprise myself. I was generating a series of Django projects in 3.1.3 outside of a virtual environment and did a quick pip install which pulled my Django requirement down to 3.05. The earlier config settings looked like this

"""                                                                                                  
Django settings for config project.                                                                                                                                                                  
Generated by 'django-admin startproject' using Django 3.1.3.                                         
"""                                                                                                     
from pathlib import Path                                                                             
                                                                                                     
# Build paths inside the project like this: BASE_DIR / 'subdir'.                                     
BASE_DIR = Path(__file__).resolve().parent.parent 

and after the install subsequent ./manage.py scripts suddenly started giving me this same traceback. Rebuilding my project configured my settings to look like this:

"""                                                                                                  
Django settings for config project.                                                                                                                                                                      
Generated by 'django-admin startproject' using Django 3.0.5.                                         
"""                                                                                                  
                                                                                                     
import os                                                                                            
                                                                                                     
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)                              
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 

Which was the tip off. It seems the settings generation script detects when your environment is better suited to one path construction mechanism or the other.

Unfortunately, wrapping str() around the paths allowed me to issue a few ./manage commands without error, but I could no longer launch the server in any other projects I created without far worse tracebacks.

I wonder if there is something like a > pip revert --time=-2hrs.

I ended up uninstalling the last couple of package installs and doing and explicit Django 3.1.3 install to get back to where I was. pip freeze time.

0
votes

I created a new Django project in Ubuntu 20.04 using Python 3.7 and Django 3.1.5. When I run ./manage.py migrate or ./manage.py makemigrations. I get the error in the original question. I found that switching to Python 3.8 fixes the error.

0
votes

I'm using Ubuntu 20.04, python 3.8.5 and Django 3.0.5. I had the same error after I installed the djongo package to work with MongoDB, same error but not the same cause, however, I was able to fix the error by converting BASE_DIR / 'db.sqlite3' to a string

str( BASE_DIR / 'db.sqlite3' )