attempted relative import with no known parent package on Google AppEngine with Python3.7 - python-3.x

Getting the following error:
"/srv/server.py", line 12, in from .routes.solver import route as solve ImportError: attempted relative import with no known parent package
Deploying the app to AppEngine Standard env, and my project looks like so:
---/
|_app.yaml
|_server.py
|_routes
|_solver.py
In server I do from .routes.solver import route as solve and get the above error in GCP, but not locally.
I tried https://stackoverflow.com/a/16985066/483616 and a few others. Tried with __init__.py at pretty much every level and every location. Then saw that it wasn't needed for python3, so removed. Pretty much unsure what to do now.

Not optimistic that this is the answer but just to throw it into the pot, have you seen Problem with Python relative paths when deploying to Google App Engine Flexible ?

Related

import module issue in a Django project: error ModuleNotFoundError

OS: Ubuntu
Django 4.1.6
I run into a very unexpected issue. I have created a Django project named data base. In this project I have created several apps (administrative, api, real_estate, telegram_app ...) with python manage.py startapp <app_name> inside the root folder.
Project structure with apps
I have added each app on the settings INSTALLED_APP.
settings.py file
Each of my apps apart from api and telegram_app have models. I have created in each app the corresponding admin.py.
After creating a New sqlite data base and the superuser i Can go to my admin site and manage each model of each app.
admin site with all modules models
So far so good then. My issue Come later. Now I want my telegram app and my api app to access models from other apps. So nothing wierd or fancy here.
But when I import model to use it I run into the error no module with that name.
error No Module name
Andy help?
I tried to change the import from database.api to api with the same result:
error 2
and also from ..api:models import but with the same result

Vite and dealing with require vs import (for google api)

First time using Vite with google apis and right at the start ran into trouble trying to import the translation client. It uses 'require' to import it const {TranslationServiceClient} = require('#google-cloud/translate') instead of 'import' syntax. I've tried workarounds including the vite require plugin but nothing has worked (reworking it into an import statement causes different errors with 'process not defined' which just stumped me even more) and it seems strange that no one else seems to mention any problem with vite and google apis. Has anyone run into this issue?
Did you try?
import TranslationServiceClient from "#google-cloud/translate";

AWS Lambda returns Unable to import module 'main': No module named 'main' when modules are there

So I'm trying to set up a function in AWS Lambda to run some python code I imported from a zip.
I've edited the handler to run the file then the function I want to run
I've tried having the file in the directory created when I imported the zip folder, after which I I moved it to the main function directory. Neither worked
Not too sure what is wrong here,
the full error returned when I run test is:
Response
{
"errorMessage": "Unable to import module 'main': No module named 'main'",
"errorType": "Runtime.ImportModuleError",
"stackTrace": []
}
Edit: really new to Lambda so please excuse any silly mistakes
The problem is that, while you appear to have a module named main, it has not been deployed to the Lambda service yet. When you click Test, Lambda runs the deployed code. Perhaps your module was renamed to main some time after your initial deployment?
Local changes to code need to be saved and then deployed. The deploy step is important because until you deploy the code, the Lambda service will continue to run the previous code.
This has actually been a common problem historically in the Lambda console, but enhancements have been made to make it more obvious that a deployment is needed. For example the console now indicates "Changes not deployed" after you make a change, until you hit the Deploy button.
I found this question while facing the problem myself. Issue was that the zip put "main.py" in a subfolder.
Hope this is helpful for any others!

tensorflow_hub not working on google app engine

The following python code is throwing an error on Google App Engine:
import tensorflow_hub as hub
embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder/4")
Error:
RuntimeError: Missing implementation that supports: loader(*('/tmp/tfhub_modules/063d866c06683311b44b4992fd46003be952409c',), **{})
My app.yaml is Standard environment and is using
instance_class: F4_1G
This works on my local box
Seems like similar issue to the one posted here. Would you be able to confirm if this mitigation helped in your case?
The error message 'RuntimeError: Missing implementation ...' comes up when using a directory that exists but does not contain a tfhub_module.pb file with a known module format. In particular, you can get this message by trying to load a hub.Module from a directory that contains something else.

Google Cloud Console - Flask - main.py vs package

OK, so I have been through some tutorials to get a flask app onto google cloud, which is fine.
I have also been through the flask tutorial to build a flaskr blog:
http://flask.pocoo.org/docs/1.0/tutorial/
It occurred to me that a sensible thing to do would be to create a database (MySQL in mycase) on google and then modify the code so that it uses that. This is fine and I can get it to work on my local machine.
However, now that I am coming to deploying this, I have hit a problem.
The google cloud tutorials tend to use a flask app that is initiated in a single file such as main.py, eg:
from flask import Flask, render_template
app = Flask(__name__)
....
The flask tutorial mentioned above uses a package and puts the code to create_app() in the __init__.py file and at present I cannot get this to start in the same way. (see sample code).
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev'
)
Are there some adjustments that I need to make to something like the app.yaml file to get it to recognise flask as the flaskr package or do I need to rewrite the whole thing so that it uses a main.py file ?
I feel that this is one of the points in time where I could really pick up a bad habit. What in general is the preferred way to write flask apps on google cloud ?
I am using the standard environment in google.
Thanks for your advice.
Mark
Since you have an application factory, you can create the app anywhere. Just create it in main.py, since this is what App Engine expects:
from my_package import create_app
app = create_app()

Resources