GAE: remote_api_shell.py uses wrong Django version - python

I have a Python 2.7 GAE app that uses Django 1.2. Everything generally works fine.
I'm trying to use remote_api_shell.py, but when I import my models.py, I get this error:
$ PYTHONPATH=.; remote_api_shell.py APP
App Engine remote_api shell
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
The db, users, urlfetch, and memcache modules are imported.
s~APP> import models
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/.../models.py", line 8, in <module>
from django.core.validators import email_re
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django_0_96/django/core/validators.py", line 13, in <module>
from django.utils.translation import gettext, gettext_lazy, ngettext
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django_0_96/django/utils/translation/__init__.py", line 3, in <module>
if settings.USE_I18N:
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django_0_96/django/conf/__init__.py", line 28, in __getattr__
self._import_settings()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django_0_96/django/conf/__init__.py", line 53, in _import_settings
raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.
I don't know why it is using Django 0.96 here. Especially since I think Python 2.7 supports only Django 1.2. Here are the relevant parts of my app.yaml:
application: APP
version: prod
runtime: python27
api_version: 1
threadsafe: true
[...]
libraries:
- name: django
version: "1.2"
If I comment out the from django.core.validators import email_re line then the import succeeds.
Any idea if I am doing something wrong or if this is this a GAE bug?

I had the same issue, and I solved it by running these two lines once I open the remote API session:
from google.appengine.dist import use_library
use_library('django', '1.3')
Though, clearly, it's a hack. Star this issue to encourage the app engine team to fix it:
https://code.google.com/p/googleappengine/issues/detail?id=8716

try typing this:
import django
django.VERSION
To check the django version! I am guessing that the python console that u are running has the old version of django!

FYI this no longer works because
use_library('django', '1.5')
now throws this error:
ValueError: 1.5 is not a supported version for django; supported versions are ['0.96', '1.0', '1.1', '1.2', '1.3']
Hopefully this is a temporary problem, but my solution was to delete (rename):
C:\Program Files (x86)\Google\google_appengine\lib\django-0.96
to something else, then the proper version of django loads w/o any other problems.

Related

six: cannot import name python_2_unicode_compatible

With six 1.10.0 installed under Python and pip 2.6, an old Django 1.0.4 app is not able to import python_2_unicode_compatible even though it finds six 1.10.0 just fine:
>>> import six
>>> six.__version__
'1.10.0'
>>> from six import python_2_unicode_compatible
>>>
I've confirmed with python code within the app that it does indeed have access to six:
['appdirs==1.4.3', 'argparse==1.4.0', 'astkit==0.5.4', 'beautifulsoup==3.2.1',
'coverage==4.3.4', 'django-cms==2.0.0a0', 'django==1.0.4', 'dnspython==1.12.0',
'flup==1.0.2', 'importlib==1.0.4', 'iniparse==0.3.1', 'instrumental==0.5.3',
'mako==1.0.6', 'markupsafe==1.0', 'minimock==1.2.8', 'mysql-python==1.2.5',
'nose==1.3.7', 'packaging==16.8', 'pillow==3.4.2', 'pip==9.0.1', 'pluggy==0.4.0',
'py==1.4.33', 'pyparsing==2.2.0', 'python-dateutil==2.6.0', 'pyzor==1.0.0',
'setuptools==35.0.1', 'six==1.10.0', 'sorl-thumbnail==12.3', 'tox==2.7.0',
'uwsgi==2.0.15','virtualenv==15.1.0', 'wheel==0.29.0']
I am tasked to move a very old site that was running django 1.0.4 (you read that right, 1.0.4) and django_cms 2.0.0 Alpha to a new server. The old server croaked, so all I have is the backup of the main website files and dependencies that were installed long ago.
I am Dockerizing it to help document and deploy this in the future.
Ubuntu 14.04
Python 2.6 (same results with 2.7)
Django 1.0.4 (installed via local zip)
django_cms 2.0.0a0 (installed via local zip)
I have tried Apache mod_wsgi, gunicorn (pip2.6 installed) and currently using uwsgi (preferred, pip2.6 installed) to load the app.
Nginx is running in another Docker container with proxy_pass, and will the frontend proxy and TLS.
uwsgi starts the site up with the custom wsgi.
Upon loading the / index page, I had many import errors. Slowly, I am resolving each and every one of them (mostly related to the Django "MIDDLEWARE_CLASSES", which I have yet to find their definition).
I am currently stuck on the following error:
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 230, in __call__
self.load_middleware()
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 41, in load_middleware
raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e)
django.core.exceptions.ImproperlyConfigured: Error importing middleware cms.middleware.user: "cannot import name python_2_unicode_compatible"
uwsgi starts up with the specified python2.6 just fine:
web_1 | [uWSGI] getting INI configuration from uwsgi.ini
web_1 | *** Starting uWSGI 2.0.15 (64bit) on [Wed Apr 26 16:27:43 2017] ***
web_1 | Python version: 2.6.9 (default, Oct 22 2014, 19:53:49) [GCC 4.8.2]
web_1 | Python main interpreter initialized at 0xef1050
web_1 | python threads support enabled
Also, python2.7 was originally configured and had the exact same error. I thought I read where python_2_unicode_compatible was deprecated in 2.7 or something, so I went back to the original version the site was running under.
Do I need to install virtualenv? I don't usually do it under Docker, and just install everything globally. I can't see how that would make a difference.
If six was not found, wouldn't I get an error that it could not import six, instead of python_2_unicode_compatible?
The python_2_unicode_compatible method was originally in Django, then added to six in 1.9.
One of your installed packages may be trying to import python_2_unicode_compatible from django.utils.encoding, rather than from the six package.

