5

我似乎不知道如何将 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.6or python2.7then 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...

4

2 回答 2

4

这似乎是在与 pip 使用的 Python 解释器不同的 Python 解释器下运行 django 开发服务器的情况。“站点包”位置应自动位于正确解释器的路径中。

您不需要在 INSTALLED_APPS 中添加“条纹”来解决无法导入的事实。您可以添加它以将其作为应用程序包含在内,并考虑其所有模块,例如它的 models.py。

根据您的评论,您默认使用 python 的 macports 安装,但是当您使用sudo命令安装条带时,它使用您的系统 python。首先,您应该尝试在不使用 sudo 的情况下进行 pip install。或者确保/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages始终在您的 PYTHONPATH 中以包含系统包和您的 macport。

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

虚拟环境

您遇到的问题使这个问题成为使用virtualenv的完美候选者。使用 virtualenv 获得的是一个完全隔离的环境,始终使用相同的 python 解释器,并拥有本地化的站点包。安装条带会将其放置在该本地库中。

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
于 2012-06-19T21:00:21.413 回答
0

请在导入 stripe 之前查看 python 路径:

import sys
print sys.path
import stripe

可能您必须在执行 python ./manage runserver 之前导出 PYTHONPATH=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

/Library/... 听起来不是标准路径...

于 2012-06-19T21:04:00.430 回答