I'm attempting to use the command dev_appserver.py ./ from my application directory, but it's throwing the following error.
ERROR 2016-01-04 04:47:51,421 wsgi.py:263]
Traceback (most recent call last):
File "/Users/Joshua/Scripts/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Users/Joshua/Scripts/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/Users/Joshua/Scripts/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/Users/Joshua/Projects/inkren/skritter-api/main.py", line 2, in <module>
from gcloud import datastore
ImportError: No module named gcloud
I'm running a virtualenv that has gcloud installed and also have the gcloud sdk installed with the proper python component. The gcloud library is found when I run python ./main.py.
# main.py
import webapp2
from gcloud import datastore
class MainPage(webapp2.RequestHandler):
def get(self):
client = datastore.Client(dataset_id='my-project')
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
What I'd like to do is be able to run my code locally in the browser. Is there a way to get dev_appserver.py to recognize the gcloud library?
Is using the python development server from gcloud still a problem? I'm successfully using dev_appserver.py from the terminal. See reference.
Perhaps the install and setup instructions have improved since a year ago. I believe the installation puts the file, dev_appserver.py, in your system path, even on Windows, so it is accessible from the terminal.
I did a bit of research on this, so... Okay, here's what you've got to do.
takes a breath
Head over to this gentleman's GitHub page, and download the package as a zip. https://github.com/dhermes/test-gcloud-on-gae
Now, you need to add some folders/files from his folder into yours to get gcloud working.
1) Open the application folder and put the vendor folder into your root directory. This folder contains the gcloud package, as well as many others. You can delete the others if you want.
2) Add the appengine_config.py into your root directory as well. This file tells app engine where your third party packages are.
3) Add darth.py to your root directory as well. This is just a help file.
Actually, that should do it. This gentleman wrote some scripts for using third party apps with app engine, and they're quite useful. Let me know if your import works after doing that!
I tested it locally and it works perfectly. Basically, any third party python packages work with app engine, as long as there are no other languages within them such as C.
Related
Deploying Google Cloud Functions from the local system fails with the following error:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function load error: File main.py that is expected to define function doesn't exist
I've used the console UI to check the contents of the package that was uploaded in the failed deployment and the file is present. The package was created using the gcloud CLI:
gcloud functions deploy <redacted> \
--trigger-http \
--runtime=python37 \
--region=europe-west1 \
--project=<redacted> \
--entry-point=<redacted>
For context, the same project was deployed successfully multiple times by several people but started failing for everyone new that checked it out after a specific date.
In our case, this was because of invalid credentials, i.e. the JSON file passed to ServiceAccountCredentials.from_json_keyfile_name was missing. Confusingly, the error doesn't mention anything about security, credentials or that missing file.
Secrets are not in version control and the shared vault had a stale file that did not match the path provided in the python script. This was fixed once the credentials were corrected.
We managed to isolate the problem to the authenticated call by successfully deploying 'dumb' functions (return a string) and then incrementally adding back functionality until it broke.
I solved this problem in my application in Node.js, basically I identified that the .gcloudignore file it invokes the .gitignore of the project, so if your function is in a skipped folder it simply does not find, then just remove the line #! Include : .gitignore file .gcloudignore
I have a simple python file that I am trying to set up to utilize sessions, when i run the file I am receiving the error below:
ModuleNotFoundError: No module named 'flask_session'
I believe I am importing the module properly, is there anything else that I can check to set this up properly?
from flask import Flask, render_template, request, session
from flask_session import Session
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
#app.route("/", methods=["GET", "POST"])
def index():
if session.get("notes") is None:
session["notes"] = []
if request.method == "POST":
note = request.form.get("note")
session["notes"].append(note)
return render_template("index.html", notes=notes)
Here is the traceback ( most recent call last )
File "c:\python37\lib\site-packages\flask\cli.py", line 325, in __call__
Open an interactive python shell in this frameself._flush_bg_loading_exception()
File "c:\python37\lib\site-packages\flask\cli.py", line 313, in _flush_bg_loading_exception
reraise(*exc_info)
File "c:\python37\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "c:\python37\lib\site-packages\flask\cli.py", line 302, in _load_app
self._load_unlocked()
File "c:\python37\lib\site-packages\flask\cli.py", line 317, in _load_unlocked
self._app = rv = self.loader()
File "c:\python37\lib\site-packages\flask\cli.py", line 372, in load_app
app = locate_app(self, import_name, name)
File "c:\python37\lib\site-packages\flask\cli.py", line 242, in locate_app
'\n\n{tb}'.format(name=module_name, tb=traceback.format_exc())
I've first encountered this problem while doing Harvard's CS50x class. I'm using Linux and I've been using Python3, as it turns out I've installed Flask-Session for Python2.
I don't know whether it also applies to Mac but I used the following instead:
$ pip3 install flask-session
Then you can check whether it's installed with pip's freeze command. After doing this my pylint in VSCode no longer gave an error.
I got the flask_session module not found a number of times and it took me 2 hours to find a fix after thousands of attempts. Well, I too visited a number of sites looking for a solution but didn't get any. I got this error while I was learning the online course cs50 web development on edx.org by Harvard and when I read the forum discussion on reddit I saw comments where they mentioned Harvard used a special server to make it work and I felt the pain of not going to Harvard but two hours later I'm screaming because I just found a solution that's no where on the internet.
You should not use from flask_session import Session
Instead use from flask_session.__init__ import Session
I cannot explain the terminology as to why we do this here but you can private message me if you want to understand why we do this. Happy Coding!
Be sure to have the extension installed:
pip install Flask-Session
oficial page with instructions
If you get the error "ModuleNotFoundError: No module named 'werkzeug.contrib'":
pip install werkzeug==0.16.0
The latest version is the 1.0.1 but only the 0.16.0 worked.
(where I found this solution) (official page)
simply goto terminal and type pip install flask_session , wait for installation, once done you are good to go. ( Also many other solutions do not work, look for red highlighted part in image. Once installed it works properly.)
I ran into the same error.
The following worked for me.
Install the extension with the following command:
$ easy_install Flask-Session
or alternatively, if you have pip installed:
$ pip install Flask-Session
I got it for windows:
There's no the library "flask_session" (--is a directory--) in your directory ...venv\Lib\site-packages (virtual environment)
You just need copy it from C:\Users\your _user\AppData\Local\Programs\Python\Python37-32\Lib\site-packages
and then paste it in venv\Lib\site-packages or where you have installed your virtual environment. Thats it and sorry for my english, greetings from Medellin Colombia.
Try to do following command instead of all go to terminal and type
$ easy_install Flask-Session
If it shows an error,use sudo before it , If it shows any error please let me know also feel free to upvote
Install the extension first using the following command $ pip install Flask-Session. You can read more on flask-session via https://pythonhosted.org/Flask-Session/
For completeness, I will share my solution:
In my case installed everything with pip3 to make it work with python 3.7, only worked with vscode after uninstalling python 2 from the system and reopening vscode.
Are you using this library? https://pythonhosted.org/Flask-Session/
The docs suggest importing it this way: from flask.ext.session import Session
I am trying to run/debug my python project from IntelliJ Ultimate 2018.1. I have defined a python SDK, a Django local server (as the project uses Django as a template language), the PYTHONPATH is properly defined, etc. If I execute
python manage.py runserver
from my MacOS terminal, the server starts normally.
When I am trying to run my IntelliJ configuration, it fails, with message:
/usr/bin/python2.7 -d /Users/my_user/dev/github/my_project/manage.py runserver 8000
Traceback (most recent call last):
File "/Users/my_user/dev/github/my_project/manage.py", line 19, in <module>
paths = importlib.import_module('settings.' + server + '.paths')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/my_user/dev/github/my_project/settings/__init__.py", line 11, in <module>
paths = importlib.import_module('my_project.settings.' + server + '.paths')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named my_project.settings.local.paths
The settings.local folder does contains the __init__.py file, and also settings folder and the root folder of my project.
Doing a few hours of research over the internet I have realised that the main problem is to include local modules on the classpath, so they are available for server startup. For PyCharm, this is clearly explained here, and I was able to run the server from PyCharm. However, when trying to do the same thing in IntelliJ, this never works. Some people write about it here for example, but that didn't worked out for me.
I have tried using a system interpreter and also a virtual environment, created with PyCharm, with no luck. I am using python 2.7, Django 1.11 and MacOS High Sierra. Is this even possible with IntelliJ? I wonder why would people buy PyCharm if the same things can be accomplished (and so much more) with IntelliJ. Also, I have noticed that when I change a setting in IntelliJ run profile (the Django local server profile), this change is reflected back in PyCharm and viceversa. This seems to me like a bug from JetBrains, as two distinct apps somehow share the same config, probably due to the fact that the same project is loaded in both apps.
The problem was in fact trivial, as clarified with JetBrains support: I needed to set the parent folder of my_project as content root (/Users/my_user/dev/github/), instead of using /Users/my_user/dev/github/my_project/ directly. This was needed because the source code contained imports from module my_project which could not be resolved, as there was no subfolder my_project inside of my main project folder. I implemented this change in Project settings -> Modules view, Project settings -> Facets view and also in the run configuration.
Im running a solaris server which uses supervisor to monitor some Python applications.
Previously, I could run the command:
paster serve /opt/pyapps/menuadmin/prod.ini
from any directory on the server. There were some recent issues and the /opt folder was restored from a previous backup. This folder contained all of the applications including supervisor.
Now we are facing issues where supervisor will not start the applications because of "version conflicts" in Pylons.
This is where it gets weird and it makes no sense why these errors would occur.
If I run the paster command from outside of the program directory, it will throw the version conflict error. eg:
cd /
paster serve /opt/pyapps/menuadmin/prod.ini
Traceback (most recent call last):
File "/opt/csw/bin/paster", line 8, in <module>
load_entry_point('PasteScript==1.7.5', 'console_scripts', 'paster')()
File "/opt/csw/lib/python2.6/site-packages/PasteScript-1.7.5-py2.6.egg/paste/script/command.py", line 93, in run
commands = get_commands()
File "/opt/csw/lib/python2.6/site-packages/PasteScript-1.7.5-py2.6.egg/paste/script/command.py", line 135, in get_commands
plugins = pluginlib.resolve_plugins(plugins)
File "/opt/csw/lib/python2.6/site-packages/PasteScript-1.7.5-py2.6.egg/paste/script/pluginlib.py", line 82, in resolve_plugins
pkg_resources.require(plugin)
File "/opt/csw/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/pkg_resources.py", line 626, in require
File "/opt/csw/lib/python2.6/site-packages/setuptools-0.6c9-py2.6.egg/pkg_resources.py", line 528, in resolve
pkg_resources.VersionConflict: (Pylons 0.9.7 (/opt/csw/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg), Requirement.parse('Pylons>=0.10'))
But if I run the command from inside the program directory, it will run fine. eg:
cd /opt/pyapps/menuadmin/
paster serve /opt/pyapps/menuadmin/prod.ini
Starting server in PID 29902.
serving on http://127.0.0.1:3002
I absolutely cannot get my head around why this would happen!
Any thoughts or comments at all are appreciated!!!!
Based upon what you have said it seems you are running two different version of paster. The first version is running the older Pylons package 0.9.7, whilst the second has the more up to date version that meets or exceeds your app's requirements.
What I would do is first check which version of paster you are running. From outside of the project just run:
which paster
Then run the same command again within the project directory and compare the results. I suspect that you will find that the paths differ. If that is the case then all you need to do is update the version of pylons for the first version, which I'm guessing is the global install.
However, as others have commented it would be better to run apps within virtualenv, especially if as you seem to indicate you have multiple virtualenv and thus multiple projects. Trust me when I say it will save you from loads of headaches later on, from someone that didn't do this originally.
I recently began my work using web.py, but I'm stuck with a problem since web.py needs to have flup installed.
I downloaded flup from http://www.saddi.com (download Link) and then extracted it to a folder on my desktop.
I then navigate to the folder and run setup.py install. On success, I get an egg file in my C:\Python27\Lib\site-packages.
However, upon running the code below (taken from webpy.org):
#!/usr/bin/python
import web
urls = ("/.*", "hello")
app = web.application(urls, globals())
class hello:
def GET(self):
return 'Hello, world!'
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
if __name__ == "__main__":
app.run()
I get this error:
AttributeError: 'module' object has no
attribute 'fromfd'
Following this site's advice, I intended to change the contents of a file called fcgi_base.py. However this file is included in the said egg file. What I want to know is how do I change this Python file?
I think I can open the egg file with winrar and then edit the said file, but should I change it back to an egg file again to actually make it work?
Thank you for reading. Also, if you've had experience making web.py work, your advice would be much appreciated.
You need to specify the address and port that you've configured apache to forward to. For example, if you configured Apache's FCGI to forward to 127.0.0.1:8080 you would do:
web.wsgi.runwsgi = lambda func, addr=('127.0.0.1', 8080): web.wsgi.runfcgi(func, addr)