bulkloader not importing ndb.model - google-app-engine

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.

Related

Unit test fails on Jenkins, not on local (React project)

My unit tests for my React project pass perfectly fine on local but fail on Jenkins.
I get this error message on Jenkins:
Error: Cannot find module '../../../../src/components/Sidebar/MenuList.jsx'
The file does exist and is imported in my test file like so:
import { MenuList } from '../../../../src/components/Sidebar/MenuList.jsx';
And exported in my MenuList.jsx file:
export class MenuList extends Component { ... }
I am using Mocha/Enzyme/Sinon for testing + Istanbul for coverage.
Since you have a mac and im guessing jenkins is a linux based.
Check the file names. meaning check that the names are case sensitive.
Your problem is that the mac is not case sensitive while the jenkins (im guessing) is in a linux host which is. so say you have a file name like this
import a from '../MyCoolDir'
and the dir name is '../myCoolDir' it will work on mac but not on your jenkins. since the linux host will not be able to find the file becase it does not have the exact name
Youll probably want to take a look at this to configure git too
How do I commit case-sensitive only filename changes in Git?
Hope it helps

Google Go AppEngine imports and conflicts when serving / testing

So I have spent the better part of two days trying to figure this one out and no matter what I do I can't get things straightened out. Here is what is going on:
Using Go and Appengine. I am running into issues when trying to
get proper unit tests working.
I have tried lots of structures but here is a sample of where I am now: https://github.com/markhayden/SampleIssue
I am running into dependency issues in either goapp serve or goapp test -v ./src/lib1 depending on how I have my import paths set.
If I use "src/lib1" for my import path and then goapp serve. My app boots and runs fine, but when I run tests I get the following failure:
src/lib1/lib1.go:5:2: cannot find package "src/lib2" in any of:
/Users/USERNAME/go_appengine/goroot/src/pkg/src/lib2 (from $GOROOT)
/Users/markhayden/Projects/go/src/src/lib2 (from $GOPATH)
Likewise, if I use "dummy/src/lib1" as my path, my tests are happy and run fine but upon goapp serve ing the app I now get:
2014/11/06 20:33:34 go-app-builder: Failed parsing input: app file lib1.go conflicts with same file imported from GOPATH
Have fiddled with all sorts of different options and can't figure out how to handle dependencies and still have solid testing. Maybe its a appengine / golang bug? Or am I missing something?
Any help would be very much appreciated. Thanks in advance!
Updated everything based on first comment feedback. I can run tests (as I was able to do before) but I still can not serve the app. Here is what I get when running goapp serve
INFO 2014-11-07 17:24:48,727 devappserver2.py:745] Skipping SDK update check.
INFO 2014-11-07 17:24:48,748 api_server.py:172] Starting API server at: http://localhost:60732
INFO 2014-11-07 17:24:48,751 dispatcher.py:185] Starting module "default" running at: http://localhost:8080
INFO 2014-11-07 17:24:48,754 admin_server.py:118] Starting admin server at: http://localhost:8000
ERROR 2014-11-07 17:24:49,041 go_runtime.py:171] Failed to build Go application: (Executed command: /Users/markhayden/go_appengine/goroot/bin/go-app-builder -app_base /Users/markhayden/Projects/go/src/github.com/markhayden/SampleIssue -arch 6 -dynamic -goroot /Users/markhayden/go_appengine/goroot -nobuild_files ^^$ -unsafe -gopath /Users/markhayden/Projects/go -print_extras_hash lib1/lib1.go lib2/lib2_test.go main_test.go main.go lib1/lib1_test.go lib2/lib2.go)
2014/11/07 09:24:49 go-app-builder: Failed parsing input: app file lib2.go conflicts with same file imported from GOPATH
$GOPATH = /Users/markhayden/Projects/go
$GOROOT = not set (according to docs it doesnt need to be if you dont use a custom directory)
App Structure:
$GOPATH/src/github.com/markhayden/SampleIssue/
- app.yaml
- /lib1
- lib1_test.go
- lib1.go
- /lib2
- lib2_test.go
- lib2.go
- main_test.go
- main.go
In main.go:
import (
"fmt"
"github.com/markhayden/SampleIssue/lib1"
"net/http"
)
In lib1/lib1.go:
import (
"fmt"
"github.com/markhayden/SampleIssue/lib2"
)
Appengine "conflicts with same file imported from GOPATH" issue:
Appengine is importing things underneath the root directory (i.e. where the app.yaml is). This will cause two imports, one by appengine when it scans the directories, and a second by your source when it is explicitly imported.
You have two choices:
Don't use the full import path (for sub-folder packages) with appengine.
Remove the source repository part of import. So instead of
"github.com/blah/blah" it would be "blah/blah".
Note: This kinda sucks as it makes your build and software appengine specific. You could make this a little better -maybe- by using build constraints. e.g. +build !appengine or
+build !appengine to include/remove certain files from the build depending on if you are targeting appengine.
Move your modules/dependencies (sub-folders) to a separate and independent project to make it work with the full path import convention:
Get rid of all directories / dependencies in the main project (where
your app.yaml is), so that appengine can't scan and find them.
Move them to another independent project (I did SampleIssueDeps)
with no app.yaml that is not a sub-directory (e.g.
/MarkHayden/SampleIssueDeps).
Then pull those dependencies via
full path import. e.g. github.com/MarkHayden/SampleIssueDeps/lib1.
Summary: For sub-folder packages in an appengine project don't include the "source repository" part of the import path OR only use appengine to init() and move all of your other code to separate projects and use like external dependencies.
I came up with another option that isn't discussed here and in my opinion is much easier to deal with (and keep your app less appengine specific). Lets say you have the repo at github.com/blah/blah and right now the root folder of the repo defines your app engine server.
First, move the app.yaml and other app engine specific files (NOT .go files) into github.com/blah/blah/appengine/app.yaml.
Next, wherever you run your init function for app engine, rename it to something like func Run() { ... }, and then in github.com/blah/blah/whatever.go write something like this:
package appengine
import "github.com/blah/blah"
func init() {
blah.Run()
}
From my experience this has resolved the issue and made things much easier. I'll update this if I run into any major issues that make this a bad solution.
I had a lot of trouble following various answers and understanding how to solve the problem.
But after a lot of research, I believe I understand both cause and solution:
Google app-builder tooling does some path-munging and is causing this.
They are aware of the bug but no ETA to fix it.
Problem summary:
any .go files inside or below the directory holding main.go / app.yaml will be double imported…
In summary, just make sure that ALL of our files/packages are siblings and not decedents of the directory holding those two files...

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.

ImportError: No module named pydevd_vm_type

I'm using PyCharm 2.6.3 and Python 2.7.5 for my Google Application Engine project.
Today occurred strange error when I was trying to debug my GAE project:
ImportError: No module named pydevd_vm_type
Module exists but doesn't want to be imported somewhere in debug tools.
When I trying to type "import pydevd_vm_type", autocomplete tool gives me right path to that module. How can I solve this issue?
Problem solved!
File Path_on_disk\PyCharm 2.6.3\helpers\pydev\pydevd.py
Error occurs in class
class PyDBCommandThread(PyDBDaemonThread)
in method
def OnRun(self)
...
import pydevd_vm_type
if pydevd_vm_type.GetVmType() == pydevd_vm_type.PydevdVmType.JYTHON and sys.hexversion <= 0x020201f0:
# a lot of comments about Jython bug
run_traced = False
and next in
import pydevd_tracing
if run_traced:
pydevd_tracing.SetTrace(None) # no debugging on this thread
I've deleted those two imports. PROFIT!!!

Bulk Upload with Google App Engine Datastore via CSV

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()

Resources