需要帮助理解模块导入错误 [英] Need help understanding module import errors
问题描述
我正在尝试将 sendgrid Python API 用作 web2py 中的模块.从命令行成功测试后,我将其放入我的模块文件夹中,但是一旦我尝试将 sendgrid 导入我的控制器文件,我得到:
I'm trying to use the sendgrid Python API as a module in web2py. After testing it successfully from the command line, I dropped it into my modules folder, but as soon as I try to import sendgrid into my controller file, I get:
File "applications/test/modules/sendgrid/__init__.py", line 4, in
<module>
del sendgrid, message NameError: name 'sendgrid' is not defined
查看 __init__.py
文件,我注意到他们在模块级别执行 * 导入,我已经看到了 之前出现过问题,但我不确定是什么问题.
Looking at the __init__.py
file, I noticed they're doing * imports on the module level, which I've seen cause problems before, but I'm not sure what the issue is.
sendgrid/__init__.py
:
from sendgrid import *
from message import *
del sendgrid, message
__version__ = "0.1.0"
version_info = (0, 1, 0)
sendgrid api:https://github.com/sendgrid/sendgrid-python
推荐答案
一般来说,3rd 方模块的最佳实践是通过 pip
或 easy_install
(最好是在 virtualenv 中),如果它们在 PyPI,而不是将它们复制到您的 PYTHONPATH
上.
Generally, the best practice for 3rd party modules is to install them via pip
or easy_install
(preferably in a virtualenv), if they're available on PyPI, rather than copying them somewhere onto your PYTHONPATH
.
尝试从您的模块文件夹中删除 sendgrid 包并执行 pip install sendgrid-python
或 easy_install sendgrid-python
如果 pip
不是可用的.
Try removing the sendgrid package from your modules folder and doing pip install sendgrid-python
or easy_install sendgrid-python
if pip
isn't available.
这篇关于需要帮助理解模块导入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!