ImportError: No module named sentry_sdk.integrations.wsgi - django

I'm trying to upgrade the implementation of the old Sentry config to the new one for my wsgi configuration in my django project but I'm getting the following error.
ImportError: No module named sentry_sdk.integrations.wsgi
Earlier I had:
from raven.contrib.django.raven_compat.middleware.wsgi import Sentry
Now I have:
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
My sentry init is in settings.py file as:
sentry_sdk.init(
dsn=SENTRY_CONFIG.get("dsn", ""),
environment=SENTRY_CONFIG["environment"],
integrations=[
DjangoIntegration(),
CeleryIntegration(),
sentry_logging,
],
attach_stacktrace=True,
send_default_pii=True,
)
Using:
Django==4.0.3
sentry-sdk==1.5.8
python3.8
Any help is appreciated, thanks in advance!

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
sentry_environment = env('SENTRY_ENVIRONMENT',default='local')
sentry_sdk.init(
dsn=env('SENTRY_DSN'),
integrations=[DjangoIntegration()],
traces_sample_rate=1.0,
# If you wish to associate users to errors (assuming you are using
# django.contrib.auth) you may enable sending PII data.
send_default_pii=True,
environment=sentry_environment,
)

Related

ResolutionImpossible - Conflicting dependencies while deploying on heroku

While deploying a Django + React project on Heroku, this error occoured:
The conflict is caused by:
djoser 2.1.0 depends on social-auth-app-django<5.0.0 and >=4.0.0
rest-social-auth 8.0.0 depends on social-auth-app-django<6.0 and >=5.0
If I downgrade to social-auth-app-django==4.0.0 pkg, then get this error:
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: WSGI application 'backend.wsgi.application' could not be loaded; Error importing module.
This error is caused by social_django which is added in settings.py
MIDDLEWARE = [
....
# For social auth
'social_django.middleware.SocialAuthExceptionMiddleware',
....
]
Fixed this error by removing/commenting it out, then found another one:
cannot import name 'urlquote' from 'django.utils.http' (lib\site-packages\django\utils\http.py)
Because urlquote() is no longer available in Django 4.0+ versions, after downgrading social-auth-app-django==4.0.0
pkg.
This try to import from django.utils.http import urlquote in filelib\site-packages\social_django\context_processors.py.
I'm in Dependency hell. I have even tried to downgrading the djoser pkg, then got other errors.
After searching a lot, I found this blog post, according to this:
First, pip install pip-tools then create a requirements.in file and add
django
djangorestframework
then run pip-compile ./requirements.in this will generate requirements.txt file:
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# pip-compile ./requirements.in
#
asgiref==3.6.0
# via django
django==4.1.5
# via
# -r ./requirements.in
# djangorestframework
djangorestframework==3.14.0
# via -r ./requirements.in
pytz==2022.7.1
# via djangorestframework
sqlparse==0.4.3
# via django
tzdata==2022.7
# via django
But this file doesn't contain other packages like:
django-cors-headers,
djoser,
PyJWT
rest-social-auth
social-auth-app-django,
etc....
Please help me with this question, any resource that can help me.

ImportError: No module named moves