Getting to interactive Django shell in PyDev

I can open an interactive shell from the command line, just not from PyDev inside Eclipse. Clicking through Django --> Shell with django environment I get the following output:
import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
C:\Python27\python.exe 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
PyDev console: using default backend (IPython not available).
from django.core import management;import carbon_factors.settings as settings;management.setup_environ(settings)
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'setup_environ'
To try and resolve this I have updated PyDev to 3.2.0 with no change to the output. I'm using Django 1.6.
I have looked at the comment here which pointed me here. However the accepted answer seems to have already be built into Django/PyDev as the line it suggests to change in manage.py is already changed.
setup_environ has been deprecated in Django 1.6. See here.
As far as I can tell, PyDev has hardcoded this method of starting a Django shell into the right click project -> "shell with Django environment" menu item. This is an issue that PyDev needs to address (hopefully by simplifying whatever they are doing under the hood with that command to just "python manage.py shell"!) I don't believe their menu command currently calls manage.py, which is why the other answers didn't work for you.
Option 1
As a hacky workaround, add "from django.conf import settings; settings.configure()" to the initial interpreter commands in Window->Preferences->PyDev->Interactive Console. You'll still see the error but at least you'll have your settings.
Option 2
In Window->Preferences->Interpreter->Environment tab, add an environment variable "DJANGO_SETTINGS_MODULE" with the Python dotted path to your settings file (exclude the '.py' at the end).
For the sake of having an up-to-date answer, this works fine for me with Django 1.8 and Eclipse Luna.
For Django 1.10 and Eclipse Neon.
Following Option 1 from andy.
My settings file is inside myproject/config/settings/local.py.
Go to: Window->Preferences->PyDev->Interactive Console->Initial Commands
and replace the first line with:
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings.local'; import django
if '1.10' in django.get_version():
django.setup()

Upgrade python to 2.7 and Django to 1.4, but it tells me No module named *

I have just upgraded my python to 2.7 on my Ubuntu server, and reinstalled the Django to 1.4. I use python flup to run the Django application as fastcgi, but when I try to restart the application, it keeps telling me No module named *
e.g.
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
...
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
ImportError: No module named *
The issue was initiated by manage.py file in the old Django (1.3) project, but according to the document https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py
that's how I run the project using python-flup
exec python ./manage.py runfcgi host=127.0.0.1 port=8083 maxchildren=40 pidfile=$PIDFILE --settings=expand.settings
The old-style manage.py will continue to work as before until Django 1.6. In 1.5 it will raise DeprecationWarning
I'm guesing by looking at django's source code that there's something wrong with either the layout of your project or your python path.
execute_from_command_line(sys.argv)
Runs the following snippet (from django/core/management/init.py), which is most likely the cause of your error:
# Import the project module. We add the parent directory to PYTHONPATH to
# avoid some of the path errors new users can have.
sys.path.append(os.path.join(project_directory, os.pardir))
import_module(project_name)
sys.path.pop()

Setting up my remote api

My app needs a backup system and remote administration so I disabled the federated login for easier remote_api access. Now I can log in but I can't import my module:
montao$ python ./remote_api_shell.py -s montaoproject.appspot.com
Email: niklasro
Password:
App Engine remote_api shell
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2]
The db, users, urlfetch, and memcache modules are imported.
s~montaoproject> import i18n
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named i18n
s~montaoproject>
Could you tell me how to run some simple remote api commands? I could run the backup system and connect via /_ah/remote.api with python 2.7 so my setup seems correct and I might just need clearer understanding how to use the remote_api.
Update
This works but it seems to use the django 0.96:
ubuntu#ubuntu:/media/Lexar/montao$ PYTHONPATH=./montaoproject python ./remote_api_shell.py -s montaoproject.appspot.com
App Engine remote_api shell
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2]
The db, users, urlfetch, and memcache modules are imported.
s~montaoproject> import i18n
WARNING:root:You are using the default Django version (0.96). The default Django version will change in an App Engine release in the near future. Please call use_library() to explicitly select a Django version. For more information see http://code.google.com/appengine/docs/python/tools/libraries.html#Django
s~montaoproject>
Prefix your command with PYTHONPATH=. (or replace . with the directory your app is in). Without telling Python where it can find modules, it doesn't know where to look, and the current directory isn't part of the path by default.

Django running wrong version

Although running "python" from the shell runs Python v2.7, Django is loading files for python2.4, as shown in the error when I load a django site:
Mod_python error: "PythonHandler django.core.handlers.modpython"
Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 287, in HandlerDispatch
log=debug)
File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 461, in import_module
f, p, d = imp.find_module(parts[i], path)
ImportError: No module named django
I think Django is installed for version 2.7 and that's why the bottom says "No module named django"
This is my first django install (it's on a mediatemple DV server) so I wouldn't be surprised if I'm doing something stupid. Thanks!
mod_python is built for 2.4, but Django is installed for 2.7. Either build mod_python for 2.7, install Django under 2.4, or put a local copy of Django with your project so that the version of Python doesn't matter.

Categories

Resources