ModuleNotFoundError: No module named 'AppName' in Django - django

I did install django in server. Instead of installing apps next to the django project folder, I installed them inside the django project folder. (I put them next to the settings.py and wsgi.py)
This is structure of django project:
BaseFile
|__django (Project File)
| |___administrator (App)
| | |___ views.py
| | |___ urls.py
| | |___ ...
| |
| |___blog (App)
| | |___ views.py
| | |___ urls.py
| | |___ ...
| |
| |___ __init__.py
| |___ settings.py
| |___ urls.py
| |___ wsgi.py
|
|___ env
|___ manage.py
|___ media
|___ static
|___ templates
And this is my settings.py :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# MY APPS:
'administrator.apps.AdministratorConfig',
'blog.apps.BlogConfig',
]
And when I want to makemigrations I got this error:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "BaseFile/env/lib64/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "BaseFile/env/lib64/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "BaseFile/env/lib64/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "BaseFile/env/lib64/python3.6/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "BaseFile/env/lib64/python3.6/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'administrator'
What is wrong with my codes?

Related

Django - separated settings by environment. Not finding variable from base settings

I've split my django environment as per this post.
In settings/base.py I have BASE_DIR specified:
BASE_DIR = Path(__file__).resolve().parent.parent.parent
In settings/__init__.py I have:
from .base import *
env = os.environ['ENV']
...
if env == 'test':
from .test import *
...
In settings/test.py I have DATA_VOLUME_BASE_DIR = BASE_DIR / 'scanned_images'.
I expect that Django loads the settings module, imports settings/base.py, checks the environment ('test'), imports settings/test.py (which works) and the variable is available (which isn't). My stacktrace:
sid_test_web | Traceback (most recent call last):
sid_test_web | File "/code/manage.py", line 22, in <module>
sid_test_web | main()
sid_test_web | File "/code/manage.py", line 18, in main
sid_test_web | execute_from_command_line(sys.argv)
sid_test_web | File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_
sid_test_web | utility.execute()
sid_test_web | File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 363, in execute
sid_test_web | settings.INSTALLED_APPS
sid_test_web | File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 82, in __getattr__
sid_test_web | self._setup(name)
sid_test_web | File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 69, in _setup
sid_test_web | self._wrapped = Settings(settings_module)
sid_test_web | File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 170, in __init__
sid_test_web | mod = importlib.import_module(self.SETTINGS_MODULE)
sid_test_web | File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
sid_test_web | return _bootstrap._gcd_import(name[level:], package, level)
sid_test_web | File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
sid_test_web | File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
sid_test_web | File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
sid_test_web | File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
sid_test_web | File "<frozen importlib._bootstrap_external>", line 883, in exec_module
sid_test_web | File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
sid_test_web | File "/code/my_project/settings/__init__.py", line 9, in <module>
sid_test_web | from .test import *
sid_test_web | File "/code/my_project/settings/test.py", line 39, in <module>
sid_test_web | DATA_VOLUME_BASE_DIR = BASE_DIR / 'scanned_images'
sid_test_web | NameError: name 'BASE_DIR' is not defined
How do I make BASE_DIR variable available?
You have to import the .base setting in your test settings to make them available in test.py
from .base import *
# ... your other test settings
The second thing is to choose which settings you like in the __init__.py.
env = os.environ['ENV']
if env == 'test':
from .test import *
else:
from .base import *

reverse(thing) django rest framework in a viewset

The main question is: What is the rest framework ViewSet friendly way to make the reverse routes?
In a django cookiecutter site I have api_router.py with this
router.register(r"users", UserViewSet)
router.register(r"userprofile", UserProfileViewSet, basename="userprofile")
router.register(r"student", StudentViewSet)
urlpatterns = router.urls
app_name = "api"
print(f"we have routes")
for route in router.urls:
print(route)
print(f"{reverse('student-list') = }")
which errors out saying that reverse can't find student-list
the student model.py has a StudentViewSet with the standard
class StudentViewSet(
RetrieveModelMixin,
CreateModelMixin,
ListModelMixin,
UpdateModelMixin,
GenericViewSet,
):
so it should have the stuff needed to create the student-list
the output of that print statement is showing student-list
**** edited ....***
athenaeum_local_django | <URLPattern '^student/$' [name='student-list']>
athenaeum_local_django | <URLPattern '^student\.(?P<format>[a-z0-9]+)/?$' [name='student-list']>
athenaeum_local_django | <URLPattern '^student/(?P<userprofile__user__username>[^/.]+)/$' [name='student-detail']>
athenaeum_local_django | <URLPattern '^student/(?P<userprofile__user__username>[^/.]+)\.(?P<format>[a-z0-9]+)/?$' [name='student-detail']>
The end of the error is as follows:
ahenaeum_local_django | File "/app/config/urls.py", line 29, in <module>
athenaeum_local_django | path("api/", include("config.api_router")),
athenaeum_local_django | File "/usr/local/lib/python3.9/site-packages/django/urls/conf.py", line 34, in include
athenaeum_local_django | urlconf_module = import_module(urlconf_module)
athenaeum_local_django | File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
athenaeum_local_django | return _bootstrap._gcd_import(name[level:], package, level)
athenaeum_local_django | File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
athenaeum_local_django | File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
athenaeum_local_django | File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
athenaeum_local_django | File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
athenaeum_local_django | File "<frozen importlib._bootstrap_external>", line 850, in exec_module
athenaeum_local_django | File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
athenaeum_local_django | File "/app/config/api_router.py", line 22, in <module>
athenaeum_local_django | print(f"{reverse('student-list') = }")
athenaeum_local_django | File "/usr/local/lib/python3.9/site-packages/django/urls/base.py", line 86, in reverse
athenaeum_local_django | return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
athenaeum_local_django | File "/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py", line 698, in _reverse_with_prefix
athenaeum_local_django | raise NoReverseMatch(msg)
athenaeum_local_django | django.urls.exceptions.NoReverseMatch: Reverse for 'student-list' not found. 'student-list' is not a valid view function or pattern name.
Unexpected API error for athenaeum_local_django (HTTP code 500)
Since you have set an app_name, you will have to use it in reverse, so:
reverse('api:student-list')

ModuleNotFoundError: No module named 'django_countries'

I have a docker django project and I want to use django-countries.
This is my requirements.txt
....
django-countries
django-cities
This is the INSTALLED_APPS in settings.py file
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'django_countries',
... (others)
]
When I run docker-up, I receive the below error:
docker_name | Traceback (most recent call last):
docker_name | File "/app/manage.py", line 22, in <module>
docker_name | main()
docker_name | File "/app/manage.py", line 18, in main
docker_name | execute_from_command_line(sys.argv)
docker_name | File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
docker_name | utility.execute()
docker_name | File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 377, in execute
docker_name | django.setup()
docker_name | File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
docker_name | apps.populate(settings.INSTALLED_APPS)
docker_name | File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
docker_name | app_config = AppConfig.create(entry)
docker_name | File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 90, in create
docker_name | module = import_module(entry)
docker_name | File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
docker_name | return _bootstrap._gcd_import(name[level:], package, level)
docker_name | File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
docker_name | File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
docker_name | File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
docker_name | ModuleNotFoundError: No module named 'django_countries'
Any ideas?
pip install django-countries-with-calling-codes

