GAE - Including external python modules without adding them to the repository?

心已入冬 提交于 2019-12-19 10:13:50

问题


I'm current working on a python based Google App Engine project. Specifically, I'm using Flask for the application. I'm wondering what the accepted method of including external python modules is, specifically when it comes to the repository. From what I can tell, including other people's code in my repository is bad form for several reasons. However, other people will be working on the same repository, so we should be using the same external modules to insure the same results.

Specifically, I need to include Flask (and its dependencies) to my application. The easiest way to do this with Google App Engine is just to throw them into the root level:

MyProject
    app.yaml
    main.py
    MyApp
    Flask
    ...

What is the proper way to bring in these external modules in such a project? Both a generalized answer and one specific to my case would be useful. Also, any other related recommendations would be appreciated. Thank you much.


回答1:


While it is indeed possible to include third party libraries as submodules or symlinks from external repositories, in practice it's not a good idea. Here are two scenarios on what could go wrong:

  1. If the third party library releases a new version that breaks the functionality, you will have to either make all the necessary changes to meet the new requirements or simply find the previous version to keep working and break the external connection. Usually this happens when you are very close to deadlines.

  2. If the third party library releases a new version and one of your colleagues is upgraded and made all the necessary changes to support the new version, on your side the code will be broken until you will upgrade as well.

The above examples are much more visible in big projects with lots of dependencies and as more people joining the project in the long run it becomes a huge problem! I could come up with more examples, but I think you can see the point.

Your best option is to include the external libraries into your repository, which also has the advantage that you are able to have the whole project up and running on a new machine without many dependencies. There are many ways on how to organize your third party libraries and all of them needs to be included on the same or deeper level with your app.yaml file. Just as @dragonx mentioned include only the core library code.

Also do not afraid putting stuff into your repository cause space is not an issue today and these libraries usually not updating that often so your repository size is not getting too much bigger over time.

Since you mentioned Flask on Google App Engine, you can check out my gae-init project, where you can see in practice how the external libraries are organised.




回答2:


You're actually asking two questions here.

  1. How do I include the external library in my GAE project?

You've got the right idea. Whatever way you go about it, you must somehow include Flask and its dependencies in the root of your GAE project. One way is to put a copy directly in there.

The second way is to use a symbolic link to the folder that contains the external library. I'm not sure about Flask, but often times external repos contain the actual library code in a subdirectory - so often you don't want the root of the repo in your GAE app, just the root of the actual source. In this case, it's easier to put a symlink that links to the source folder.

  1. How do I manage external libraries in my source repo?

This is a harder question to answer since it depends what source control tool you're using. Yes, you do want to have everyone use the same versions of external libraries, and they should be included in your source control somehow.

If you're using git, git submodule is the way to go. It's a bit confusing to start with but it'll get the job done.

I'd recommend a repo structure that looks something like this

repo/
    thirdparty/
        flask/
        other_dependency/
        another_dependency/
            README.TXT
            setup.py
            src/
    app/
        app.yaml
        your_source.py
        softlink_to_flask
        softlink_to_other_dependency
        softlink_to_another_dependency_src

In this example you keep the source to your external libraries in the thirdparty folder. These may be git submodules. In the app folder you have your source, and softlinks to the appropriate files that are actually needed for your app to run. In this case, the actual code for another_dependency may be in the another_dependency/src folder rather than the actual root of another dependency. This way you don't need to include the unnecessary files in your deployment folder, but you can still keep the entire library in your repo.




回答3:


You can't just create requirements.txt and put it to GAE. Your code must include all pure python libraries that used your project and doesn't supported by GAE (https://developers.google.com/appengine/docs/python/tools/libraries27).

If you look at flask deploy example for GAE (http://flask.pocoo.org/docs/quickstart/#deploying-to-a-web-server and https://github.com/kamalgill/flask-appengine-template) you can find some dependencies like flask, werkzeug and etc. and all this dependencies you must push to GAE server.

So I see three solutions:

  1. Use local requirements for local development and make custom build function that will download all dependencies, put with your application and upload to GAE server.

  2. Add tools for local deployment when you just start project that put required libraries with your application (don't forget about .gitignore).

  3. Use something like git submodules to requirements repositories.




回答4:


There is two case for using python third party packages in google app engine project:

If your library is one of the supported runtime-provided third-party libraries of GAE section

just add it to your app.yml file under libraries

  libraries:
        - name: package_name
          version: latest

Add your code

import pack_name

Sometimes you need to install the package with

pip install package_name

Make sure you're using the right interpreter, by using

pip freeze

you can make sure the package is installed successfully to the right path.

Otherwise, if GAE does not support you library, you need to download it manually and save it locally under root/Lib directory: or through GIT or through pip (pip install package_name -t path/to/your/Lib/dir)

After that, we should declare Lib directory as source dir in pycharm pycharm->preferences->Project Structure Choose Lib directory and mark it as source.
Then, import it.

import pack_name

Pay attention that when you're doing the import, you choosing the local path and not your python path.

In general, that's recommended to have requirements.txt file, that includes all the used packages names, and then the pycharm will recognize the uninstalled packages and suggest you to install them.

Good Luck



来源:https://stackoverflow.com/questions/15651891/gae-including-external-python-modules-without-adding-them-to-the-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!