six: cannot import name python_2_unicode_compatible - python

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.

Related

How to deal with staticfiles module in an old version of django?

I want to deploy my app developed with django 1.2.5 and I have a list of required packages, I have installed them and when I launch http://localhost:8000 I get the following error:
from ckeditor.widgets import CKEditorWidget
File "build\bdist.win32\egg\ckeditor\widgets.py", line 4, in <module>
ImportError: No module named staticfiles.templatetags.staticfiles
I have installed a package called: django-ckeditor 4.4.8 and it seems to be the root cause, I have seen the ckeditor\widgets.py file and trying to modify it but no chance (because of compilation in an egg file):
#ckeditor\widgets.py
from django.contrib.staticfiles.templatetags.staticfiles import static
...
try:
js += (
static('ckeditor/ckeditor/ckeditor.js'),
static('ckeditor/ckeditor-init.js'),
)
any workaround for this issue?
Firstly, you should really upgrade your app to a later version of Django. Django 1.2 is over 5 years old and has not received security updates for years.
It looks like the line that is causing you problems has been reverted in master. So you could try the previous version 4.4.7 or wait for the next release.

Cant Find HashLib Module After Server Update

I run Python Scripts on our Dreamhost Server. Our Python scripts use Python 2.7 - we made a custom installation because Dreamhost uses Python 2.6. Everything worked fine for 1 year.
Dreamhost did a server update yesturday and now our scripts fail to find a specific module - MD5. The scripts output the error below when we go to import hashlib.
What do I need to do to rectify this?
Should I reinstall Python 2.7?
Should I reinstall Pip and Easy_Install?
Should I reinstall VirtualEnv?
Is there something else you recommend I do?
Error from all Python scripts:
/home/user/script.py in ()
import hashlib
hashlib undefined
/home/user/python/lib/python2.7/hashlib.py in ()
# version not supporting that algorithm.
try:
globals()[__func_name] = __get_hash(__func_name)
except ValueError:
import logging builtin globals = <built-in function globals, __func_name = 'md5', __get_hash = <function __get_builtin_constructor /home/user/python/lib/python2.7/hashlib.py in __get_builtin_constructor(name='md5')
return _sha.new
elif name in ('MD5', 'md5'):
import _md5
return _md5.new
elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
_md5 undefined
<type 'exceptions.ImportError': No module named _md5
args = ('No module named _md5',)
message = 'No module named _md5'
I was having the exact same issue. I run Python 2.7 in my own virtualenv.
I am trying to avoid reinstalling python and run a Django 1.7 application.
The following approach works for me.
STEP 1. (This step might not be necessary)
I uninstalled pythonbrew since it says here: http://wiki.dreamhost.com/Python
that pythonbrew has been deprecated.
If you were doing this from scratch pyenv is way to go, but you don't need to reinstall
virtualenv, etc. Just get rid of pythonbrew to start with.
$ rm -Rf ~/.pythonbrew
Removed references in .bashrc to pythonbrew
STEP 2.
There is no need to re-install virtualenv. Just create a new virtual env
$~/env> virtualenv myNewEnvironment
$~/env/myNewEnvironment/bin> source activate
$ pip freeze
You have a clean slate now, start rebuilding dependencies from scratch.
At least is solves the "import hashlib" issue. This gives you a clean
version of python properly linked to the new Ubuntu OS.
(myNewEnvironment):~> which python
~/env/myNewEnvironment/bin/python
(myNewEnvironment):~> python
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
installed on Ubuntu 12.04 (which is the new OS)
Verify:
import hashlib should not throw error
STEP 3.
pip install Django
pip install MySQL-python
Its also probably safer to complete/recheck the remaining steps listed out in
http://wiki.dreamhost.com/Django (or appropriate wiki page for your framework)
For now this allows me to get my site up and running, (but) there is a warning
that I am ignoring for now until I figure out more:
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
Good luck!

GAE: remote_api_shell.py uses wrong Django version

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.

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.

Mercurial http interface under Ubuntu Hardy. Doesn't work

I am trying to deploy mercurial under Ubuntu 8.04.
Mercurial packages were installed correctly, but when I've configured http interface I always get 500 error.
I enabled outputting debug info to error.log and got:
mod_wsgi (pid=21159): Exception occurred within WSGI script
'/home/hg/rep/hgwebdir.wsgi'.
Traceback (most recent call last):
File "/home/hg/rep/hgwebdir.wsgi", line 67, in <module>
wsgicgi.launch(application)
File "/var/lib/python-support/python2.5/mercurial/hgweb/wsgicgi.py", line 64, in launch
result = application(environ, start_response)
TypeError: 'hgwebdir' object is not callable
My desktop is with Ubuntu 10.04, and home server with ubuntu 9.10, and configuration is the same, and works like a charm.
I compiled python 2.6, and in hgwebdir.wsgi put path to this library -
import sys
sys.path.insert(0, "/path/to/python/lib")
But it doesn't work anyways.
What shall I do??
Thanks.
Which version of mercurial are you using? If you're still using the 1.0.x that ubuntu ships update to the PPAs from launchpad: https://launchpad.net/~mercurial-ppa/+archive/stable-snapshots
In 1.6 hgwebdir has been renamed to just 'hgweb' which will alter your config slightly.
Also what are you using the launch the wsgi stuff? Apache?

Categories

Resources