windows, docker web_1 | ModuleNotFoundError: No module named 'rest_framework'
Closed this issue · 8 comments
I'm trying to make an example at https://github.com/morenoh149/django-rest-datatables-relations-example
I'm on windows trying to use docker. The base project works fine but now I want to add django-restframework. I edited requirements.txt and rebuild the project with docker-compose up -d --no-deps --build
but that doesn't seem to install the latest requriements.
I tried deleting pipfile.lock but cannot regenerate it on windows. Also the compose file seems to simply copy the Pipfile.lock into the docker container.
error
web_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
web_1 | res = instance.__dict__[self.name] = self.func(instance)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
web_1 | return import_module(self.urlconf_name)
web_1 | File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
web_1 | return _bootstrap._gcd_import(name[level:], package, level)
web_1 | File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 991, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 783, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1 | File "/code/config/urls.py", line 5, in <module>
web_1 | from rest_framework import routers
web_1 | ModuleNotFoundError: No module named 'rest_framework'
Any tips on how to get past this?
I think you should add djangorestframework
to the Pipfile and then generate the lock.
The Dockerfile copies both Pipfile
and Pipfile.lock
and then pipenv install
is executed.
https://github.com/wsvincent/djangox/blob/8e05085caf6ddff9f527d57c30bfb8cd2f7d35ed/Dockerfile#L12
I believe requirements.txt
exists in this project only for compatibility with pip
. (#35)
If you want to use it, you need to modify the dockerfile to copy requirements.txt
and pip install it.
ok. I regenerated the lockfile with pipenv. I had to remove the python_version = "3.8"
since I have 3.9 installed. I also added the requirements
djangorestframework = "==3.10.3"
djangorestframework-datatables = "==0.5.0"
and it builds with docker without errors with > docker-compose up -d --no-deps --build
.
But when the containers run they seem to crash with this error
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 392, in check
all_issues = checks.run_checks(
File "/usr/local/lib/python3.8/site-packages/django/core/checks/registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/usr/local/lib/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/code/config/urls.py", line 5, in <module>
from rest_framework import routers
File "/usr/local/lib/python3.8/site-packages/rest_framework/routers.py", line 25, in <module>
from rest_framework import RemovedInDRF311Warning, views
File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 16, in <module>
from rest_framework.response import Response
File "/usr/local/lib/python3.8/site-packages/rest_framework/response.py", line 11, in <module>
from rest_framework.serializers import Serializer
File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 24, in <module>
from django.db.models.fields import FieldDoesNotExist
ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models.fields' (/usr/local/lib/python3.8/site-packages/django/db/models/fields/__init__.py)
I had to remove the python_version = "3.8" since I have 3.9 installed.
The thing is, you're running this project on docker, so you might want to change the base image in the Dockerfile.
https://github.com/wsvincent/djangox/blob/8e05085caf6ddff9f527d57c30bfb8cd2f7d35ed/Dockerfile#L2
Also, the [requires]
section from the Pipfile is used by pipenv on pipenv install
. If you remove it pipenv will not check the current python version. Source
From https://github.com/izimobil/django-rest-framework-datatables:
Note: Django 3.0 or superior is only supported with Django REST Framework 3.11 or superior and DRF-datatables version 0.5.1 or superior.
I think that explains the error:
ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models.fields' (/usr/local/lib/python3.8/site-packages/django/db/models/fields/__init__.py)
ok great. I bumped the dockerfile to use the 3.9 python base image. I also pulled in the latest version of the restframework deps (don't know why I was using older versions).
# Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
django = "~=3.1.0"
django-allauth = "==0.43.0"
django-crispy-forms = "~=1.10.0"
django-debug-toolbar = "==3.2"
whitenoise = "==5.2.0"
djangorestframework = "~=3.12.0"
djangorestframework-datatables = "~=0.6.0"
psycopg2-binary = "==2.8.6"
[requires]
python_version = "3.9"
The project runs with docker-compose up
. But after making a super user and trying to log in with the correct password I get this sqlite file permission error (despite the fact that I'm running powershell as an administrator)
Environment:
Request Method: POST
Request URL: http://localhost:8000/admin/login/?next=/admin/
Django Version: 3.1.7
Python Version: 3.9.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
'django.contrib.sites',
'allauth',
'allauth.account',
'crispy_forms',
'debug_toolbar',
'rest_framework',
'rest_framework_datatables',
'accounts',
'pages']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
The above exception (unable to open database file) was the direct cause of the following exception:
File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.9/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 410, in login
return LoginView.as_view(**defaults)(request)
File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/views/decorators/debug.py", line 89, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/views.py", line 63, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/views/generic/edit.py", line 142, in post
return self.form_valid(form)
File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/views.py", line 92, in form_valid
auth_login(self.request, form.get_user())
File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 132, in login
user_logged_in.send(sender=user.__class__, request=request, user=user)
File "/usr/local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in send
return [
File "/usr/local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 178, in <listcomp>
(receiver, receiver(signal=self, sender=sender, **named))
File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/models.py", line 20, in update_last_login
user.save(update_fields=['last_login'])
File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 67, in save
super().save(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 753, in save
self.save_base(using=using, force_insert=force_insert,
File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 790, in save_base
updated = self._save_table(
File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 872, in _save_table
updated = self._do_update(base_qs, using, pk_val, values, update_fields,
File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 926, in _do_update
return filtered._update(values) > 0
File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 803, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1522, in execute_sql
cursor = super().execute_sql(result_type)
File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1156, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /admin/login/
Exception Value: unable to open database file
so then I tried using the postgres container instead. Changed my settings.py
to
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': '',
# 'HOST': '127.0.0.1',
'PORT': '5432',
}
}
but I get a connection problem
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
web_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
web_1 | connection = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect
web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web_1 | django.db.utils.OperationalError: could not connect to server: No such file or directory
web_1 | Is the server running locally and accepting
web_1 | connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
web_1 |
tried adding the following to docker-compose.yml
but it didn't help
db:
image: postgres:11
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
ports:
- "5432:5432"
tty: true
How do you configure the docker-compose.yml
to connect to the stock postgres container?
I always forgot how to fix that problem.
The default postgres image exposes the port 5432, so you don't need to put this.
I've never used tty
but it shouldn't be a problem.
ports:
- 5432:5432
tty: true
Why did you comment HOST in DATABASES config in settings.py
?
@marcorichetta I don't know what the HOST value should be. I was hoping it would find it in some default location.
'HOST': '127.0.0.1',
doesn't work either. I've tried variations on several of these configs. So then my question again, what are the right config values for this? :)
It seems it should be db, as the name of the service in docker-compose.yaml. Take a look at the reference example from Docker docs. https://docs.docker.com/compose/django/
Those instructions worked! The fully runnable example is at https://github.com/morenoh149/django-rest-datatables-relations-example Thanks @marcorichetta