Google App Engine - Production Only - Module Import Error - python

I am getting this error when running the app in production. It works fine in my local environment. But, after I upload to server, it throws this exception.
The test_handler module exists in test package (and it works fine in local). I believe that the entire folder structure will be uploaded to the server, when we upload.
Also, is there a way to see the entire folder structure in the Google Application Account online?
============================================================================
<type 'exceptions.ImportError'>: cannot import name test_handler
Traceback (most recent call last):
File "/base/data/home/apps/mad-scribe/1.346944987034829366/url_handler.py", line 15, in <module>
from test import test_handler

You can now download the source if you were the developer who uploaded it.
This should give you a concrete understanding of what was uploaded.
AppEngine Docs: Downloading Source Code

The issue was with the package name. I named the package as 'test', which might have stepped on the shoes of some other package.
from test import test_handler
Renaming the package name (test -> test_handlers) fixed the issue.
Thanks to all who responded to this question.

Related

Pycharm "no module named REPORT.py" however it exist and works. But then

Pycharm says REPORT.py file doesn't exist. However the code works perfectly.
Here is the image of the issue:
Now that is not the strangest part. I know sometimes when you working within a package you have to reference the package name: import package_name.filename so when I tried that it appears to have fixed the incorrect reporting.
But then...
Traceback (most recent call last):
File "C:/Users/REDACTED/PycharmProjects/REDACTED/MAIN/MAIN.py", line 2, in <module>
import MAIN.REPORTS as PDD
File "C:\Users\REDACTED\PycharmProjects\REDACTED\MAIN\MAIN.py", line 2, in <module>
import MAIN.REPORTS as PDD
ModuleNotFoundError: No module named 'MAIN.REPORTS'; 'MAIN' is not a package
This seams like a mistake with how Pycharm is checking for my file.
For the sake of completeness I also tried to import from.
Pycharm is also not marking this as invalid but I get a new interesting error when running the code:
Traceback (most recent call last):
File "C:/Users/REDACTED/PycharmProjects/REDACTED/MAIN/MAIN.py", line 2, in <module>
from MAIN import REPORTS as PDD
File "C:\Users\REDACTED\PycharmProjects\REDACTED\MAIN\MAIN.py", line 2, in <module>
from MAIN import REPORTS as PDD
ImportError: cannot import name 'REPORTS'
As request in the comments here is my folder structure:
Per suggestion in the comments I did try to import * but I still get the same reporting issue from Pycharm.
Update:
I believe I found out why the issue existed when trying to import from my package.
Due to my package name being MAIN and my main py file being MAIN.py I believe my code was trying to import from the py file and not the package.
After renaming my package to MAIN_PACK and doing import MAIN_PACK.REPORT as PDD the code works fine without any reporting from Pycharm saying it is not valid.
That at the vary least fixes the Pycharm reporting.
However that still does not explain why Pycharm reports that import REPORTS is not a module but still the code would work.Does anyone know why this is occurring?
Set the main folder as the source root. You can do that by right clicking the MAIN folder and navigating to the bottom of the list. Mark the directory as source root. Go into Pycharm settings under console and check add source roots to python path.
Pycharm uses source root to resolve imports. More information can be found here
https://www.jetbrains.com/help/pycharm/content-root.html
Relevant text from link:
Source roots (or source folders; shown as the Source root icon ).
These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports.
The files under the source roots are interpreted according to their type. PyCharm can parse, inspect, index, and compile the
contents of these roots.

Importing a python module to enable a script to be run from command line

I'm new to python and trying to test a script from this github repo (https://github.com/mgp25/psn-api).
The root directory has an example.py and I'm trying to run it with
$ python example.py
which gives this error:
Traceback (most recent call last):
File "example.py", line 1, in <module>
from src.Auth import Auth
ImportError: No module named src.Auth
How can I get this to run?
There is a folder in the root directory named src but because I'm new to python I don't know how to connect things so that the src.Auth module gets imported (or if that's even the right terminology)
Python 3.3+ will happily interpret it as a package without an __init__.py, fwiw, and I believe that's what the author wrote in.
Also note from trying to run it just now, you'll need to install simplejson and requests. (Normally there'd be a requirements.txt or similar saying this.)
Being in the repository root directory, you do:
touch src/__init__.py
This will create an empty file but it is necessary for the Python module search system. Then you should be able to run it without problems, unless there is some dependency on external libraries.

flask.cli.NoAppException: Application crashing

My Flask Application is crashing when I'm trying to access it.
This is similar error to this or this. However, my set-up seems correct.
flask.cli.NoAppException: The file/path provided (server) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py
My environment variable correctly set.
export FLASK_APP=server.py
export FLASK_DEBUG=1
My server file is server.py, and I don't have any __init__.py in the directory.
I don't recall having change anything special in the code. Where does the bug could come from?
The issue was that some package were missing or corrupted.
I reinstalled everything with pip3 install -r requirements.txt --ignore-installed and now it works fine.
This error may be a sign that an import is not found.
To see which import is missing, try to run your server.py file with the python interpreter:
python yourapp.py
Example of output :
Traceback (most recent call last):
File "yourapp.py", line 4, in <module>
from flask_httpauth import HTTPBasicAuth
ImportError: No module named flask_httpauth
Source (and other leads)
For me, solution was fixing a misspelled function name "create_app" in my _ init _.py
from flask import Flask
def create_app(test_config=None):
app = Flask(__name__)
return app
Super late answer, but I just ran into this and the solution was to use a Remote WSL connection in VS Code. I thought I was in the correct folder, but VS Code was looking at a very similar folder in Windows. You can tell you're in the wrong folder by running "touch somefile.abc" in your linux terminal, from the project folder, but that file doesn't automatically appear in VS Code's folder structure.
https://code.visualstudio.com/docs/setup/linux
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl

Testing Google App Engine apps

I am looking to see how others are testing their GAE apps. I've come across a few unit testing frameworks for python (green, pytest, nose, etc).
I then found the tutorial on the GAE site: https://developers.google.com/appengine/docs/python/tools/localunittesting and have been following that.
However, after doing what they say I get the following error:
Traceback (most recent call last):
File "tests/test.py", line 2, in <module>
from google.appengine.ext import db
ImportError: No module named appengine.ext
I am just running
python testrunner.py main/tests usr/local/google_appengine
from the terminal.
Any other ideas?
I have appengine in my pythonpath:
echo $PYTHONPATH
-> /usr/local/google_appengine

How to install the python library - google-api-python-client-gae?

I'm trying to code a program to check files in Google Cloud Storage using Python.
Now I'm read the google doc -> https://developers.google.com/api-client-library/python/start/installation.
At the last step - "App Engine", it asks user to do following:
->> cd myproject
->> unzip google-api-python-client-gae-1.1.zip
So what's the next steps? I'm still having problem to run the Google sample python code about GCS, which throw error:
Traceback (most recent call last):
File "gcs_test.py", line 22, in <module>
from google.appengine.ext import webapp
ImportError: No module named google.appengine.ext
I tried to add below code but still got same error:
from sys import path
path.append(r"C:\Py_dev\gcs_test\google-api-python-client-gae-1.2.zip")
path.append(r"C:\Py_dev\gcs_test\C:\Py_dev\gcs_test\apiclient")
Thanks for all kind help!
google.appengine is the library used by App Engine apps, not by the api client library, the library you are looking for is apiclient
If you wish to run a normal python script instead of an App Engine app, you should remove references to App Engine specific libraries.
If you are running an app engine app, make sure you are using dev_appserver.py or the actual app engine servers, not just running the script using python

Categories

Resources