使用Google App Engine和Django将第三方库(twilio)添加到项目中

使用Google App Engine和Django将第三方库(twilio)添加到项目中

问题描述:

所有人.

我是这个领域的新手.我使用django框架使用Google App Engine开发Web应用程序.我对python lib dir问题有麻烦... ImportError:没有名为...的模块.

I'm a newbie in this field. I develops web application with google app engine using django framework. I have a troubleshot about python lib dir problem... ImportError: no module named...

我的appengine_config.py文件是

my appengine_config.py file is

# [START vendor]
from google.appengine.ext import vendor 

vendor.add('lib') # I believes this line is to add 'lib' folder to PATH.

# vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib')) # <-- and I tried too this line.

# [END vendor]

我的'requirements.txt'文件是

my 'requirements.txt' file is

MySQL-python==1.2.5 #app engine django project default
Django==1.11.3 #app engine django project default
django-twilio # add i want
twilio # add i want

,并且我使用pip install -t lib -r requirements.txt

ROOT
├── lib
│   ├── django
│   ├── pytz
│   ├── wanttousing_lib
│   └── ...
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── controllers.py
│   ├── models.py
│   ├── views.py
│   ├── templates
│   └── ....
├── test
│   ├── like
│   │   ├── models_tests.py
│   │   └── controllers_tests.py
│   └── ....
├── static
│   ├── css
│   └── js
├── app.yaml
├── manage.py
├── appengine_config.py
├── requirement-vendor.txt
└── requirements.txt

所以,我安装了我的项目...但是..编译错误.

so, I installed in my project... but..compiled error.

from wanttousing_lib import example_module importError wanttousing_lib ..........

from wanttousing_lib import example_module importError wanttousing_lib..........

但是,如果我将我的wanttousing_lib移至ROOT dir,它就可以工作.....

however, if I move my wanttousing_lib to ROOT dir, it works.....

ROOT
├── lib
│   ├── django
│   ├── pytz
│   
│   └── ...
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── controllers.py
│   ├── models.py
│   ├── views.py
│   ├── templates
│   │   └── like
│   │        ├── index.html
│   │        └── _likehelpers.html
│   └── ....
├── test
│   ├── like
│   │   ├── models_tests.py
│   │   └── controllers_tests.py
│   └── ....
├── static
│   ├── css
│   └── js
├── app.yaml
├── manage.py
├── appengine_config.py
├── requirement-vendor.txt
├── requirements.txt
└── wanttousing_lib  <--- moved

->所有回溯.

Unhandled exception in thread started by <function wrapper at 0x103e0eaa0>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    self.check(display_num_errors=True)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "ROOT/mysite/urls.py", line 19, in <module>
    from polls.views import index
  File "ROOT/polls/views.py", line 17, in <module>
    from sms_twilio.tests import send_sms_test
  File "ROOT/sms_twilio/tests.py", line 13, in <module>
    from twilio import twiml
ImportError: No module named twilio

错误源:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.test import TestCase

# Create your tests here.
from django.conf import settings

# file: your_code.py
# import twilio  # no need for 'from lib import twilio'
# do stuff with twilio...

from twilio import twiml
from twilio.rest import Client


def send_twilio_message(to_number, body):
    client = Client(
    #client = twilio.rest.TwilioRestClient(
        settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)

    return client.messages.create(
        body=body,
        to=to_number,
        from_=settings.TWILIO_PHONE_NUMBER
    )

def send_sms_test():
    client = Client(
    #client = twilio.rest.TwilioRestClient(
        settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)

    return client.messages.create(
        body="[TEST] SEND SMS !! HELLO !!",
        to="TO_SENDER",
        from_=settings.TWILIO_PHONE_NUMBER
    )

也许,我是否将库列表添加到app.yaml中? 喜欢

perhaps, Do I add library list to app.yaml ? like

libraries:
- name: MySQLdb
  version: 1.2.5
- name: twilio <-- like this
  version: -

requirement-vendor.txt文件为

requirement-vendor.txt file is

Django==1.11.3

我该如何解决?请帮忙...

how can i fix it? please help...

我的python lib的目录是两个目录.

My python lib's dir are two dir.

1)/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/...

1) /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/...

2)/usr/local/lib/python2.7/...

2) /usr/local/lib/python2.7/...

我的项目指向1),但是pip安装指向2)...

My project points at 1), but pip install at 2)...

我尝试过1)./pip install twilio.这样就可以了!

I tried at 1) ./pip install twilio. and so, it works!

谢谢.