Importing a custom APM processor in a Python FastAPI app - python

I am using Elasticsearch APM in Python to log requests for a FastAPI app.
There is a mechanism to load custom processors by simply specifying the module path to the custom processor function.
My app layout is as follows:
app
├── __init__.py
├── apm_processors.py
├── main.py
└── version.py
My apm_processors.py file has the following function defined:
def ip_processor(client, event):
# process the event
pass
So, in my app, I do:
# other imports
from elasticapm.contrib.starlette import make_apm_client, ElasticAPM
from . import apm_processors
And to set up APM, I call:
apm_config = {
"SERVICE_NAME": config('ELASTIC_APM_SERVICE_NAME', cast=str, default="Server"),
"SECRET_TOKEN": str(config('ELASTIC_APM_SECRET_TOKEN', cast=Secret, default="")),
"SERVER_URL": config('ELASTIC_APM_SERVER_URL', cast=str, default="http://localhost:8200"),
"PROCESSORS": (
"elasticapm.processors.sanitize_http_request_cookies",
"elasticapm.processors.sanitize_http_headers",
"apm_processors.ip_processor"
)
}
apm = make_apm_client(apm_config)
app = FastAPI()
# other app setup
app.add_middleware(ElasticAPM, client=apm)
This throws an error:
ModuleNotFoundError: No module named 'apm_processors'
With the following stacktrace:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/usr/local/lib/python3.9/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.9/site-packages/uvicorn/subprocess.py", line 76, in subprocess_started
target(sockets=sockets)
File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 60, in run
return asyncio.run(self.serve(sockets=sockets))
File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/local/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
return future.result()
File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 67, in serve
config.load()
File "/usr/local/lib/python3.9/site-packages/uvicorn/config.py", line 458, in load
self.loaded_app = import_from_string(self.app)
File "/usr/local/lib/python3.9/site-packages/uvicorn/importer.py", line 24, in import_from_string
raise exc from None
File "/usr/local/lib/python3.9/site-packages/uvicorn/importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/statisticsserver/./app/main.py", line 75, in <module>
apm = make_apm_client(apm_config)
File "/usr/local/lib/python3.9/site-packages/elasticapm/contrib/starlette/__init__.py", line 70, in make_apm_client
return client_cls(config, **defaults)
File "/usr/local/lib/python3.9/site-packages/elasticapm/base.py", line 153, in __init__
"processors": self.load_processors(),
File "/usr/local/lib/python3.9/site-packages/elasticapm/base.py", line 640, in load_processors
return [seen.setdefault(path, import_string(path)) for path in processors if path not in seen]
File "/usr/local/lib/python3.9/site-packages/elasticapm/base.py", line 640, in <listcomp>
return [seen.setdefault(path, import_string(path)) for path in processors if path not in seen]
File "/usr/local/lib/python3.9/site-packages/elasticapm/utils/module_import.py", line 47, in import_string
module = import_module(module_path)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'apm_processors'
As far as I can tell it uses import_module to import the function as specified, but for some reason it claims there is no such module — although I am actually able to import it.
How can I specify the processor?

Since the context/main module of the application is app, the import should be done as follows:
"PROCESSORS": (
"elasticapm.processors.sanitize_http_request_cookies",
"elasticapm.processors.sanitize_http_headers",
"app.apm_processors.ip_processor"
)
This way, the import succeeds and the function will be called for each event.

Related

when i runserver (python manage.py runserver) this error is raised: TypeError: a bytes-like object is required, not 'str'

I tried deploying my django/React (using postgreSQL) app using Render. The deploy worked but no data is being returned.
Also, when I try to load seed data or run the server from my directory the following error is thrown:
Traceback (most recent call last):
File "/Users/xxx/xxx/xxx/xxx/manage.py", line 22, in <module>
main()
File "/Users/xxx/xxx/xxx/xxx/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/core/management/__init__.py", line 386, in execute
settings.INSTALLED_APPS
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/conf/__init__.py", line 87, in __getattr__
self._setup(name)
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/conf/__init__.py", line 74, in _setup
self._wrapped = Settings(settings_module)
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/conf/__init__.py", line 183, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/Cellar/python#3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/xxx/xxx/xxx/xxx/project/settings.py", line 114, in <module>
'default': dj_database_url.parse(os.environ.get('DATABASE_URL'), conn_max_age=600),
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/dj_database_url.py", line 94, in parse
if "?" in path and not url.query:
TypeError: a bytes-like object is required, not 'str'
Here's part of my settings.py:
DATABASES = {
'default': dj_database_url.parse(os.environ.get('DATABASE_URL'), conn_max_age=600),
}
I've also tried:
DATABASE_URL = os.environ.get('DATABASE_URL')
DATABASES = {
'default': dj_database_url.config()
}
But when I run the server, the app loads but has no data (just like the deployed app).
The code block surrounding line 94 in dj_database_url.py is as follows:
# Split query strings from path.
path = url.path[1:]
if "?" in path and not url.query:
path, query = path.split("?", 2)
else:
path, query = path, url.query
query = urlparse.parse_qs(query)
type(os.environ.get('DATABASE_URL')) returns <class 'NoneType'>
and the value returned for os.environ.get('DATABASE_URL') is None

