I have two different projects in Google app engine account. The folders structure in my computer is as following:
- parent_folder
- common_folder
- project1_folder
- project2_folder
I have few python classes in common_folder that I want to use in both projects (project1 and project2).
I want to import the common classes into my projects, so Google App Engine will recognize them also on Production env (maybe GAE can create a copy of the common files when uploading?)
I tried using the following code in order to import my class from a specific parent directory path:
import os
PARENT_DIR = os.path.dirname(os.path.dirname(__file__))
print PARENT_DIR
def load_src(name, fpath):
import os, imp
return imp.load_source(name, os.path.join(os.path.dirname(__file__), fpath))
load_src("globals", PARENT_DIR +"/common/globals.py")
from globals import *
This code worked only from my localhost environment but when I uploaded it to GAE, it didn't recognize the class:
> File
> "/base/data/home/apps/s~loan-management-system/1.394267205500555512/main.py",
> line 39, in load_src
> return imp.load_source(name, os.path.join(os.path.dirname(__file__), fpath)) IOError: [Errno 2] No
> such file or directory
The reason for the error you see is that nothing above the module/service dir (or app dir for single module apps) is uploaded to GAE during deployment.
You can symlink the necessary file(s) inside your app dirs, see https://stackoverflow.com/a/34291789/4495081. The deployment utility knows to follow the symlink and include the common folder package in the deployment.
I would suggest making the common folder a package (add a
__init__.py
file) then vendor it in inside each application but instead of copying/installing the code just symlink the common code dir inside thelib
dir.Then you don't need to fiddle with the path manually, in your app you'd just use the classes like this (assuming for example that
MyClass
comes fromcommon_folder/class.py
):