Using a Django model from a mercurial python hook - python

I'm trying to queue up some build requests in a DB during a changegroup hook. The DB table is managed by a Django app. So, in the hook, I want to use the Django model to submit the build request. However, I'm unable to get my settings right to import the model successfully.
Based on this (https://docs.djangoproject.com/en/1.3/ref/django-admin/) info, all I need to do is add my django site to the sys path, and set the DJANGO_SETTINGS_MODULE. The django site lives in /opt/mysite, so - trying this from the python console:
>>> import sys
>>> import os
>>> sys.path.append('/opt')
>>> os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
>>> from mysite.myapp.models import Build
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/mysite/myapp/models.py", line 1, in <module>
from django.db import models
File "/usr/lib/pymodules/python2.7/django/db/__init__.py", line 78, in <module>
connection = connections[DEFAULT_DB_ALIAS]
File "/usr/lib/pymodules/python2.7/django/db/utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
File "/usr/lib/pymodules/python2.7/django/db/utils.py", line 33, in load_backend
return import_module('.base', backend_name)
File "/usr/lib/pymodules/python2.7/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/pymodules/python2.7/django/db/backends/mysql/base.py", line 28, in <module>
from django.db import utils
ImportError: cannot import name utils
>>>
Second Attempt:
Based on this (http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/) I can use setup_environ(), when I try this in the python shell, it works:
>>> import sys
>>> sys.path.append('/opt')
>>> from django.core.management import setup_environ
>>> from mysite import settings
>>> setup_environ(settings)
'/opt/mysite'
>>> from mysite.myapp.models import Build
>>> Build.objects.all()
[<Build: #1 F nkj sdfsdfsdfs43qg test_branch>, <Build: #2 F nkj sdfsdfsdfs43qg test_branch>, <Build: #13 Q >, <Build: #14 Q nkj_bug243>, <Build: #11 F nkj 444hwe45hedrrt nkj_bug272>, <Build: #12 F nkj sdfsdfsdfs43qg test_branch>]
However, when I try this in my Mercurial hook, I get blasted with errors the minute I try to import my Build model:
#!/usr/bin/env python
import sys
path = '/opt'
if path not in sys.path:
sys.path.append(path)
from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)
from mysite.myapp.models import Build
import datetime
... do stuff ...
When it gets to the 'from mysite.myapp.models import Build' line I get the following error output:
** unknown exception encountered, please report by visiting
** http://mercurial.selenic.com/wiki/BugTracker
** Python 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1]
** Mercurial Distributed SCM (version 1.9.1)
** Extensions loaded:
Traceback (most recent call last):
File "/usr/bin/hg", line 38, in <module>
mercurial.dispatch.run()
File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 27, in run
sys.exit(dispatch(request(sys.argv[1:])))
File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 64, in dispatch
return _runcatch(req)
File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 87, in _runcatch
return _dispatch(req)
File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 679, in _dispatch
cmdpats, cmdoptions)
File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 454, in runcommand
ret = _runcommand(ui, options, cmd, d)
File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 733, in _runcommand
return checkargs()
File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 687, in checkargs
return cmdfunc()
File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 676, in <lambda>
d = lambda: util.checksignature(func)(ui, *args, **cmdoptions)
File "/usr/lib/python2.7/dist-packages/mercurial/util.py", line 385, in check
return func(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/mercurial/commands.py", line 3884, in push
newbranch=opts.get('new_branch'))
File "/usr/lib/python2.7/dist-packages/mercurial/localrepo.py", line 1428, in push
lock=lock)
File "/usr/lib/python2.7/dist-packages/mercurial/localrepo.py", line 1849, in addchangegroup
source=srctype, url=url)
File "/usr/lib/python2.7/dist-packages/mercurial/localrepo.py", line 224, in hook
return hook.hook(self.ui, self, name, throw, **args)
File "/usr/lib/python2.7/dist-packages/mercurial/hook.py", line 160, in hook
mod = extensions.loadpath(path, 'hghook.%s' % hname)
File "/usr/lib/python2.7/dist-packages/mercurial/extensions.py", line 45, in loadpath
return imp.load_source(module_name, path)
File "/opt/unapse/belvedere/mercurial_hook.py", line 14, in <module>
from unapse.belvedere.models import Build
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 109, in _demandimport
mod = _origimport(name, globals, locals)
File "/opt/unapse/belvedere/models.py", line 1, in <module>
from django.db import models
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 109, in _demandimport
mod = _origimport(name, globals, locals)
File "/usr/lib/pymodules/python2.7/django/db/__init__.py", line 14, in <module>
if not settings.DATABASES:
File "/usr/lib/pymodules/python2.7/django/utils/functional.py", line 276, in __getattr__
self._setup()
File "/usr/lib/pymodules/python2.7/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/usr/lib/pymodules/python2.7/django/conf/__init__.py", line 139, in __init__
logging_config_func(self.LOGGING)
File "/usr/lib/python2.7/logging/config.py", line 776, in dictConfig
dictConfigClass(config).configure()
File "/usr/lib/python2.7/logging/config.py", line 575, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'mail_admins': __import__() argument 1 must be string, not DictConfigurator
Any ideas as to what could be causing this problem?