ImportError: DLL load failed while importing cloneNodes: The parameter is incorrect

I'm facing this issue when I run this command and it creates a makecpython.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("cloneNodes", ["app/v3/impl/cloneNodes.pyx"])]
setup(
name = 'arr',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules,
compiler_directives={'language_level' : "3"}
)
Later I run my file to clone the nodes to this is created, this is the clonenode.py file made:
from cpython cimport *
def cloningNodes(matrix,toClone):
cdef Py_ssize_t matrixSize=PyList_Size(matrix),cloneSize=PyList_Size(toClone),newSize=matrixSize+cloneSize,i,j
output=PyList_New(newSize)
for i in range(matrixSize):
currentMatrixRow=<object>PyList_GET_ITEM(matrix,i)
Py_INCREF(currentMatrixRow)
PyList_SET_ITEM(output,i,
currentMatrixRow)
for j in range(cloneSize):
currentRow=<object>PyList_GET_ITEM(output,i)
PyList_Append(currentRow,<object>PyList_GET_ITEM(
<object>PyList_GET_ITEM(matrix,i),PyLong_AsSsize_t(<object>PyList_GET_ITEM(toClone,j))
)
)
for i in range(cloneSize):
toCloneRow=<object>PyList_GET_ITEM(output,PyLong_AsSsize_t(<object>PyList_GET_ITEM(toClone,i)))
Py_INCREF(toCloneRow)
PyList_SET_ITEM(output,i+matrixSize,toCloneRow)
return output
Now I have to run the uvicorn main app thus instead of showing the link it goes to the dll error:
uvicorn main:app
Traceback (most recent call last):
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\nkgad\AppData\Local\Programs\Python\Python39\Scripts\uvicorn.exe\__main__.py", line 7, in <module>
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\site-packages\click\core.py", line 1128, in __call__
return self.main(*args, **kwargs)
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\site-packages\click\core.py", line 1053, in main
rv = self.invoke(ctx)
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\site-packages\click\core.py", line 1395, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\site-packages\click\core.py", line 754, in invoke
return __callback(*args, **kwargs)
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\site-packages\uvicorn\main.py", line 425, in main
run(app, **kwargs)
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\site-packages\uvicorn\main.py", line 447, in run
server.run()
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\site-packages\uvicorn\server.py", line 68, in run
return asyncio.run(self.serve(sockets=sockets))
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\asyncio\base_events.py", line 642, in run_until_complete
return future.result()
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\site-packages\uvicorn\server.py", line 76, in serve
config.load()
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\site-packages\uvicorn\config.py", line 448, in load
self.loaded_app = import_from_string(self.app)
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\site-packages\uvicorn\importer.py", line 24, in import_from_string
raise exc from None
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\site-packages\uvicorn\importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
File "c:\users\nkgad\appdata\local\programs\python\python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File ".\main.py", line 3, in <module>
from app.api import app
File ".\app\api.py", line 4, in <module>
from .v3.controller.route_info_controller import RouteController
File ".\app\v3\controller\route_info_controller.py", line 2, in <module>
from ..impl.vrp_impl import VRPImpl
File ".\app\v3\impl\vrp_impl.py", line 6, in <module>
from .open_route_map_impl import OpenRouteImpl
File ".\app\v3\impl\open_route_map_impl.py", line 2, in <module>
from cloneNodes import cloningNodes
ImportError: DLL load failed while importing cloneNodes: The parameter is incorrect.
How can I resolve this DLL load failure issue?

ModuleNotFoundError: No module named 'api.serlializer'

I've installed Django 3.1.4 and djangorestframework 3.12.2.
I am attempting to run makemigrations for a music website I am building and I keep getting this error in the terminal and haven't been able to figure out what is causing it.
(Django_React) PS C:\Users\BB_SO\desktop\dev\Django_React\music_site> python .\manage.py makemigrations 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 "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\management\base.py", line 368, in execute
self.check()
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\management\base.py", line 396, in check
databases=databases,
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\BB_SO\anaconda3\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\BB_SO\desktop\dev\Django_React\music_site\music_site\urls.py", line 19, in <module>
urlpatterns = [path("admin/", admin.site.urls), path("", include("api.urls"))]
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\BB_SO\anaconda3\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\BB_SO\desktop\dev\Django_React\music_site\api\urls.py", line 2, in <module>
from .views import RoomView
File "C:\Users\BB_SO\desktop\dev\Django_React\music_site\api\views.py", line 3, in <module>
from .serlializer import RoomSerializer
ModuleNotFoundError: No module named 'api.serlializer'
Here my serializer.py file content:
from rest_framework import serializer
from .models import Room
class RoomSeralizer(seriarlizers.ModelSerializer):
class Meta:
model = Room
fields = (
"id",
"code",
"host",
"guest_can_pause",
"vote_to_skip",
"created_at",
)
Does anyone know what might be causing this error? I am still very new to coding and am using vscode if that helps at all. Thanks in advance for your help!
In serializer.py Try:
from rest_framework import serializers
Django-rest framework doesn't have any API called serializer.
More: https://www.django-rest-framework.org/api-guide/serializers/
import should be
from rest_framework import serializers

