python - google app engine - how to add lib folder? -
i keep getting importerror: no module named twitter when it's in lib module
details:
- the twitter module located in lib directory.
- the lib directory has empty init.py
- i'm getting error on page outside directory
- when take twitter module out of lib directory , import twitter, works
- when put twitter module inside lib , lib import twitter, or when import twitter, or saying import lib.twitter says, go **** , it
- i tried doing this: https://cloud.google.com/appengine/docs/python/tools/libraries27?hl=en , suggests to:
to tell app how find libraries in directory, create (or modify) file named appengine_config.py in root of project, add these lines:
from google.appengine.ext import vendor # add libraries installed in "lib" folder. vendor.add('lib')
and tried import twitter, , tried lib import twitter, , says, "haha, nice try."
now what?
tl;dr - comments right. 'lib' not module, it's folder containing modules. needs on python import path before import twitter
, using vendor.add('lib')
, should set import path correctly when deploy or use dev_appserver.py. if place vendor.add(lib)
snippet in appengine_config.py
file , run dev_appserver.py or deploy, , twitter module inside lib
, should able import twitter
. if you're running outside of app engine/devappserver, you'll need make sure lib
on pythonpath.
here longer explanation of lib
folder is, vendoring is, , how fit bigger picture of managing dependencies on app engine.
with app engine, can specify dependencies provided sandbox in app.yaml. can se list of available libraries here. other dependencies (which can pure python libraries), libraries need directly included inside project. directly including code these dependencies part of project (rather listing them in requirements.txt , expecting them installed @ deploy time) commonly called 'vendoring'.
now, though including dependencies directly, we'd rather use pip , requirements.txt, few reasons mention below. pip used install libraries system libraries or virtualenv, not project itself. feature added pip , feature added app engine fix this.
the pip feature install dependencies folder rather system libraries or virtualenv. use -t flag this:
pip install -r requirements.txt -t lib
where 'lib' folder install into. way, still specify dependencies in requirements.txt, , use pip install them, installed right directory specified. comments noted, lib folder not module - contains them. lib folder should not have init.py, should contain folder 'twitter' have init.py. since lib
not module, from lib import twitter
doesn't make sense. note lib
arbitrary name, 1 pick convention.
there few big advantages vendoring using pip rather manually downloading dependencies , adding them project. 1 advantage don't need check dependencies source control - add requirements.txt, , tell other users use pip -t
command vendor dependencies too. advantage it's more organized cleanly separate code third-party modules. point of vendoring features keep these advantages while still following app engine requirements include dependencies in directory @ deploy time.
the app engine vendor extension using made recognize folder contains modules have been 'vendored' , add path. vendor extension using in snippet. makes sure lib
folder on import path can import modules installed it.
once run vendor.add command, should able import modules in lib folder. comment notes, need make sure runs before import twitter
. practice make sure vendor commands run before else. can accomplish putting code inside file named appengine_config.py in directory. that's special file runs before else. take @ example of doing here.
a few last notes might helpful:
appengine_config.py
run in gae environment or when run dev_appserver, emulates gae environment. if you're ever running outside of gae environment, make sure pythonpath includeslib
folder want import from.- since can vendor libraries using pip, might ask why ever include dependency using app.yaml? answer since pure python libraries can vendored, still should use app.yaml dependencies libraries need c libraries, such mysql.
- if have vendored libraries , app.yaml libaries, don't want vendor libraries in gae sandbox since versions might conflict. in case, it's idea have separate requirements.txt dependencies want vendor, , dependencies want included when running locally gae provide in sandbox. here example of mysql.
Comments
Post a Comment