I am trying to follow below tutorial but I am facing few issue, when i run manage.py. Any help could be great help.
http://docs.mongodb.org/ecosystem/tutorial/write-a-tumblelog-application-with-flask-mongoengine/#id1
manage.py run output:
(Tumbler)afiz Tumbler $ python manage.py
Traceback (most recent call last):
File "manage.py", line 6, in <module>
from tumblelog import app
File "/home/afiz/.virtualenvs/tumblelog/__init__.py", line 18, in <module>
register_blueprints(app)
File "/home/afiz/.virtualenvs/tumblelog/__init__.py", line 13, in register_blueprints
from tumblelog.views import posts
File "/home/afiz/.virtualenvs/tumblelog/views.py", line 5, in <module>
from tumblelog.models import Post, Comment
File "/home/afiz/.virtualenvs/tumblelog/models.py", line 6, in <module>
class Post(db.DynamicDocument):
File "/home/afiz/.virtualenvs/Tumbler/local/lib/python2.7/site-packages/mongoengine/base/metaclasses.py", line 361, in __new__
meta['index_specs'] = new_class._build_index_specs(meta['indexes'])
File "/home/afiz/.virtualenvs/Tumbler/local/lib/python2.7/site-packages/mongoengine/base/document.py", line 722, in _build_index_specs
unique_indices = cls._unique_with_indexes()
File "/home/afiz/.virtualenvs/Tumbler/local/lib/python2.7/site-packages/mongoengine/base/document.py", line 861, in _unique_with_indexes
field.document_type != cls):
File "/home/afiz/.virtualenvs/Tumbler/local/lib/python2.7/site-packages/mongoengine/fields.py", line 563, in document_type
self.document_type_obj = get_document(self.document_type_obj)
File "/home/afiz/.virtualenvs/Tumbler/local/lib/python2.7/site-packages/mongoengine/base/common.py", line 25, in get_document
""".strip() % name)
mongoengine.errors.NotRegistered: `Comment` has not been registered in the document registry.
Importing the document class automatically registers it, has it
been imported?
manage.py file:
#set the path
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from flask.ext.script import Manager, Server
from tumblelog import app
manager = Manager(app)
# Turn on debugger by default and reloader
manager.add_command("runserver", Server(
use_debugger = True,
use_reloader = True,
host = '0.0.0.0')
)
if __name__ == "__main__":
manager.run()
I had the same problem as you are facing now. In models.py file I just wrote
class Comment(db.EmbeddedDocument):
and it's content first then added
class Post(db.Document):
and then it's content. In other words, I first wrote Comment class then Post class and problem got solved ;) :) Cheers!!
Inside Post, when EmbeddedDocumentField is assigned to a variable, it needs to be pre-registered. Therefore, always register such field i.e. Comments before they are used.
Related
While following a Flask tutorial I stumbled around this very strange problem! While setting up the Migrate directory , after the first step of setting the flask app using set FLASK_APP=sql1.py, when I ran this command flask db init I got this error:
(first_flask_env) C:\Users\aakash\Desktop\python programs>flask db init
Traceback (most recent call last):
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\aakash\Anaconda3\envs\first_flask_env\Scripts\flask.exe\__main__.py", line 7, in <module>
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\flask\cli.py", line 894, in main
cli.main(args=args, prog_name=name)
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\flask\cli.py", line 557, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\click\core.py", line 697, in main
rv = self.invoke(ctx)
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\click\core.py", line 1061, in invoke
cmd_name, cmd, args = self.resolve_command(ctx, args)
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\click\core.py", line 1100, in resolve_command
cmd = self.get_command(ctx, cmd_name)
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\flask\cli.py", line 500, in get_command
self._load_plugin_commands()
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\flask\cli.py", line 496, in _load_plugin_commands
self.add_command(ep.load(), ep.name)
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\pkg_resources\__init__.py", line 2472, in load
return self.resolve()
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\pkg_resources\__init__.py", line 2478, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\flask_migrate\__init__.py", line 8, in <module>
from alembic import __version__ as __alembic_version__
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\__init__.py", line 8, in <module>
from . import op # noqa
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\op.py", line 1, in <module>
from .operations.base import Operations
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\operations\__init__.py", line 1, in <module>
from .base import Operations, BatchOperations
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\operations\base.py", line 3, in <module>
from .. import util
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\util\__init__.py", line 6, in <module>
from .pyfiles import ( # noqa
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\util\pyfiles.py", line 6, in <module>
from mako.template import Template
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\template.py", line 10, in <module>
from mako.lexer import Lexer
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\lexer.py", line 11, in <module>
from mako import parsetree, exceptions, compat
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\parsetree.py", line 9, in <module>
from mako import exceptions, ast, util, filters, compat
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\exceptions.py", line 11, in <module>
from mako import util, compat
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\util.py", line 11, in <module>
from mako import compat
File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\compat.py", line 124, in <module>
time_func = time.clock
AttributeError: module 'time' has no attribute 'clock'
I'm getting the exact same error while running my .py script also,here's the script:
from flask import Flask
import os
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
basedir = os.path.abspath(os.path.dirname(__file__)) #Full directory path of the file I'm working with..here, sql1.py
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Migrate(app,db) #Here it connect the application "app.py"
# with the database "db"
class puppy(db.Model):
__tablename__ = 'Name Provided by me!!'
id = db.Column(db.Integer,primary_key = True)
name = db.Column(db.Text)
age = db.Column(db.Integer)
def __init__(self,name,age):
self.name = name
self.age = age
def __repr__(self):
return f"puppy {self.name} is {self.age} year/s old!"
I looked into every solution I found (here and this)and made sure everything was correct! I ran this command when my environment was activated which had installed every package needed including SQLAlchemy and Flask-Migrate.
I even deleted them (packages) and re-installed their latest versions but still getting the same error!
I'm using Python 3.8.5
You have some dependencies that are too old and incompatible with Python 3.8. At the very least, you should update the mako package:
pip install --upgrade mako
I had a similar issue I had to update flask_sqlalchemy even after doing this I had issues. I ended up having to make a new virtual environment, I would make a small hello world project on another virtual environment to see if that works.
For me, it was giving the same error, so I removed __init__.py file from the folder which has app.py
After removing I ran the
flask init db
it worked :)
I have a python program that uses django and it generates the error django.template.exceptions.TemplateDoesNotExist. I made a sample of code to illustrate my problem. I created two files a test_file.py and HelloTemplate.txt, located in the same directory C:\Games\Game1.
#file name: test_file.py
from django.template.loader import get_template
from django.conf import settings
settings.configure(DEBUG=True,TEMPLATE_DIRS=('.', ))
#I also tried TEMPLATE_DIRS=('C:\Games\Game1', ), didn't solve the problem
import sys
def main():
myTemplate = get_template("HelloTemplate.txt")
if __name__ == '__main__':
main()
The second file is a txt file with a line Hello as fellow:
filename: HelloTemplate.txt
Hello Template
Here's the error that I got
Traceback (most recent call last):
File "test_file.py", line 17, in <module>
main()
File "test_file.py", line 13, in main
myTemplate = get_template("HelloTemplate.txt")
File "C:\WinPython-32bit-2.7.5.3\python-2.7.5\lib\site-packages\django\template\loader.py", line 25, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: HelloTemplate.txt
Thanks for your help to fix this error.
I guess get_template searches the file's concrete path which you define in the django project settings.py.
I am new to django. Want to create tables in the Database of my project, so I searched the tutorial.
models.py:
from django.db import models
class Blah(models.Model):
first = models.IntegerField()
second = models.IntegerField()
Script to write data into db:
from django.conf import settings
settings.configure()
from core.models import Blah
b = Blah(first = 1, second = 2)
b.save()
When I am trying to launch the script with django 1.9, it gives me the error:
C:\Python27\Scripts\reports>to_db.py
Traceback (most recent call last):
File "C:\Python27\Scripts\reports\to_db.py", line 4, in <module>
from core.models import Blah
File "C:\Python27\Scripts\reports\core\models.py", line 5, in <module>
class Blah(models.Model):
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Python27\lib\site-packages\django\apps\registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "C:\Python27\lib\site-packages\django\apps\registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I have already added the app to INSTALLED_APPS and I am able to do the exact same commands with "manage.py shell", but not with the script.
What am I missing here?
Thanks
If you do need to script against Django (and the ORM), you'll want to use the shell admin command, passing your script as input to it. Related question
I've just started with GAE with Eclipse under Ubuntu 12.10 and when I try to run a sample app, I've stuck with an error that I can't solve:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
application = webapp.WSGIApplication([('/', MainPage)], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
I got this error:
Traceback (most recent call last):
File "/home/mordrec/workspace/helloworld/helloworld.py", line 20, in <module>
main()
File "/home/mordrec/workspace/helloworld/helloworld.py", line 17, in main
run_wsgi_app(application)
File "/opt/google_appengine/google/appengine/ext/webapp/util.py", line 98, in run_wsgi_app
run_bare_wsgi_app(add_wsgi_middleware(application))
File "/opt/google_appengine/google/appengine/ext/webapp/util.py", line 116, in run_bare_wsgi_app
result = application(env, _start_response)
File "/opt/google_appengine/google/appengine/ext/webapp/_webapp25.py", line 688, in __call__
match = regexp.match(request.path)
File "/opt/google_appengine/lib/webob_1_1_1/webob/request.py", line 303, in path
urllib.quote(self.path_info, PATH_SAFE))
File "/opt/google_appengine/lib/webob_1_1_1/webob/descriptors.py", line 23, in fget
return req.environ[key]
KeyError: 'PATH_INFO'
I was facing the exact same issue (but on mac) an hour or two ago. Step #5 ("Deploy It Locally") on the below instructions worked for me.
You can follow these same instructions, more or less, to get debugging to work (just do "Debug As").
http://www.mkyong.com/google-app-engine/google-app-engine-python-hello-world-example-using-eclipse/
To run it locally, right click on the helloworld.py, choose “Run As” –> “Run Configuration”, create a new “PyDev Google App Run“.
In Main tab -> Main module, manually type the directory path of “dev_appserver.py“. “Browse” button is not able to help you, type manually.
In Arguments tab -> Program arguments, put "${project_loc}/src".
Run it. By default, it will deploy to localhost:8080.
Done.
I am currently developing wep.py application. This is my web application which is binded with web.py and wsgi.
root/main.py
import web
import sys
import imp
import os
sys.path.append(os.path.dirname(__file__))
#from module import module
from exam import exam
urls = (
'/exam', 'exam'
)
application = web.application(urls, globals(), autoreload = True).wsgifunc()
My application has an abstract class called module in module.py in root directory and its purpose is to be inherited by modules.
root/module.py
class module:
def fetchURL(self, url):
# ...
return content
The lower level module called "exam" would inherits module
root/exam/init.py
from module import module
class exam(module):
def getResults(self):
# error occurs here
self.fetchURL('math.json')
When I call the parent method, web.py raises an exception
WalkerError: ('unexpected node type', 339)
Environment: Python 2.5
How can I resolve the problem? Thanks
// EDIT 03 July 10:22 GMT+0
The stack trace is as follows
mod_wsgi (pid=1028): Exception occurred processing WSGI script 'D:/py/labs_library/index.py'.
Traceback (most recent call last):
File "D:\csvn\Python25\lib\site-packages\web\application.py", line 277, in wsgi
result = self.handle_with_processors()
File "D:\csvn\Python25\lib\site-packages\web\application.py", line 247, in handle_with_processors
return process(self.processors)
File "D:\csvn\Python25\lib\site-packages\web\application.py", line 244, in process
raise self.internalerror()
File "D:\csvn\Python25\lib\site-packages\web\application.py", line 467, in internalerror
return debugerror.debugerror()
File "D:\csvn\Python25\lib\site-packages\web\debugerror.py", line 305, in debugerror
return web._InternalError(djangoerror())
File "D:\csvn\Python25\lib\site-packages\web\debugerror.py", line 290, in djangoerror
djangoerror_r = Template(djangoerror_t, filename=__file__, filter=websafe)
File "D:\csvn\Python25\lib\site-packages\web\template.py", line 845, in __init__
code = self.compile_template(text, filename)
File "D:\csvn\Python25\lib\site-packages\web\template.py", line 924, in compile_template
ast = compiler.parse(code)
File "D:\csvn\Python25\lib\compiler\transformer.py", line 51, in parse
return Transformer().parsesuite(buf)
File "D:\csvn\Python25\lib\compiler\transformer.py", line 128, in parsesuite
return self.transform(parser.suite(text))
File "D:\csvn\Python25\lib\compiler\transformer.py", line 124, in transform
return self.compile_node(tree)
File "D:\csvn\Python25\lib\compiler\transformer.py", line 167, in compile_node
raise WalkerError, ('unexpected node type', n)
WalkerError: ('unexpected node type', 339)
If it is possible I would like to turn off the template functionality as I use python only for JSON output for mobile app.
if you create python module you should add __init__.py in top of your hierarchy:
dvedit/
__init__.py
clipview.py
filters/
__init__.py
it means that in every directory which will be imported via from ... import ... should have __init__.py file.
further info available: http://wiki.cython.org/PackageHierarchy