我似乎无法弄清楚如何将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
然后import 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
这是我收到的:
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...
答案 0 :(得分:3)
这似乎是在与pip使用的python解释器不同的python解释器下运行django dev服务器的情况。 “site-packages”位置应自动位于正确解释器的路径中。
您不需要在INSTALLED_APPS中添加“stripe”来解决无法导入的问题。您可以添加它以将其作为应用程序包含在内,并考虑所有模块,例如models.py。
根据您的评论,您使用macports安装的python作为默认值,但是当您使用sudo
命令安装条带时,它使用您的系统python。首先,你应该尝试在不使用sudo的情况下进行pip安装。或者确保{PYTHONPATH中的/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
始终包含系统包和macport。
或者,您可以尝试使用带有所需python的显式路径的sudo,因此环境不会为您猜测:sudo /opt/local/bin/pip install ...
<强> VIRTUALENV 强>
您遇到的问题使此问题成为使用virtualenv的理想选择。使用virtualenv获得的是一个完全孤立的环境,始终使用相同的python interpeter,并拥有本地化的站点包。安装stripe会将它放在本地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还允许您明确告诉它使用哪个python而不是默认值:
virtualenv --no-site-packages -p /opt/local/bin/python myproject
答案 1 :(得分:0)
请在导入条带之前查看python路径:
import sys
print sys.path
import stripe
可能你必须导出PYTHONPATH = / Library / Frameworks / Python.framework / Versions / 2.7 / lib / python2.7 / site-packages 就在做python之前./manage runserver
/ Library / ...听起来不是标准路径...