3

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

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

Community
  • 1
  • 1
Yarin
  • 173,523
  • 149
  • 402
  • 512

1 Answers1

4

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.

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.

shakefu
  • 93
  • 4
  • Shakefu- " best practice for 3rd party modules is to install them via pip or easy_install" - Can you explain why this is preferred? – Yarin May 25 '12 at 20:50
  • Because it actually installs the package via its `python setup.py install` to your `PYTHONPATH`, and runs the install scripts hooks necessary to install executable scripts, build C extensions, etc., that isn't done by just copying in a module. Edit: Also, using a virtualenv is nice because it gives you a controlled package/dependency environment for each project, rather than installing every package system-wide (which may require root privileges). There are some instances where you can't use a virtualenv, but not many. – shakefu May 25 '12 at 20:53
  • Because if you copy/paste the directory, you might end up with some problems, especially because "sengrid" will no longer be in the `PYTHONPATH`. You are tampering with the `PYTHONPATH`. – jadkik94 May 25 '12 at 20:53
  • Made this its own [question](http://stackoverflow.com/q/10761521/165673) - i need to understand this.. – Yarin May 25 '12 at 21:23