database Django on jython - database

please i need help connecting database with jython on django and help me with the server when i put jython manage.py runserver the server don´t run
DATABASES = {
'default': {
#'ENGINE': 'doj.backends.zxjdbc.sqlite3',
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tanagraweb',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
and django throw this:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\jython2.7b3\Lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\jython2.7b3\Lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\jython2.7b3\Lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\jython2.7b3\Lib\site-packages\django\core\management\base.py", line 280, in execute
translation.activate('en-us')
File "C:\jython2.7b3\Lib\site-packages\django\utils\translation\__init__.py", line 130, in activate
return _trans.activate(language)
File "C:\jython2.7b3\Lib\site-packages\django\utils\translation\trans_real.py", line 188, in activate
_active.value = translation(language)
File "C:\jython2.7b3\Lib\site-packages\django\utils\translation\trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "C:\jython2.7b3\Lib\site-packages\django\utils\translation\trans_real.py", line 159, in _fetch
app = import_module(appname)
File "C:\jython2.7b3\Lib\site-packages\django\utils\importlib.py", line 40, in import_module
__import__(name)
File "C:\jython2.7b3\Lib\site-packages\django\contrib\admin\__init__.py", line 6, in <module>
from django.contrib.admin.sites import AdminSite, site
File "C:\jython2.7b3\Lib\site-packages\django\contrib\admin\sites.py", line 4, in <module>
from django.contrib.admin.forms import AdminAuthenticationForm
File "C:\jython2.7b3\Lib\site-packages\django\contrib\admin\forms.py", line 6, in <module>
from django.contrib.auth.forms import AuthenticationForm
File "C:\jython2.7b3\Lib\site-packages\django\contrib\auth\forms.py", line 17, in <module>
from django.contrib.auth.models import User
File "C:\jython2.7b3\Lib\site-packages\django\contrib\auth\models.py", line 47, in <module>
#python_2_unicode_compatible
File "C:\jython2.7b3\Lib\site-packages\django\db\models\base.py", line 96, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "C:\jython2.7b3\Lib\site-packages\django\db\models\base.py", line 264, in add_to_class
value.contribute_to_class(cls, name)
File "C:\jython2.7b3\Lib\site-packages\django\db\models\options.py", line 124, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\jython2.7b3\Lib\site-packages\django\db\__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\jython2.7b3\Lib\site-packages\django\db\utils.py", line 198, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\jython2.7b3\Lib\site-packages\django\db\utils.py", line 113, in load_backend
return import_module('%s.base' % backend_name)
File "C:\jython2.7b3\Lib\site-packages\django\db\utils.py", line 113, in load_backend
return import_module('%s.base' % backend_name)
File "C:\jython2.7b3\Lib\site-packages\django\utils\importlib.py", line 40, in import_module
__import__(name)
File "C:\jython2.7b3\Lib\site-packages\django\db\backends\mysql\base.py", line 17, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
i research and found that is the connector of mysql but i don´t know why this does´t work
if anyone has a project in jython on django i will be so much gratefull this is too hard

Although MySQLdb is the most widely used MySql library among the Python/Django community, you can't use it because Jython does not work with C extensions.
To use Jython with Django, you need to install django-jython and set the db backend to 'ENGINE': 'doj.db.backends.mysql'
This is documented in the official doc Running Django on Jython

Install pymysql with "pip install pymysql"
Add the following to your manage.py file:
import pymysql
pymysql.install_as_MySQLdb()
In your DATABASES setting, use:
ENGINE': 'django.db.backends.mysql',

Related

I created a virtual environmnt for my new projec, instaled django and started the new project. whnvr I run code with manage.py i get this long error

`when i run line of code: python manage.py startapp blog error below displays
Traceback (most recent call last):
File "C:\Users\lenevo\Desktop\portfolio-project\manage.py", line 22, in <module>
main()
File "C:\Users\lenevo\Desktop\portfolio-project\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\lenevo\Desktop\reemaenv\Lib\site-packages\django\core\management_init_.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\lenevo\Desktop\reemaenv\Lib\site-packages\django\core\management_init_.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\lenevo\Desktop\reemaenv\Lib\site-packages\django\core\management\base.py", line 336, in run_from_argv
connections.close_all()
File "C:\Users\lenevo\Desktop\reemaenv\Lib\site-packages\djayour textngo\db\utils.py", line 224, in close_all
connection.close()
File "C:\Users\lenevo\Desktop\reemaenv\Lib\site-packages\django\db\backends\sqlite3\base.py", line 248, in close
if not self.is_in_memory_db():
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lenevo\Desktop\reemaenv\Lib\site-packages\django\db\backends\sqlite3\base.py", line 367, in is_in_memory_db
return self.creation.is_in_memory_db(self.settings_dict['NAME'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lenevo\Desktop\reemaenv\Lib\site-packages\django\db\backends\sqlite3\creation.py", line 12, in is_in_memory_db
return database name == ':memory:' or 'mode=memory' in database name
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Type Error: argument of type 'Windows Path' is not Iterable
plz help me solve this problem `

ModuleNotFoundError: No module named 'google.cloud.location'

We have an application that uses firestore. It is running on Python 3.x on standard Google app engine. The application was running fine, till this morning when we tried to run the new version. The deployment of the new version is executed without any error. The requirement.txt is as follows -
firebase_admin==3.0.0
sendgrid==6.9.3
google-auth==1.35.0
google-auth-httplib2==0.1.0
jinja2==3.0.3
MarkupSafe==2.0.1
pytz==2021.3
Flask==2.0.2
twilio==6.46.0
httplib2==0.20.2
requests==2.24.0
requests_toolbelt==0.9.1
google-cloud-tasks==2.7.1
google-cloud-logging==1.15.1
googleapis-common-protos==1.54.0
protobuf==3.20.1
We get the following error.
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore_v1/services/firestore/client.py", line 55, in <module>
from google.cloud.location import locations_pb2 # type: ignore
ModuleNotFoundError: No module named 'google.cloud.location'
Please help.
Updated (9-24-2022) with the complete error output.
Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 92, in init_process
super().init_process()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/util.py", line 359, in import_app
mod = importlib.import_module(module)
File "/opt/python3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/srv/main.py", line 8, in <module>
from controllers import server, common, header
File "/srv/controllers/server.py", line 19, in <module>
from controllers import basehandler
File "/srv/controllers/basehandler.py", line 12, in <module>
from google.cloud import firestore
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore/__init__.py", line 18, in <module>
from google.cloud.firestore_v1 import __version__
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore_v1/__init__.py", line 36, in <module>
from google.cloud.firestore_v1.async_client import AsyncClient
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore_v1/async_client.py", line 45, in <module>
from google.cloud.firestore_v1.async_transaction import AsyncTransaction
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore_v1/async_transaction.py", line 49, in <module>
from google.cloud.firestore_v1.client import Client
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore_v1/client.py", line 44, in <module>
from google.cloud.firestore_v1.services.firestore import client as firestore_client
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore_v1/services/firestore/__init__.py", line 16, in <module>
from .client import FirestoreClient
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore_v1/services/firestore/client.py", line 55, in <module>
from google.cloud.location import locations_pb2 # type: ignore
ModuleNotFoundError: No module named 'google.cloud.location'
It looks like Google made a mistake in setting requirements for one its Python packages.
If you create a new virtual environment, install with pip install -r you should be able to recreate the error you are seeing in production.
To fix it, I would do pip list on your local machine (in the environment that is still working) and add fixed versions of other installed google packages until it works.
You can test by creating a new Python virtual environment, installing with pip install -r and seeing if it works.
It is a tedious process unfortunately.
I got this exact same error just now while trying to link a Python script to Firestore. I don't know if this is a "correct" solution but I got my code to run and save data to my Firestore database after this doing this.
In the error message, this line specifies the file where google.cloud.location is being imported:
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/cloud/firestore_v1/services/firestore/client.py", line 55, in <module>
If you open the client.py file and comment out the line 55 ("from google.cloud.location import locations_pb2 # type: ignore"), then run your code again, the next error message will show the other file where google.cloud.location is being imported. Repeat this process for all five files where this module is imported. (client.py, base.py, grpc.py, grpc_asyncio.py, async_client.py)
From searching through the code, I'm seeing that locations_pb2 (imported from google.cloud.location) is never used. I suspect that commenting this out entirely won't affect the functionality of firebase_admin.

django call_command syncdb failed: unable to open database file

I want to add some python codes after syncdb, so I decide to write a build.py which does everything including syncdb.
I write something in build.py as:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.management import call_command
from django.contrib.auth.models import User
call_command('syncdb', interactive=False)
But when I run build.py, it said:
Traceback (most recent call last):
File "/home/csimstu/PycharmProjects/TeenHope/TeenHope/build.py", line 5, in <module>
call_command('syncdb', interactive=False)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 161, in call_command
return klass.execute(*args, **defaults)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 255, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 385, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 56, in handle_noargs
cursor = connection.cursor()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 324, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 306, in _cursor
self._sqlite_create_connection()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 296, in _sqlite_create_connection
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
I've tried ./manage.py syncdb and use call_command in interactive shell mode, and both ways worked perfectly okay. How could it be?
If the database file isn't found that's probably because you didn't use a full path in your settings.
import os
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.realpath(os.path.join(os.path.dirname(__file__), "relative_path_here", "database.db")),
Out of topic: I'd recommend to use Fabric for this kind of commands.

Using SQL Server backend with Django

I'm trying to connect to an external SQL Server Express database using the django-pyodbc version at: this github link but I keep getting this error when I run manage.py shell to check if I can execute come ORM commands
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py",line 232, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py",line 371, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/shell.py", line 45, in handle_noargs
from django.db.models.loading import get_models
File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 93, in __getitem__
conn = backend.DatabaseWrapper(db, alias)
File "/usr/local/lib/python2.7/site-packages/sql_server/pyodbc/base.py", line 133, in __init__
self.ops = DatabaseOperations(self)
File "/usr/local/lib/python2.7/site-packages/sql_server/pyodbc/operations.py", line 10, in __init__
super(DatabaseOperations, self).__init__()
TypeError: __init__() takes exactly 2 arguments (1 given)
My settings.py has this:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'DBName',
'HOST': '192.168.29.1',
'USER': 'user',
'PASSWORD': 'pswd',
'PORT': 1433,
}
}
DATABASE_OPTIONS = {
'driver': '/usr/local/lib/libtdsodbc.so',
'dsn': 'FreeTDS',
'extra_params': "TDS_VERSION=8.0"
}
I'm completely at a loss as to what I'm supposed to do, so all ideas are welcome. Essentially what I want is to be able to use a small app I've made to interface with the SQL Server database, while being able to use normal DJango ORM commands inplace of pyodbc connection strings (and BTW using pyodbc, I can connect to the SQL Server database without an issue)
it seem to me that you are using the old repo for django-pyodbc at http://code.google.com/p/django-pyodbc/
make sure to cleanly uninstall the old version and then reinstall
pip uninstall sql-server.pyodbc
pip install git+https://github.com/avidal/django-pyodbc.git