The DictConfigurator error is a bug in python, hopefully fixed in the next 2.7 release (http://bugs.python.org/issue12718), something (probably mercurial) is overriding import and python's logging code fails in that case.
Until then here is what I did to fix it. Copy dictconfig.py from django/utils to somewhere in your python system path (I put it alongside my hook). Change the line
importer = __import__
to:
def importer(self, *args):
return __import(*args)__
And then in settings.py for your django project add to the end:
LOGGING_CONFIG = 'website.dictconfig.dictConfig'
Update website.dictconfig to whatever path you put the dictconfig file into so python can find it.
UPDATE: I think there is an even easier way. Add this to the top of your hook script, before importing anything else:
from mercurial import demandimport;
demandimport.disable()
This makes Mercurial revert the import override and seems to solve the problem cleanly.

If you're running with your environment in a virtualenv dir you'll need to add that to the site packages and I'd also suggest that you put the django dir at the front of sys.path instead of the end.
Here's the code we use to integrate our Django app into other python apps.
import os
import sys
# point this at the virtualenv dir that your django deployment runs out of
# you are using virtualenv. right?
DJANGO_ENV = '/home/core/python-envs/production'
# where your code lives (ie where settings.py is)
DJANGO_DIR = '/home/core/code/production'
# add our site packages
import site
site_packages = os.path.join(DJANGO_ENV, 'lib', 'python%s' % sys.version[:3], 'site-packages')
site.addsitedir(site_packages)
# put the main Django directory on first
sys.path.insert(0, DJANGO_DIR)
# setup the Django environment pointing to our settings
import settings
import django.core.management
django.core.management.setup_environ(settings)
# finally, import our objects
from yourapp.models import YourModel
One last thing to note is that when we import our code into some applications we've run into scoping issues and imports clashing. To get around it we wrap the work we want to do in a function so that it gets its own scope.
For example, instead of:
from yourapp.models import YourModel
objs = YourModel.objects.filter(something=True)
...
Do something like:
def do_work():
from yourapp.models import YourModel
objs = YourModel.objects.filter(something=True)
...
do_work()

Related

Flask(flask db init): AttributeError: module 'time' has no attribute 'clock'

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

Django 1.9 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet

Trying to use django-shopify-sync in a Django 1.9 project. When loading the config for the app it gives me the following error, likely because it's trying to load some models in the config?
Tried moving the the two imports that eventually import models into the ready() function below, but still getting the same error. Culpirt lines 2 and 3 in the following file https://github.com/andresdouglas/django-shopify-sync/blob/master/shopify_sync/apps.py
The error is:
$ python manage.py runserver
Unhandled exception in thread started by <function wrapper at 0x10753e500>
Traceback (most recent call last):
File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/andres/.virtualenvs/[...]/src/shopify-sync/shopify_sync/apps.py", line 2, in <module>
from shopify_sync.handlers import webhook_received_handler
File "/Users/andres/.virtualenvs/[...]/src/shopify-sync/shopify_sync/handlers.py", line 3, in <module>
from .models import (CustomCollection, Customer, Order, Product, Shop,
File "/Users/andres/.virtualenvs/[...]/src/shopify-sync/shopify_sync/models/__init__.py", line 3, in <module>
from .address import Address
File "/Users/andres/.virtualenvs/[...]/src/shopify-sync/shopify_sync/models/address.py", line 6, in <module>
from .base import ShopifyResourceModel
File "/Users/andres/.virtualenvs/[...]/src/shopify-sync/shopify_sync/models/base.py", line 144, in <module>
class ShopifyResourceModel(models.Model):
File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/andres/.virtualenvs/[...]/lib/python2.7/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/Users/andres/.virtualenvs/[...]/lib/python2.7/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.
UPDATE: If I move the following lines (model imports) https://github.com/andresdouglas/django-shopify-sync/blob/master/shopify_sync/handlers.py#L3-L4 to inside get_topic_models it seems to fix the error. But that's kind of dirty, can anyone come up with a better solution?
If you move the following lines (model imports) https://github.com/andresdouglas/django-shopify-sync/blob/master/shopify_sync/handlers.py#L3-L4
from .models import (CustomCollection, Customer, Order, Product, Shop,
SmartCollection)
inside get_topic_models it seems to fix the error. But that's kind of dirty, can anyone come up with a better solution?
It looks like you may have an ordering issue. Make sure your application is in the INSTALLED_APPS tuple after django-shopify-sync. You can find a few more details in the Application registry documentation.
As unsatisfying as an inline import is, you may be stuck with it. I'd suggest moving
from shopify_sync.handlers import webhook_received_handler
from shopify_webhook.signals import webhook_received
into the ready method in apps.py. This will delay the import until the models are ready.
The change I tried is:
diff --git a/shopify_sync/apps.py b/shopify_sync/apps.py
index 663b43b..0bc1fcc 100644
--- a/shopify_sync/apps.py
+++ b/shopify_sync/apps.py
## -1,7 +1,5 ##
from django.apps import AppConfig
-from shopify_sync.handlers import webhook_received_handler
-from shopify_webhook.signals import webhook_received
-
+import importlib
class ShopifySyncConfig(AppConfig):
"""
## -16,5 +14,9 ## class ShopifySyncConfig(AppConfig):
The ready() method is called after Django setup.
"""
+ signals_webhook_received = importlib.import_module('.signals', package='shopify_webhook')
+ handlers_webhook_received_handler = importlib.import_module('.handlers', package='shopify_sync')
+
# Connect shopify_webhook's webhook_received signal to our synchronisation handler.
- webhook_received.connect(webhook_received_handler, dispatch_uid = 'shopify_sync_webhook_received_handler')
+ signals_webhook_received.webhook_received.connect(handlers_webhook_received_handler.webhook_received_handler, dispatch_uid = 'shopify_sync_webhook_received_handler')
+

Can't import django.db.models from Python GUI

Whenever I type this command in Python GUI:
from django.db import models
it gives me an error like this
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
from django.db import models
File "C:\Python27\lib\site-packages\django\db\models\__init__.py", line 5, in <module>
from django.db.models.query import Q
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 17, in <module>
from django.db.models.deletion import Collector
File "C:\Python27\lib\site-packages\django\db\models\deletion.py", line 4, in <module>
from django.db.models import signals, sql
File "C:\Python27\lib\site-packages\django\db\models\sql\__init__.py", line 4, in <module>
from django.db.models.sql.subqueries import *
File "C:\Python27\lib\site-packages\django\db\models\sql\subqueries.py", line 12, in <module>
from django.db.models.sql.query import Query
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 22, in <module>
from django.db.models.sql import aggregates as base_aggregates_module
File "C:\Python27\lib\site-packages\django\db\models\sql\aggregates.py", line 9, in <module>
ordinal_aggregate_field = IntegerField()
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 116, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
self._setup(name)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
What can it be the problem? I have django in directory C:\Python27\Lib\site-packages\django\db and in there i have a file called models.
This is the pic
Bizarre isn't it?
Not all of Django supports simply being loaded as a Python module, unfortunately; you need to use the means provided by Django itself to bootstrap the environment needed to load stuff like models.
One option is to use the Django shell but it's also possible to use a purely programmatic solution from your own code. See https://docs.djangoproject.com/en/dev/topics/settings/ for all the options.
Here's also an example by somebody else https://gist.github.com/jordanorelli/1025419 but I haven't verified if it's up to date.
The answer is in the error message:
You must either define the environment variable DJANGO_SETTINGS_MODULE or...
For this reason Django provides a shortcut to loading a python shell with Django settings configured:
https://docs.djangoproject.com/en/dev/ref/django-admin/#shell

Inheritance in web.py?

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

python: GeoDjango: Eclipse: During Unit Test, raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?)

I am creating unit test py file (tests.py) then i received the below error messages.
Could you give me some idea on how to resolve it.
pydev debugger: warning: psyco not available for speedups (the debugger will still work correctly, but a bit slower)
pydev debugger: starting
Finding files... done.
Importing test modules ... Traceback (most recent call last):
File "C:\Development\eclipse-SDK-3.7-win32\plugins\org.python.pydev.debug_2.2.3.2011100616\pysrc\pydev_runfiles.py", line 307, in __get_module_from_str
mod = __import__(modname)
File "C:\Development\workspace37_pydev\pyproject\py23\service\rs\tests.py", line 4, in <module>
from django.test.testcases import TestCase
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\test\__init__.py", line 6, in <module>
from django.test.testcases import TestCase
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\test\testcases.py", line 10, in <module>
from django.db import transaction
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\db\__init__.py", line 9, in <module>
if not settings.DATABASE_ENGINE:
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\conf\__init__.py", line 28, in __getattr__
self._import_settings()
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\conf\__init__.py", line 59, in _import_settings
self._target = Settings(settings_module)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\conf\__init__.py", line 94, in __init__
raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'py23.settings' (Is it on sys.path? Does it have syntax errors?): No module named py23.settings
ERROR: Module: tests could not be imported (file: C:\Development\workspace37_pydev\pyproject\py23\service\rs\tests.py).
done.
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Any guidance on the right direction is appreciated.
UPDATES1
I add my tests.py source code
# -*- encoding:utf-8 -*-
from django.contrib.gis.geos import GEOSGeometry
from django.test.testcases import TestCase
from django.utils import simplejson as json
import unittest
import utils
class RSCapTestCase(TestCase):
fixtures = [ 'rs.json' ]
def test_001_normal(self):
response = self.client.get('/service/rs/cap', {})
cap = json.loads(response.content)
self.assertEqual(response.status_code, 200)
self.assertEqual(cap['version'], '1.0')
self.assertEqual(len(cap['rs']), 3)
self.assertEqual(cap['rs'][0], 'r1')
self.assertEqual(cap['updateSequence'], '0001')
def test_002_version(self):
response = self.client.get('/service/rs/cap/1.0', {})
cap = json.loads(response.content)
self.assertEqual(response.status_code, 200)
self.assertEqual(cap['version'], '1.0')
self.assertEqual(len(cap['rs']), 3)
self.assertEqual(cap['rs'][0], 'r1')
self.assertEqual(cap['updateSequence'], '0001')
def test_003_version_sequence(self):
response = self.client.get('/service/rs/cap/1.0/0001', {})
cap = json.loads(response.content)
self.assertEqual(response.status_code, 200)
self.assertEqual(cap['version'], '1.0')
self.assertEqual(len(cap['rs']), 3)
self.assertEqual(cap['rs'][0], 'r1')
self.assertEqual(cap['updateSequence'], '0001')
UPDATES2
I added the below code
_ _ init _ _.py (same folder with tests.py)
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'py23.settings'
Here's the console when I execute tests.py
Finding files... done.
Importing test modules ... done.
======================================================================
ERROR: test_001_normal (py23.service.rs.tests.RSCapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\test\testcases.py", line 202, in __call__
self._pre_setup()
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\test\testcases.py", line 183, in _pre_setup
call_command('flush', verbosity=0, interactive=False)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\core\management\__init__.py", line 158, in call_command
return klass.execute(*args, **options)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\core\management\base.py", line 210, in execute
translation.activate('en-us')
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\utils\translation\__init__.py", line 73, in activate
return real_activate(language)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\utils\translation\__init__.py", line 43, in delayed_loader
return g['real_%s' % caller](*args, **kwargs)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\utils\translation\trans_real.py", line 209, in activate
_active[currentThread()] = translation(language)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\utils\translation\trans_real.py", line 198, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\utils\translation\trans_real.py", line 181, in _fetch
app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:])
AttributeError: 'module' object has no attribute 'messages'
.
.
.
----------------------------------------------------------------------
Ran 3 tests in 0.234s
FAILED (errors=43)
Here's what I did to solve this problem without any changes to code:
Go into the project properties in Eclipse
Go to "PyDev - PYTHONPATH" and then to the tab "String substitution variables"
Click "Add variable"
Name: "DJANGO_SETTINGS_MODULE", Value: location of your settings module (like "py23.settings" in the answer by eros)
That did it for me, at least.
I just add the following:
import os
from os.path import abspath, dirname
import sys
# Set up django
project_dir = abspath(dirname(dirname(__file__)))
sys.path.insert(0, project_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'py23.settings'

Categories

Resources