Domanda

Anyone know the easiest ways to import these libraries without modifying the libraries' import path?

Here is the folder structure

src/
    main.py
    app.yaml
    index.yaml
    lib/
        flask/
        werkzeug/
        simplejson/
        __init__.py

For example, in the main.py I added from lib import flask and got

from werkzeug.exceptions import abort
ImportError: No module named werkzeug.exceptions

what is the proper way to use import flask in the main.py and for other libraries as well.

Thanks in advance.

enter image description here

main.py:

import os, sys

#libdir = os.path.abspath("lib")
#sys.path.append(libdir)
import webapp2
from flask import Flask
#app = Flask(__name__)

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello world!')

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

app.yaml:

- url: /lib
  static_dir: lib
È stato utile?

Soluzione

You really will need to add lib to your path, either using site.addsitedir or manipulating sys.path. However don't do it in main.py but put all path and other global additions in appengine_config.py which is loaded before any of your code is run.

See https://developers.google.com/appengine/docs/python/tools/appengineconfig

Altri suggerimenti

Try doing this in main.py (it might work in a similar way inside lib/init.py, but I havn't tested that.

libdir = os.path.abspath("lib")
sys.path.append(libdir)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top