Django's locale files installed in weird place

1k Views Asked by At

I'm creating a new environment setup script for my organization, but am having problems with installing Django. After executing pip install Django==1.4.19 under virtualenv a test that requires localization support yields:

  ...
  File "/home/ubuntu/venvs/myenv/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 105, in activate
    return _trans.activate(language)
  File "/home/ubuntu/venvs/myenv/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 194, in activate
    _active.value = translation(language)
  File "/home/ubuntu/venvs/meynv/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 184, in translation
    current_translation = _fetch(language, fallback=default_translation)
  File "/home/ubuntu/venvs/myenv/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 147, in _fetch
    res._info = res._info.copy()
AttributeError: 'NoneType' object has no attribute '_info'

Diving into Django's trans_real.translation() function reveals that the error comes from trying to find catalog django under ~/venvs/myenv/local/lib/python2.7/site-packages/django/conf/locale, however that directory contains no translation files or LC_MESSAGES directory:

(myenv)ubuntu@ubuntu:~$ ls ~/venvs/myenv/local/lib/python2.7/site-packages/django/conf/locale/en/
total 12
-rw-rw-r-- 1 ubuntu ubuntu 1637 Jun 22 13:51 formats.py
-rw-rw-r-- 1 ubuntu ubuntu 1094 Jun 22 13:51 formats.pyc
-rw-rw-r-- 1 ubuntu ubuntu    0 Jun 22 13:51 __init__.py
-rw-rw-r-- 1 ubuntu ubuntu  149 Jun 22 13:51 __init__.py

Instead, the .po and .mo files can be found under the virtualenv's root:

(myenv)ubuntu@ubuntu:~$ ls -l ~/venvs/myenv/django/conf/locale/en/LC_MESSAGES/
-rw-rw-r-- 1 ubuntu ubuntu   356 Jun 22 13:51 django.mo
-rw-rw-r-- 1 ubuntu ubuntu 21041 Jun 22 13:51 django.po

In general the directory ~/venvs/myenv/local/lib/python2.7/site-packages/django contains no .po- or .mo-files, and all of them can be found under ~/venvs/myenv/django in appropriate subdirectories.

Pip version is 7.0.3, and if I downgrade it to version 6.1.1 (which we have been using earlier), the translation files can be (properly?) found under site-packages/django and everything works.

What could cause this pecular behaviour with newer pip?

UPDATE It appears also djcelery package is affected by this. Its .html-files (and only those) are installed under virtualenv's root, while the rest is under site-packages/djcelery:

/home/ubuntu/venvs/myenv/djcelery/
+-- templates
    +-- admin
    |   +-- djcelery
    |       +-- change_list.html
    +-- djcelery
        +-- confirm_rate_limit.html

Is there some common denominator between these html-files and django's .po/.mo-files?

2

There are 2 best solutions below

0
On

Not exactly an elegant fix, but if you just need to get your project up and running and downgrading pip is not an option, a symlink will do it:

ln -s ~/.virtualenvs/<virtualenv>/django/conf/locale/en/LC_MESSAGES/ \
~/.virtualenvs/<virtualenv>/local/lib/python2.7/site-packages/django/conf/locale/en/
0
On

I had this problem when I was using testing with Tox old django 1.2 project, I created following function that automatically solves it:

import os

from distutils.sysconfig import get_python_lib
from distutils.dir_util import copy_tree


def resolve_pip_data_files(fix_packages=None):
    """
    :param fix_packages: list of packages to fix, defaults to Django

    """
    if fix_packages is None:
        fix_packages = ['django']

    env_path = os.environ.get('VIRTUAL_ENV', None)
    site_packages = get_python_lib()

    if not env_path:
        return  # not in virtual env

    for package in fix_packages:
        package_data_dir = os.path.join(env_path, package)
        package_dir = os.path.join(site_packages, package)

        if os.path.exists(package_data_dir) and os.path.exists(package_dir):
            copy_tree(package_data_dir, package_dir)

if __name__ == '__main__':
    resolve_pip_data_files()