You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3环境下导入django-pagseguro的pagseguro_urlpatterns报错求助

Fixing the "ImportError: cannot import name 'patterns'" with django-pagseguro

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-pagseguro in 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:
    1. Replace the import line:
      from django.conf.urls import patterns, url
      
      With the appropriate line for your Django version:
      # 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
      
    2. Rewrite the urlpatterns from using patterns() to a regular list:
      For Django 1.10-1.11:
      urlpatterns = [
          url(r'^retorno/$', 'django_pagseguro.views.retorno', name='pagseguro_retorno'),
          url(r'^notificacao/$', 'django_pagseguro.views.notificacao', name='pagseguro_notificacao'),
      ]
      
      For Django 2.0+, it's better to import views directly instead of using string references:
      from django_pagseguro.views import retorno, notificacao
      
      urlpatterns = [
          re_path(r'^retorno/$', retorno, name='pagseguro_retorno'),
          re_path(r'^notificacao/$', notificacao, name='pagseguro_notificacao'),
      ]
      
  • Save the file and try importing again. This should bypass the missing patterns error.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:50:52