Versions
Python : 2.7.14
six : 1.9.0 & 1.11.0(tried on both)
OS : mac(10.13.3) & ubuntu(16.04) [tried on both]
Error
from six.moves import http_client
ImportError: No module named moves
Description
In flask application which is running on google app engine while running it on local system using dev_appserver.py getting above error while importing from six.moves import http_client
What I have tried
After importing six have tried dir(six) which shows that moves is there in list but it's not able to import it which is very strange.
Output of six.__version__: 1.11.0
Output if dir(six)
['/opt/tribes-backend', '/opt/tribes-backend/lib1', '/usr/lib/google-cloud-sdk/platform/google_appengine', '/usr/lib/google-cloud-sdk/platform/google_appengine', '/usr/lib/python2.7', '/usr/lib/python2.7/lib-dynload', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/ssl-2.7.11', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/grpcio-1.0.0', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/six-1.9.0', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/protobuf-3.0.0', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/enum-0.9.23', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/futures-3.0.5', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/setuptools-36.6.0', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/pytz-2017.2', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.3', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/webob-1.1.1', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/werkzeug-0.11.10', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/yaml-3.10', '/usr/local/lib/python2.7/dist-packages/enum', '/usr/lib/google-cloud-sdk/platform/google_appengine/lib/concurrent/concurrent', '/usr/local/lib/python2.7/dist-packages/concurrent', '/usr/local/lib/python2.7/dist-packages/google', '/usr/lib/google-cloud-sdk/platform/google_appengine/google']
['BytesIO', 'Iterator', 'MAXSIZE', 'Module_six_moves_urllib', 'Module_six_moves_urllib_error', 'Module_six_moves_urllib_parse', 'Module_six_moves_urllib_request', 'Module_six_moves_urllib_response', 'Module_six_moves_urllib_robotparser', 'MovedAttribute', 'MovedModule', 'PY2', 'PY3', 'StringIO', '_LazyDescr', '_LazyModule', '_MovedItems', '_SixMetaPathImporter', 'author', 'builtins', 'doc', 'file', 'name', 'package', 'path', 'version', '_add_doc', '_assertCountEqual', '_assertRaisesRegex', '_assertRegex', '_func_closure', '_func_code', '_func_defaults', '_func_globals', '_import_module', '_importer', '_meth_func', '_meth_self', '_moved_attributes', '_print', '_urllib_error_moved_attributes', '_urllib_parse_moved_attributes', '_urllib_request_moved_attributes', '_urllib_response_moved_attributes', '_urllib_robotparser_moved_attributes', 'absolute_import', 'add_metaclass', 'add_move', 'advance_iterator', 'assertCountEqual', 'assertRaisesRegex', 'assertRegex', 'b', 'binary_type', 'byte2int', 'callable', 'class_types', 'create_bound_method', 'exec_', 'functools', 'get_function_closure', 'get_function_code', 'get_function_defaults', 'get_function_globals', 'get_method_function', 'get_method_self', 'get_unbound_function', 'indexbytes', 'int2byte', 'integer_types', 'iterbytes', 'iteritems', 'iterkeys', 'iterlists', 'itertools', 'itervalues', 'moves', 'next', 'operator', 'print_', 'python_2_unicode_compatible', 'raise_from', 'remove_move', 'reraise', 'string_types', 'sys', 'text_type', 'types', 'u', 'unichr', 'viewitems', 'viewkeys', 'viewvalues', 'with_metaclass', 'wraps'].
As it can be seen from above output moves inside six still giving error while importing it.
Spent a lot of time on this and no solution till now any help would be greatly appreciated here. :(
UPDATE1
Error stacktrace :-
from google.cloud.datastore import helpers
File "/opt/tribes-backend/denv/local/lib/python2.7/site-packages/google/cloud/datastore/helpers.py", line 27, in <module>
from google.cloud._helpers import _datetime_to_pb_timestamp
File "/opt/tribes-backend/denv/local/lib/python2.7/site-packages/google/cloud/_helpers.py", line 30, in <module>
from six.moves import http_client
ImportError: No module named moves
For me the issue was resolved by following it on https://github.com/googleapis/python-ndb/issues/249
andrewsg commented 11 days ago:
I think we've identified an issue with devappserver related to the six library specifically. Could you please try a workaround? Add the line: import six; reload(six) to the top of your app, before NDB is loaded and let me know if that works.
Based on follow up with google support team have figured out that communicating with datastore using google-cloud-datastore is deprecated
instead using ndb for communicating datastore is the way to go.
Updated documentation stating deprecation using client datastore library is documented here
Documentation to getting started with ndb client library in python is documented here
In some cases if you want to use some python pure libraries, like six, available in your applications you will need to use third-party libraries.
Follow the instructions here, to add a third-party library, until the command pip install -t lib -r requirements.txt. In your requirements.txt file just add six==1.11.0.
It solved the problem for me.

Add method imports to shell_plus

In shell_plus, is there a way to automatically import selected helper methods, like the models are?
I often open the shell to type:
proj = Project.objects.get(project_id="asdf")
I want to replace that with:
proj = getproj("asdf")
Found it in the docs. Quoted from there:
Additional Imports
In addition to importing the models you can specify other items to
import by default. These are specified in SHELL_PLUS_PRE_IMPORTS and
SHELL_PLUS_POST_IMPORTS. The former is imported before any other
imports (such as the default models import) and the latter is imported
after any other imports. Both have similar syntax. So in your
settings.py file:
SHELL_PLUS_PRE_IMPORTS = (
('module.submodule1', ('class1', 'function2')),
('module.submodule2', 'function3'),
('module.submodule3', '*'),
'module.submodule4'
)
The above example would directly translate to the following python
code which would be executed before the automatic imports:
from module.submodule1 import class1, function2
from module.submodule2 import function3
from module.submodule3 import *
import module.submodule4
These symbols will be available as soon as the shell starts.
ok, two ways:
1) using PYTHONSTARTUP variable (see this Docs)
#in some file. (here, I'll call it "~/path/to/foo.py"
def getproj(p_od):
#I'm importing here because this script run in any python shell session
from some_app.models import Project
return Project.objects.get(project_id="asdf")
#in your .bashrc
export PYTHONSTARTUP="~/path/to/foo.py"
2) using ipython startup (my favourite) (See this Docs,this issue and this Docs ):
$ pip install ipython
$ ipython profile create
# put the foo.py script in your profile_default/startup directory.
# django run ipython if it's installed.
$ django-admin.py shell_plus