South migration schemamigration doesn't work

Just to make sure I have the setup right in my current django project, I did the following:
added south to installed_apps
manage.py syncdb (using windows)
manage.py convert_to_south app
I didn't make changes to my database yet but whether or not I do, I run:
manage.py schemamigration app --auto
and I get the following errors:
Traceback (most recent call last):
File "manage.py", line 17, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 443, in execute
_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 196, in run_from_ar
gv
self.execute(*args, **options.__dict__)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 232, in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\south\management\commands\schemamigration.py", line 98, i
n handle
old_orm = last_migration.orm(),
File "C:\Python27\lib\site-packages\south\utils.py", line 62, in method
value = function(self)
File "C:\Python27\lib\site-packages\south\migration\base.py", line 418, in orm
return FakeORM(self.migration_class(), self.app_label())
File "C:\Python27\lib\site-packages\south\orm.py", line 46, in FakeORM
_orm_cache[args] = _FakeORM(*args)
File "C:\Python27\lib\site-packages\south\orm.py", line 125, in __init__
self.models[name] = self.make_model(app_label, model_name, data)
File "C:\Python27\lib\site-packages\south\orm.py", line 318, in make_model
field = self.eval_in_context(code, app, extra_imports)
File "C:\Python27\lib\site-packages\south\orm.py", line 236, in eval_in_context
return eval(code, globals(), fake_locals)
File "<string>", line 1
SouthFieldClass(default=datetime.datetime(2012, 9, 14, 20, 46, 3, 957000, tzinfo=<UTC>))
^
SyntaxError: invalid syntax
What's going on?
It's not letting me use schemamigration. Been searching a lot of places and deleting and resetting the database and everything, can't seem to find out what the problem is.
Problem resolved:
Didn't know about south_migrationhistory location. I was always wondering about where some extra information was hanging.
Have a look here: http://south.aeracode.org/ticket/1069
Its a bug that is fixed in newer South versions. It seems you are using an obsolete version of South(<=0.7.3)

Resources