Django on Docker: How to access a file from views.py

I've been developing a web application under the docker environment. On that way, the approach that views.py in the container access to an other file failed. Following is the logs when the container named ‘web’ run
And, demo/demo_app_views.py is
class TestViews(TemplateView):
template_name='top.html'
with open('usr/src/demo/data/a', 'rb') as data:
b = pickle.load(data)
elder_brother=b['1']
def get_context_data(self, **kwargs):
context=super().get_context_data(**kwargs)
context['brother']=elder_brother
return context
top=TestViews.as_view()
No matter of course, I confirmed the file ‘a’ stored on ‘data’ folder in ‘web’ container
And Dockerfile is
FROM python:3.7-alpine
WORKDIR /usr/src/demo
RUN mkdir /usr/src/demo/staticfiles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apk update \ && apk add postgresql-dev gcc python3-dev musl-dev
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY ./entrypoint.sh .
COPY . .
ENTRYPOINT ["/usr/src/demo/entrypoint.sh"]
and... Docker-compose.yml is
version: '3.7'
services:
web:
build: ./demo
command: gunicorn demo.wsgi:application --bind 0.0.0.0:8000
volumes:
- ./demo/:/usr/src/demo:cached
- static_volume:/usr/src/demo/staticfiles
expose:
- 8000
env_file:
- ./base.env
depends_on:
- db
db:
image: postgres:11.4-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=project_demo
- POSTGRES_PASSWORD=project_demo
- POSTGRES_DB=project_demo_dev
nginx:
build: ./nginx
volumes:
- static_volume:/usr/src/demo/staticfiles
ports:
- 1337:80
depends_on:
- web
volumes:
postgres_data:
static_volume:
and the displaced log after docker-compose up -d --build
web_1 | res = instance.__dict__[self.name] = self.func(instance)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 564, in urlconf_module
web_1 | return import_module(self.urlconf_name)
web_1 | File "/usr/local/lib/python3.7/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 1006, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1 | File "/usr/src/demo/demo/urls.py", line 21, in <module>
web_1 | path('demo_app/', include('demo_app.urls')),
web_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
web_1 | urlconf_module = import_module(urlconf_module)
web_1 | File "/usr/local/lib/python3.7/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 1006, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1 | File "/usr/src/demo/demo_app/urls.py", line 18, in <module>
web_1 | from . import views
web_1 | File "/usr/src/demo/demo_app/views.py", line 27, in <module>
web_1 | class TestViews(TemplateView):
web_1 | File "/usr/src/demo/demo_app/views.py", line 30, in TestViews
web_1 | with open('usr/src/demo/data/a', 'rb') as data:
web_1 | FileNotFoundError: [Errno 2] No such file or directory: 'usr/src/demo/data/a'
web_1 | Traceback (most recent call last):
web_1 | File "manage.py", line 21, in <module>
web_1 | main()
web_1 | File "manage.py", line 17, in main
web_1 | execute_from_command_line(sys.argv)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
web_1 | utility.execute()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
web_1 | self.fetch_command(subcommand).run_from_argv(self.argv)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
web_1 | self.execute(*args, **cmd_options)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 361, in execute
web_1 | self.check()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
web_1 | include_deployment_checks=include_deployment_checks,
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 65, in _run_checks
web_1 | issues.extend(super()._run_checks(**kwargs))
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
web_1 | return checks.run_checks(**kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
web_1 | new_errors = check(app_configs=app_configs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
web_1 | return check_resolver(resolver)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
web_1 | return check_method()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 398, in check
web_1 | for pattern in self.url_patterns:
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
web_1 | res = instance.__dict__[self.name] = self.func(instance)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 571, in url_patterns
web_1 | patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
web_1 | res = instance.__dict__[self.name] = self.func(instance)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 564, in urlconf_module
web_1 | return import_module(self.urlconf_name)
web_1 | File "/usr/local/lib/python3.7/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 1006, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1 | File "/usr/src/demo/demo/urls.py", line 21, in <module>
web_1 | path('demo_app/', include('demo_app.urls')),
web_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
web_1 | urlconf_module = import_module(urlconf_module)
web_1 | File "/usr/local/lib/python3.7/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 1006, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1 | File "/usr/src/demo/demo_app/urls.py", line 18, in <module>
web_1 | from . import views
web_1 | File "/usr/src/demo/demo_app/views.py", line 27, in <module>
web_1 | class TestViews(TemplateView):
web_1 | File "/usr/src/demo/demo_app/views.py", line 30, in TestViews
web_1 | with open('usr/src/demo/data/a', 'rb') as data:
web_1 | FileNotFoundError: [Errno 2] No such file or directory: 'usr/src/demo/data/a'
web_1 | [2020-07-04 07:52:18 +0000] [1] [INFO] Starting gunicorn 20.0.4
web_1 | [2020-07-04 07:52:18 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
web_1 | [2020-07-04 07:52:18 +0000] [1] [INFO] Using worker: sync
web_1 | [2020-07-04 07:52:18 +0000] [10] [INFO] Booting worker with pid: 10
I’m not good in docker, so please tell me how to access the file ‘a’, which is dictionary type file, not database
Thank you
David's advisable comment led the answer and it is to change
open('usr/src/demo...')
to
open('/usr/src/demo...')
Thank David

RUN pip install -r requirements.txt does not install requirements in docker container

I am new to django, docker and scrapy and I am trying to run a django app that also uses scrapy (I basically create a django app that is also a scrapy app and try to call a spider from a django view). Despite specifying this scrapy in requirements.txt and running pip from the Dockerfile, the dependencies are not installed in the container prior to running python manage.py runserver 0.0.0.0:8000 and the django app fails during the system checks, resulting in the web container exiting because of the following exception:
| Exception in thread django-main-thread:
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.7/threading.py", line 926, in _bootstrap_inner
web_1 | self.run()
web_1 | File "/usr/local/lib/python3.7/threading.py", line 870, in run
web_1 | self._target(*self._args, **self._kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
web_1 | self.check(display_num_errors=True)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
web_1 | include_deployment_checks=include_deployment_checks,
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
web_1 | return checks.run_checks(**kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
web_1 | new_errors = check(app_configs=app_configs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
web_1 | all_namespaces = _load_all_namespaces(resolver)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
web_1 | url_patterns = getattr(resolver, 'url_patterns', [])
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
web_1 | res = instance.__dict__[self.name] = self.func(instance)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 579, in url_patterns
web_1 | patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
web_1 | res = instance.__dict__[self.name] = self.func(instance)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 572, in urlconf_module
web_1 | return import_module(self.urlconf_name)
web_1 | File "/usr/local/lib/python3.7/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 1006, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1 | File "/code/composeexample/urls.py", line 21, in <module>
web_1 | path('scrapy/', include('scrapy_app.urls')),
web_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
web_1 | urlconf_module = import_module(urlconf_module)
web_1 | File "/usr/local/lib/python3.7/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 1006, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1 | File "/code/scrapy_app/urls.py", line 4, in <module>
web_1 | from scrapy_app import views
web_1 | File "/code/scrapy_app/views.py", line 1, in <module>
web_1 | from scrapy.crawler import CrawlerProcess
web_1 | ModuleNotFoundError: No module named 'scrapy'
I tried using pip3 instead of pip, pip install --no-cache-dir -r requirements.txt, changing the order of the statements in the Dockerfile and I also checked that Scrapy==1.7.3 appears in requirements.txt. Nothing seems to work.
This is my Dockerfile:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
And this is my docker-compose.yml:
version: '3'
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
A little late, but I came across this issue and eventually figured it out (and am putting this here for anyone else who has the same issue).
The first time I tried building my docker image, my requirements.txt didn't contain the required modules. Of course, I added the required modules but nothing seemed to happen, this is because we need to rebuild the container from scratch, otherwise we are just attempting to build the same version again and again.
To rebuild the container with our updated files, we write:
docker-compose rm -f
docker-compose pull
docker-compose up
If that doesn't work, try the same but replace final line with docker-compose up --build -d.
I got this from this answer.
It seems like you are lacking scrapy in your requirements.txt !
I tried to build a minimal version with all of your components. Hope it helps.
test.py
import scrapy
from time import sleep
def main():
while True:
print(scrapy)
sleep(1)
if __name__ == "__main__":
main()
requirements.txt
Scrapy==1.7.3
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
WORKDIR /code
COPY requirements.txt .
RUN pip3 install -r requirements.txt
COPY . ./
CMD [ "python3", "test.py" ]
docker-compose.yml
version: '3'
services:
db:
image: postgres
web:
build: .
ports:
- "8000:8000"
depends_on:
- db