Importing CSV to Django and settings not recognised

So i'm getting to grips with Django, or trying to. I have some code that isn't dependent on being called by the webpage - it's designed to populate the database with information. Eventually it will be set up as a cron job to run overnight. This is the first crack at it, which is to do an initial population (once I have that working, I'll move to an add structure, where only new records are pushed.) I'm using Python 2.7, Django 1.5 and Sqlite3. When I run this code, I get
Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
That seems fairly obvious, but I've spent a couple of hours now trying to work out how to adjust that setting. How do I call / open a connection / whatever the right terminology is here? I have a number of functions like this that will be scheduled jobs, and this has been frustrating me all afternoon.
import urllib2
import csv
import requests
from django.db import models
from gmbl.models import Match
master_data_file = urllib2.urlopen("http://www.football-data.co.uk/mmz4281/1213/E0.csv", "GET")
data = list(tuple(rec) for rec in csv.reader(master_data_file, delimiter=','))
for row in data:
current_match = Match(matchdate=row[1],
hometeam=row[2],
awayteam = row [3],
homegoals = row [4],
awaygoals = row[5],
homeshots = row[10],
awayshots = row[11],
homeshotsontarget = row[12],
awayshotsontarget = row[13],
homecorners = row[16],
awaycorners = row[17])
current_match.save()
I had originally started out with http://django-csv-importer.readthedocs.org/en/latest/ but I had the same error, and the documentation doesn't make much sense trying to debug it. When I tried calling settings.configure in the function, it said it didn't exist; presumably I had to import it, but couldn't make that work.
Make sure Django, and your project are in PYTHONPATH then you can do:
import urllib2
import csv
import requests
from django.core.management import setup_environ
from django.db import models
from yoursite import settings
setup_environ(settings)
from gmbl.models import Match
master_data_file = urllib2.urlopen("http://www.football-data.co.uk/mmz4281/1213/E0.csv", "GET")
data = list(tuple(rec) for rec in csv.reader(master_data_file, delimiter=','))
# ... your code ...
Reference: http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/
Hope it helps!

cannot install django-filebrowser app

I did exactly as it says here: http://readthedocs.org/docs/django-filebrowser/en/latest/quickstart.html#quickstart (only used easy_install instead of pip)
it seems that I get an import error when trying to connect to admin interface:
Request Method: GET
Request URL: http://localhost:8000/admin/
Django Version: 1.3
Exception Type: ImportError
Exception Value:
No module named sites
Exception Location: c:\workspace\expedeat\..\expedeat\urls.py in <module>, line 5
Python Executable: c:\Tools\Python26\python.exe
Python Version: 2.6.4
the import the exception comes from is: from filebrowser.sites import site in urls.py
Also testing filebrowser fails with this message:
Creating test database for alias 'default'...
.......F......
======================================================================
FAIL: test_directory (filebrowser.tests.settings.SettingsTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\Tools\Python26\lib\site-packages\django_filebrowser-3.3.0-py2.6.egg\filebrowser\tests\set
tings.py", line 29, in test_directory
self.assertEqual(os.path.exists(os.path.join(MEDIA_ROOT,DIRECTORY)), 1)
AssertionError: False != 1
----------------------------------------------------------------------
Ran 14 tests in 0.008s
FAILED (failures=1)
Destroying test database for alias 'default'...
I must be doing something wrong. Any help would be appreciated.
the sites.py module does not exist in version you are using, so the error message is correct.
the installation doc you are using is for version 3.4. The pip install is 3.3. The difference being in the urls.py
3.4
from filebrowser.sites import site
urlpatterns = patterns('',
url(r'^admin/filebrowser/', include(site.urls)),
)
3.3
urlpatterns = patterns('',
(r'^admin/filebrowser/', include('filebrowser.urls')),
)
The test fails because it's looking for a non-existing directory.
To find out what directory it's looking for, do the following:
% python ./manage.py shell
>>> from django.conf import settings
>>> import filebrowser.settings
>>> filebrowser.settings.MEDIA_ROOT
'/srv/repositories/project/media'
>>> filebrowser.settings.DIRECTORY
'uploads/'
Based on the output, you know what directory it's looking for, /srv/repositories/project/media/uploads in this example. Create the directory and you should be one step further on your way.
Using the "FILEBROWSER_" prefix, you can supply configuration for the filebrowser app.
I use the following in my settings.py :
FILEBROWSER_DIRECTORY = MEDIA_ROOT