编程语言
首页 > 编程语言> > python-将Stripe导入Django-NameError

python-将Stripe导入Django-NameError

作者:互联网

我似乎无法弄清楚如何将Stripe导入到我的Django项目中.我正在运行Python 2.7.3,并且不断收到

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

即使当我刚打开终端并输入python时,

>>> import stripe

我收到:

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

但是,如果我在目录中:

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

然后,上面的import stripe命令成功导入了Stripe.但是,正如我已经说过的,Django不知道如何导入Stripe.该问题可能是某个地方的路径参考吗?我以为这可能是Python版本的问题,但是无论我键入python2.6还是python2.7然后导入Stripe,除非我在上面的那个目录中,否则它仍然不起作用.我是否需要在我的INSTALLED_APPS或项目的settings.py文件中的某处包括条带?

我按照他们的文档安装了Stripe:

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

其中回应:

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...

在这一点上,我不确定该怎么做.以下是Django追溯.

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

编辑:

import sys
print sys.path
import stripe

退货

['/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']

按照答案,我还尝试了pip install –index-url https://code.stripe.com –upgrade stripe

这是我收到的:

要求已经是最新的:./stripe-1.7.2-py2.7.egg中的条带
要求已经是最新的:/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages中的request> = 0.8.8(来自条带)
要求已经是最新的:/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages中的certifi> = 0.0.4(来自请求> = 0.8.8->条)
打扫干净…

解决方法:

这似乎是在与pip使用的python解释器不同的python解释器下运行django dev服务器的情况. “ site-packages”位置应该自动位于正确解释器的路径中.

您无需在INSTALLED_APPS中添加“条带”即可解决无法导入的事实.您可以添加它以使其包含在应用程序中,并考虑其所有模块,例如其models.py.

根据您的评论,您将使用python的macports安装作为默认安装,但是当使用sudo命令安装stripe时,其安装将使用系统python.首先,您应该尝试不使用sudo进行pip安装.或确保PYTHONPATH中始终始终包含/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages,以同时包含系统软件包和macport.

或者,您可以尝试使用sudo并使用您想要的python的显式路径,这样环境就不会为您猜测:sudo / opt / local / bin / pip install …

虚拟环境

您正在遇到的问题使此问题成为使用virtualenv的理想选择.使用virtualenv所获得的是一个完全隔离的环境,始终使用相同的python interpeter,并具有本地化的站点包.安装stripe会将其放置在该本地库中.

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

virtualenv还可以让您明确告诉它要使用哪个python,而不是默认值:

virtualenv --no-site-packages -p /opt/local/bin/python myproject

标签:stripe-payments,python,django
来源: https://codeday.me/bug/20191201/2078856.html