使用 Google App Engine 和 Django 将第三方库 (twilio) 添加到项目
Adding a third-party library (twilio) to project using Google App Engine and Django
大家。
我是这个领域的新手。我使用 google 应用程序引擎使用 django 框架开发 Web 应用程序。我有一个关于 python lib 目录问题的问题... ImportError: no module named...
我的 appengine_config.py 文件是
# [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' 文件是
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
所以,我在我的项目中安装...但是..编译错误。
from wanttousing_lib import example_module
importError wanttousing_lib........
然而,如果我将我的 wanttousing_lib 移动到 ROOT 目录,它会起作用......
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?
喜欢
libraries:
- name: MySQLdb
version: 1.2.5
- name: twilio <-- like this
version: -
要求-vendor.txt文件是
Django==1.11.3
我该如何解决?请帮忙...
前一段时间我遇到过类似的问题,我没有使用 vendor.add('lib')
,而是成功地这样做了:
vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))
我在第 4 步中找到了在 GAE 上使用第三方库的解决方案 here in the docs:
To copy a library into your project:
Create a directory to store your third-party libraries, such as lib/.
mkdir lib
Use pip (version 6 or later) with the -t flag to copy the libraries into the folder you created in the previous step. For example:
pip install -t lib/ <library_name>
Create a file named appengine_config.py
in the same folder as your app.yaml
file.
Edit the appengine_config.py
file and provide your library directory to the vendor.add()
method.
# appengine_config.py
from google.appengine.ext import vendor
# Add any libraries install in the "lib" folder.
vendor.add('lib')
The appengine_config.py
file above assumes that the current working directory is where the lib folder is located. In some cases, such as unit tests, the current working directory can be different. To avoid errors, you can explicity pass in the full path to the lib folder using:
vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))
我的 python 库的目录是两个目录。
1) /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/...
2) /usr/local/lib/python2.7/...
我的项目指向 1),但 pip install 指向 2)...
我尝试了 1) ./pip install twilio
。所以,它有效!
谢谢。
大家。
我是这个领域的新手。我使用 google 应用程序引擎使用 django 框架开发 Web 应用程序。我有一个关于 python lib 目录问题的问题... ImportError: no module named...
我的 appengine_config.py 文件是
# [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' 文件是
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
所以,我在我的项目中安装...但是..编译错误。
from wanttousing_lib import example_module
importError wanttousing_lib........
然而,如果我将我的 wanttousing_lib 移动到 ROOT 目录,它会起作用......
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? 喜欢
libraries:
- name: MySQLdb
version: 1.2.5
- name: twilio <-- like this
version: -
要求-vendor.txt文件是
Django==1.11.3
我该如何解决?请帮忙...
前一段时间我遇到过类似的问题,我没有使用 vendor.add('lib')
,而是成功地这样做了:
vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))
我在第 4 步中找到了在 GAE 上使用第三方库的解决方案 here in the docs:
To copy a library into your project:
Create a directory to store your third-party libraries, such as lib/.
mkdir lib
Use pip (version 6 or later) with the -t flag to copy the libraries into the folder you created in the previous step. For example:
pip install -t lib/ <library_name>
Create a file named
appengine_config.py
in the same folder as yourapp.yaml
file.Edit the
appengine_config.py
file and provide your library directory to thevendor.add()
method.# appengine_config.py from google.appengine.ext import vendor # Add any libraries install in the "lib" folder. vendor.add('lib')
The
appengine_config.py
file above assumes that the current working directory is where the lib folder is located. In some cases, such as unit tests, the current working directory can be different. To avoid errors, you can explicity pass in the full path to the lib folder using:vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))
我的 python 库的目录是两个目录。
1) /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/...
2) /usr/local/lib/python2.7/...
我的项目指向 1),但 pip install 指向 2)...
我尝试了 1) ./pip install twilio
。所以,它有效!
谢谢。