Importing Stripe into Django - NameError

2024/11/16 16:28:49

I can't seem to figure out how to import Stripe into my Django project. I'm running Python 2.7.3 and I keep receiving

NameError at /complete/ global name. 'stripe' is not defined.

Even when I just open up Terminal and type python then

>>> import stripe

I receive:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named stripe

However, if I'm in the directory:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

Then the above import stripe command successfully imports Stripe. However, as I've stated, Django does not know how to import Stripe. Might the issue be a path reference somewhere? I thought it might be a Python version issue, but regardless of whether I type python2.6 or python2.7 and then import stripe, it still does not work unless I'm in that directory above. Do I need to include stripe in my INSTALLED_APPS or somewhere in my project's settings.py file?

I installed Stripe as per their documentation:

 sudo pip install --index-url https://code.stripe.com --upgrade stripe

Which responds with:

Requirement already up-to-date: stripe in ./stripe-1.7.2-py2.7.egg
Requirement already up-to-date: requests>=0.8.8 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from stripe)
Requirement already up-to-date: certifi>=0.0.4 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from requests>=0.8.8->stripe)
Cleaning up...


At this point, I'm not quite sure what to do. Below is the Django traceback.


Environment:Request Method: POSTRequest URL: http://localhost:8000/complete/Django Version: 1.5.dev20120523102003
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.sites','django.contrib.messages','django.contrib.staticfiles','django.contrib.flatpages','django.contrib.admin','catalog','utils','cart','checkout')
Installed Middleware:
('django.middleware.common.CommonMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.contrib.flatpages.middleware.FlatpageFallbackMiddleware')Traceback:
File "/Users/Diepenbrock/django-trunk/django/core/handlers/base.py" in get_response111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/Diepenbrock/Documents/django_/ecomstore/checkout/views.py" in complete_order15.   stripe.api_key = "wXvAcOY6Rferd6oYNsc7Qi82aMm1KwyP"Exception Type: NameError at /complete/
Exception Value: global name 'stripe' is not defined

EDIT:

import sys
print sys.path
import stripe

returns

['/Users/Diepenbrock/Documents/django_/ecomstore', '/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg', '/Users/Diepenbrock/django-trunk', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages']



As per an answer, I also tried pip install --index-url https://code.stripe.com --upgrade stripe


This what I received:


Requirement already up-to-date: stripe in ./stripe-1.7.2-py2.7.egg Requirement already up-to-date: requests>=0.8.8 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from stripe) Requirement already up-to-date: certifi>=0.0.4 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from requests>=0.8.8->stripe) Cleaning up...

Answer

This appears to be a situation of running the django dev server under a different python interpreter than the one being used by pip. The "site-packages" location should automatically be in your path for the correct interpreter.

You should not need to add "stripe" to the INSTALLED_APPS to solve the fact that it can't be imported. You would add it for it to be included as an app and have all of its modules considered, like its models.py for instance.

Based on your comments, you are using a macports install of python as your default, but when you use the sudo command to install stripe, its using your system python. First off, you should try to pip install without the sudo. Or make sure /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages is always in your PYTHONPATH to include both system packages and your macports.

Or, you can try using sudo with an explicit path to the python you want, so the environment doesn't guess for you: sudo /opt/local/bin/pip install ...

Virtualenv

What you are running into makes this problem a perfect candidate for making use of virtualenv. What you gain from using virtualenv is a completely isolated environment, using the same python interpeter all the time, and having a localized site-packages. Installing stripe would place it in that local lib.

pip install virtualenv
virtualenv --no-site-packages myproject
cd myproject
source bin/activate
pip install --index-url https://code.stripe.com --upgrade stripe

virtualenv lets you also explicitly tell it which python to use as opposed to the default:

virtualenv --no-site-packages -p /opt/local/bin/python myproject
https://en.xdnf.cn/q/71648.html

Related Q&A

getting line-numbers that were changed

Given two text files A,B, what is an easy way to get the line numbers of lines in B not present in A? I see theres difflib, but dont see an interface for retrieving line numbers

How to subclass a subclass of numpy.ndarray

Im struggling to subclass my own subclass of numpy.ndarray. I dont really understand what the problem is and would like someone to explain what goes wrong in the following cases and how to do what Im t…

How to ignore an invalid SSL certificate with requests_html?

So basically Im trying to scrap the javascript generated data from a website. To do this, Im using the Python library requests_html. Here is my code :from requests_html import HTMLSession session = HTM…

Fabric asks for root password

I am using Fabric to run the following:def staging():""" use staging environment on remote host"""env.user = ubuntuenv.environment = stagingenv.hosts = [host.dev]_setup_pa…

Beautifulsoup results to pandas dataframe

The below code returns me a table with the following resultsr = requests.get(url) soup = bs4.BeautifulSoup(r.text, lxml)mylist = soup.find(attrs={class: table_grey_border}) print(mylist)results - it st…

XGBoost CV and best iteration

I am using XGBoost cv to find the optimal number of rounds for my model. I would be very grateful if someone could confirm (or refute), the optimal number of rounds is: estop = 40res = xgb.cv(params, d…

Whats the correct way to implement a metaclass with a different signature than `type`?

Say I want to implement a metaclass that should serve as a class factory. But unlike the type constructor, which takes 3 arguments, my metaclass should be callable without any arguments:Cls1 = MyMeta()…

Python -- Regex -- How to find a string between two sets of strings

Consider the following:<div id=hotlinklist><a href="foo1.com">Foo1</a><div id=hotlink><a href="/">Home</a></div><div id=hotlink><a…

Kivy TextInput horizontal and vertical align (centering text)

How to center a text horizontally in a TextInput in Kivy?I have the following screen:But I want to centralize my text like this:And this is part of my kv language:BoxLayout: orientation: verticalLabe…

How to capture python SSL(HTTPS) connection through fiddler2

Im trying to capture python SSL(HTTPS) connections through Fiddler2 local proxy. But I only got an error.codeimport requests requests.get("https://www.python.org", proxies={"http": …