Python3环境下导入django-pagseguro的pagseguro_urlpatterns报错求助
Hey there! Let's break down what's going on here and how to fix this issue.
The Root Cause
The error you're hitting happens because django.conf.urls.patterns was removed in Django 1.10, and the version of django-pagseguro you have installed was built for older Django versions that still included this module. When you try to import pagseguro_urlpatterns, the package attempts to pull in patterns—which no longer exists in your current Django setup—triggering the ImportError.
Solutions to Try
1. Upgrade django-pagseguro (Recommended)
First, check if there's an updated version of django-pagseguro that supports your Django version. Run this command in your terminal:
pip install --upgrade django-pagseguro
After upgrading, try importing pagseguro_urlpatterns again in your Python shell. Most modern versions of the package have already dropped the use of patterns, so this should resolve the issue right away.
2. Manually Patch the urls.py File (If Upgrade Isn't an Option)
If upgrading doesn't work (or if no compatible version exists), you can manually fix the package's urls.py file:
- Locate the installation path of
django-pagseguroin your virtual environment. From your error message, it's:C:\Users\patff\AppData\Local\conda\conda\envs\k36\lib\site-packages\django_pagseguro\urls.py - Open this file in a text editor and make these changes:
- Replace the import line:
With the appropriate line for your Django version:from django.conf.urls import patterns, url# For Django 1.10 to 1.11 from django.conf.urls import url # OR for Django 2.0+ (use re_path for regex-based URLs) from django.urls import re_path - Rewrite the
urlpatternsfrom usingpatterns()to a regular list:
For Django 1.10-1.11:
For Django 2.0+, it's better to import views directly instead of using string references:urlpatterns = [ url(r'^retorno/$', 'django_pagseguro.views.retorno', name='pagseguro_retorno'), url(r'^notificacao/$', 'django_pagseguro.views.notificacao', name='pagseguro_notificacao'), ]from django_pagseguro.views import retorno, notificacao urlpatterns = [ re_path(r'^retorno/$', retorno, name='pagseguro_retorno'), re_path(r'^notificacao/$', notificacao, name='pagseguro_notificacao'), ]
- Replace the import line:
- Save the file and try importing again. This should bypass the missing
patternserror.
3. Downgrade Django (Not Recommended)
As a last resort, you could downgrade Django to a version that still supports patterns (like Django 1.9). But this is not ideal—older Django versions often have unpatched security vulnerabilities. If you must go this route, run:
pip install django==1.9
内容的提问来源于stack exchange,提问作者Patricia

