Python 将条带导入Django-NameError
我似乎不知道如何将Stripe导入我的Django项目。我正在运行Python2.7.3,并且不断收到Python 将条带导入Django-NameError,python,django,stripe-payments,Python,Django,Stripe Payments,我似乎不知道如何将Stripe导入我的Django项目。我正在运行Python2.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>
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
然后,上面的导入条带
命令成功导入条带。然而,正如我所说的,Django不知道如何导入Stripe。问题可能是某个地方的路径引用?我认为这可能是Python版本的问题,但无论我是键入python2.6
还是python2.7
,然后再键入import stripe
,它仍然无法工作,除非我在上面的目录中。我是否需要在已安装的_应用程序或项目的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安装——索引urlhttps://code.stripe.com --升级条带
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
这是我收到的:
要求已更新:条带输入。/stripe-1.7.2-py2.7.2
需求已经是最新的:请求>=0.8.8 in/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages(来自stripe)
要求已更新:certifi>=0.0.4 in/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages(来自请求>=0.8.8->stripe)
清理…
这似乎是在与pip使用的python解释器不同的python解释器下运行django dev服务器的情况。“站点包”位置应自动位于正确解释器的路径中
您不需要在已安装的应用程序中添加“条带”,以解决无法导入的问题。您可以将其添加为应用程序,并考虑其所有模块,例如models.py
根据您的评论,您使用macports安装python作为默认设置,但是当您使用sudo
命令安装stripe时,它使用的是您的系统python。首先,您应该尝试在没有sudo的情况下进行pip安装。或者确保PYTHONPATH中始终包含/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site包
,以同时包含系统包和macports
或者,您可以尝试使用sudo,并带有指向所需python的显式路径,这样环境就不会为您猜测:sudo/opt/local/bin/pip install…
Virtualenv
您所遇到的问题使此问题成为利用的最佳候选。使用virtualenv所获得的是一个完全隔离的环境,始终使用相同的python interpeter,并且具有本地化的站点包。安装stripe将把它放在本地lib中
pip安装virtualenv
virtualenv--无站点包myproject
cd myproject
源箱/激活
pip安装--索引urlhttps://code.stripe.com --升级条带
virtualenv还允许您显式地告诉它使用哪种python,而不是默认的:
virtualenv--无站点包-p/opt/local/bin/python myproject
请在导入条带之前查看python路径:
import sys
print sys.path
import stripe
可能您必须导出PYTHONPATH=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
在执行python之前。/manage runserver
/图书馆/。。。听起来这不是一个标准路径…您是否可以在另一个python版本下运行django,在该版本中您通过pip安装了“stripe”?您能否将结果提供给:
哪个python
?另外,在视图.py
中,是否导入顶部的条带
?最后,如何启动开发服务器?哪种python返回/opt/local/bin/python
。如果在视图顶部包含“导入条带”,则会收到一个错误:importErrorat/complete/
--没有名为stripe的模块
,这就是您的问题所在。您使用的是Mac端口Python,但是当您使用<代码> SUDO < /Cord>时,它使用您的系统Python,您可能需要考虑使用Django条纹支付()。'/Labor……将是OSX Python Hyb埃里克的系统位置,我将其添加到我的问题(上看),因为我不能将所有内容包含在该评论中。我刚刚尝试在没有sudo
的情况下安装Stripe,我将结果包含在上面的问题中。我正在研究你答案的下一部分。@TonyDiepenbrock:你可能还有pip
指向另一个python interp。基本上,你只是有一个混合路径和蟒蛇。virtualenv可能会帮你解决这个问题。