Django and Stripe, Module 'stripe' has no 'Customer' member

Viewed 323

so I'm trying to create a customer in stripe using django, according to the documentation in stripe, the code stripe.Customer.create should work, but it just appears this error, can someone tell me why is this happening?

views.py

from django.shortcuts import render, redirect
from django.urls import reverse
from django.http import JsonResponse

import stripe
# Create your views here.
stripe.api_key = "xxxx"

def index(request):
    return render(request, 'sales/index.html')

def charge(request):
    amount = 5
    if request.method == 'POST':
        print('Data', request.POST)

        stripe.Customer.create(
            email=request.POST['email']
        )

    return redirect(reverse('success', args=[amount]))

def successMsg(request, args):
    amount = args
    return render(request, 'sales/success.html', {'amount':amount})


Traceback

Traceback (most recent call last):
  File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\snin2\Desktop\django braintree\stripe\sales\views.py", line 17, in charge
    stripe.Customer.create(
AttributeError: module 'stripe' has no attribute 'Customer'
[18/Sep/2020 08:36:39] "POST /charge/ HTTP/1.1" 500 66773


2 Answers

The one and only reason for this error is you might have either a stripe.py file or a module in your project directory.

and, yes, you have a package named stripe under django braintree directory.

\django braintree\stripe\sales\views.py

How can we verify that?

You can check the location of the module by inspecting the .__file__ attribute of the module.

import stripe
print(stripe.__file__)
            ^^^^^^^^^^^

References

  1. Python Stripe: 'module' object has no attribute 'Charge' -- (StackOverlofw)
  2. AttributeError: 'module' object has no attribute 'Charge' -- (GitHub)

You have named your project/app as stripe which makes conflict with the stripe package you installed

Change you project/app name to a different name and try again

NOTE: avoid using package name as app name or project name, this will create issues during referencing

Related