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: POST
Request 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_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/Diepenbrock/Documents/django_/ecomstore/checkout/views.py" in complete_order
15. 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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With