'tuple' object is not callable , machine learning API using Django API

I am trying build a machine learning API using DJANGO rest framework API. After creating a project and configuring model.py of created project 'MyAPI' ,for required approvals. And used the following commend to run the server, got following error.
how to fix them ?, Please guide me.
````C:\Users\Padmini\DjangoAPI>python manage.py runserver```
error
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Padmini\DjangoAPI\MyAPI\models.py", line 4, in <module>
class approvals(models.Model):
File "C:\Users\Padmini\DjangoAPI\MyAPI\models.py", line 7, in approvals
('Female','Female')
TypeError: 'tuple' object is not callable````
my model.py as following code
from django.db import models
# Create your models here.
class approvals(models.Model):
GENDER_CHOICES= (
('Male', 'Male')
('Female','Female')
)
MARRIED_CHOICES=(
('Yes','Yes'),
('No','No')
)
GRADUATED_CHOICES= (
('Graduate','Graduated'),
('Not_Graduate','Not_Graduate')
)
SELFEMPLOYED_CHOICES=(
('Yes','Yes'),
('No','No')
)
PROPERTY_CHOICES=(
('Rural','Rural'),
('Semiurban','Semiurban'),
('Urban','Urban')
)
firstname=models.CharField(max_length=15)
lastname=models.CharField(max_length=15)
dependants=models.IntegerField(default=0)
applicantincome=models.IntegerField(default=0)
coapplicatincome=models.IntegerField(default=0)
loanamt=models.IntegerField(default=0)
loanterm=models.IntegerField(default=0)
credithistory=models.IntegerField(default=0)
gender=models.CharField(max_length=15, choices=GENDER_CHOICES)
married=models.CharField(max_length=15, choices=MARRIED_CHOICES)
graduatededucation=models.CharField(max_length=15, choices=GRADUATED_CHOICES)
selfemployed=models.CharField(max_length=15, choices=SELFEMPLOYED_CHOICES)
area=models.CharField(max_length=15, choices=PROPERTY_CHOICES)
def __str__(self):
return '{}, {}'.format(self.lastname, self.firstname)`````

How i fix this Unable to import 'django.urls' error?

Errors are:
1. No name 'urls' in module 'django'pylint(no-name-in-module)
Unable to import 'django.urls'
2. No name 'test' in module 'django'pylint(no-name-in-module)
Unable to import 'django.test'pylint(import-error)
3. No name 'http' in module 'django'pylint(no-name-in-module)
Unable to import 'django.http'pylint(import-error)
When I start the server the terminal shows me this -
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "c:\users\aviba\appdata\local\programs\python\python38-32\Lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "c:\users\aviba\appdata\local\programs\python\python38-32\Lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\core\management\base.py", line 387, in check
all_issues = self._run_checks(
File "C:\Users\aviba\Envs\test\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\aviba\Envs\test\lib\site-packages\django\urls\resolvers.py", line 399, in check
for pattern in self.url_patterns:
File "C:\Users\aviba\Envs\test\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\urls\resolvers.py", line 584, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\urls\resolvers.py", line 577, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\aviba\Envs\test\lib\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 "C:\Users\aviba\djangoproject\firsttry\firsttry\urls.py", line 20, in <module>
path('',include('firstapp.urls')),
File "C:\Users\aviba\Envs\test\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\aviba\Envs\test\lib\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 "C:\Users\aviba\djangoproject\firsttry\firstapp\urls.py", line 2, in <module>
from . import views
File "C:\Users\aviba\djangoproject\firsttry\firstapp\views.py", line 2, in <module>
from django.http import httpresponse
ImportError: cannot import name 'httpresponse' from 'django.http' (C:\Users\aviba\Envs\test\lib\site-packages\django\http\__init__.py)
urls.py of the app
from django.urls import path
from . import views
urlpattarns = [
path('',views.home, name='home'),
]
views.py of the app
from django.shortcuts import render
from django.http import httpresponse
def home(request):
return httpresponse("Hello Django")
urls.py of the project
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('',include('firstapp.urls')),
path('admin/', admin.site.urls),
]
modules in python are case sensitive, it's HttpResponse not httpresponse
Try this as views.py:
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello Django")
Here httpresponse is case sensitive and hence we use it this way.
Read this it might help you more.
Make sure you are using Python3 as interpreter. I had the same issue (using Django 3.1.2) and after selecting the right interpreter (python 3.7.8) it was fixed.

Categories

Resources