Bulk Upload with Google App Engine Datastore via CSV - google-app-engine

I am building a GAE webapp using Python. I am also using the Datastore and trying to bulk upload data to the DB using the terminal and a CSV file as per:
https://developers.google.com/appengine/docs/python/tools/uploadingdata
I have created a Loader class in a separate.py file in my app root directory. I am not really sure if this loader class should be in my main.py webapp file, or another file in the root directory.
Loader class:
import datetime
from google.appengine.ext import db
from google.appengine.tools import bulkloader
import models
class FTSELoader(bulkloader.Loader):
def __init__(self):
bulkloader.Loader.__init__(self, 'FTSE',
[('date', lambda x: datetime.datetime.strptime(x, '%Y/%m/%d')),
('close', float)])
loaders = [FTSELoader]
My kind class (i.e. my Datastore table) I am trying to create/upload is called "FTSE". I then run this command in Terminal:
appcfg.py upload_data --config_file=FTSEdataloader.py --filename=FTSEdata.csv -- kind=FTSE --url=http://<myapp.appspot.com>/_ah/remote_api
I get the following error:
File "FTSEdataloader.py", line 4, in
import models
ImportError: No module named models
I do not have a "models.py" like in the GAE demonstration. What should take its place?
Thanks

I had the same problem. I'm not sure why the appcfg.py can't find the models module when running the upload script. I got around the problem by doing this:
import datetime
from google.appengine.ext import db
from google.appengine.tools import bulkloader
class FTSE(db.Model):
date = DateTimeProperty()
close = FloatProperty()
class FTSELoader(bulkloader.Loader):
def __init__(self):
bulkloader.Loader.__init__(self, 'FTSE',
[('date', lambda x: datetime.datetime.strptime(x, '%Y/%m/%d')),
('close', float)])
loaders = [FTSELoader]
Basically it is just putting your model definition in the bulkloader. It certainly isn't the best way to do this, but it will work around the PYTHONPATH problem that appcfg.py seems to have when it is running the bulk upload.

You do this using a file of Python code. The file imports or defines the Model classes for the entities being created, defines a loader class for each kind you wish to import, and declares the available loader classes in a global variable.
For example, say you have a Model class named "FTSE" defined in a file named models.py (which is in your PYTHON PATH, such as the directory where you'll run the tool Ex: C:\Python27) that resembles the following:
models.py
from google.appengine.ext import db
class FTSE(db.Model):
date = db.DateProperty()
close = db.FloatProperty()

Related

Import another app's model

What is the equivalent in Wagtail for importing another apps model? In Django I simply do from app.models import SomeModel but I can't figure out how to get hold of another apps model in Wagtail since they all inherent from wagtail.wagtailcore.models import Page
If I wanted to import class HomePage(Page) from home app in blog app... I've tried things like:
from project_name.home.models import HomePage
But it return a ModuleNotFoundError: No module named 'project_name.home'

wagtailcore owner alter field migration being generated when doing app makemigrations

When running makemigrations for other apps in this particular project I sporadically get the following wagtailcore migration being created (e.g. in this case wagtail/wagtailcore/migrations/0033_auto_20170210_0710.py) and my app migrations setting it as a dependancy. I've tried to track down the reason several times but failed and resorted to just deleting it and updating the dependancy in my app migration. I'd be grateful if anyone can point out why/where/how/what I'm doing wrong/missing here.
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-10 07:10
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('wagtailcore', '0032_add_bulk_delete_page_permission'),
]
operations = [
migrations.AlterField(
model_name='page',
name='owner',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='owned_pages', to=settings.AUTH_USER_MODEL, verbose_name='owner'),
),
]
It wasn't a Wagtail issue. It was a Puput issue fixed in the version 0.9. Please try to update to that version.

importing a git-submodule into a golang gae app

I have a submodule in my golang google-app-engine project that I would like to add to my path.
$ ls ./openid/src/openid
discover.go integration verify.go
discover_test.go nonce_store.go xrds.go
discovery_cache.go nonce_store_test.go xrds_test.go
fake_getter_test.go normalizer.go yadis_discovery.go
getter.go normalizer_test.go yadis_discovery_test.go
html_discovery.go redirect.go
html_discovery_test.go redirect_test.go
In the example code for this package, it imports "openid". I'm new to golang's import rules, and I can't for the life of me figure out what I need to put in the import statement of my main file to import this package. I've tried "openid/src/openid", "myapp/openid/src/openid", etc. Can someone provide some clarification of how this works? Or do I need to actually modify the app.yaml file?
"Organizing Go code" mentions:
An import path is the string with which users import a package.
It specifies the directory (relative to $GOROOT/src/pkg or $GOPATH/src) in which the package's source code resides.
So make sure to use an import statement referring to a path which exists in your $GOPATH/src. (GOPATH being your "workspace", in which you have namespaces, as shown in this video).
Also make sure you build first openid/src/openid sources. And install it (go install).
As detailed in this answer:
Import paths can be be globally unique.
In conjunction with GOPATH, import path can be translated unambiguously to a directory path.
Any directory path under GOPATH can be unambiguously translated to an import path.

Google App Engine doesn't find local python module

For some reason when I uploaded my app engine project yesterday (before this, everything worked fine), it can't find one of my .py files/modules. My directory is as follows:
app_directory/
gaesessions/
__init__.py
lib/
httplib2/
__init__.py
other stuff
app.yaml
appengine_config.py
index.yaml
All other .py files/modules
For some reason I now get the following error:
import_string() failed for 'games.GetMyGames'. Possible reasons are:
- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;
Original exception:
ImportError: cannot import name GameModel
I realized I had a circular import:
in File1.py
from File2 import class1
and in File2.py
from File1 import class3
I changed to:
in File1.py
import File2
and in File2.py
import File1
and I moved all of my class imports from File1 and File2 further down the files and this solved my issue. Hopefully this helps someone else.

bulkloader not importing ndb.model

I am still new to Python and GAE. I have an application on local server that is running just fine. I can add entity to my datastore, I can view my website, etc: everything is fine.
Now I am trying to use bulkloader to add entities to my datastore. I followed the tutorial at https://developers.google.com/appengine/docs/python/tools/uploadingdata. My loader is below:
from google.appengine.ext import ndb
from google.appengine.tools import bulkloader
import my_model
class ArticleLoader(bulkloader.Loader):
def __init__(self):
bulkloader.Loader.__init__(self, 'Article',
[('title', str),
('author', str)
])
loaders = [ArticleLoader]
I am getting the error:
No module named my_model
Does anyone have a fix for this?
Note: I am only using one directory. So my loader is in the same location as the other file that imports the my_model module.
This can also happen if your PYTHONPATH is not properly set up. If you're on Linux, try running this before you run the Bulkloader:
export PYTHONPATH=$PYTHONPATH:.
This appends your current directory to your PYTHONPATH and should make your my_model module visible. Since my memory is terrible and I always forget to do it, I've ended up using a simple shell script that includes this at the beginning and then the bulkload command itself.
If you're on Windows, you should be able to modify your path by using sys.path.append. Haven't tested this, but you could try adding this to your script (note that this should work on Linux as well):
import sys
# ...
sys.path.append('.')
Your code should be located in a file named my_model.py. You are getting that error because there is no module named my_module. Might be worth a read of the Python module and package docs.

Resources