Django的语言环境文件安装在奇怪的地方

时间:2015-06-22 13:01:54

标签: python django pip virtualenv

我正在为我的组织创建一个新的环境设置脚本,但是在安装Django时遇到了问题。在virtualenv下执行private void InsertbuttonActionPerformed(java.awt.event.ActionEvent evt) { fileWriter = null; try { String phone = Phone.getText(); String fname = Fname.getText(); String lname = Lname.getText(); String nic = NIC.getText(); String city = City.getSelectedItem().toString(); fileWriter = new FileWriter(file, true); fileWriter.append(phone + "|" + fname + "|" + lname + "|" + nic + "|" + city+ "|"); fileWriter.append("\r\n"); fileWriter.flush(); JOptionPane.showMessageDialog(InsertGUI.this, "<html> " + phone + " <br> Successfully saved! </html>"); Phone.setText(null); NIC.setText(null); Fname.setText(null); Lname.setText(null); City.setSelectedIndex(0); } catch (IOException ex) { Logger.getLogger(InsertGUI.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fileWriter.close(); } catch (IOException ex) { Logger.getLogger(InsertGUI.class.getName()).log(Level.SEVERE, null, ex); } } } private void searchbuttonActionPerformed(java.awt.event.ActionEvent evt) { try { fileReader = new FileReader(file); BufferedReader br = new BufferedReader(fileReader); String selectedphone = SearchF.getText(); String content = ""; String temp = ""; try { while ((temp = br.readLine()) != null) { content += temp; } } catch (IOException ex) { Logger.getLogger(UpdateGUI.class.getName()).log(Level.SEVERE, null, ex); } HashMap<String, String> map = new HashMap(); StringTokenizer stringTokenizer = new StringTokenizer(content, "|"); boolean found = false; while (stringTokenizer.hasMoreTokens()) { String phone = stringTokenizer.nextElement().toString(); String fname = stringTokenizer.nextElement().toString(); String lname = stringTokenizer.nextElement().toString(); String nic = stringTokenizer.nextElement().toString(); String city = stringTokenizer.nextElement().toString(); if (phone.equalsIgnoreCase(selectedphone)) { Phone.setText(phone); NIC.setText(nic); Fname.setText(fname); Lname.setText(lname); switch (city) { case "Ambalangoda": City.setSelectedIndex(0); break; case "Ampara": City.setSelectedIndex(1); break; case "Anuradhapura": City.setSelectedIndex(2); break; case "Avissawella": City.setSelectedIndex(3); break; case "Badulla": City.setSelectedIndex(4); break; case "Balangoda": City.setSelectedIndex(5); break; } found = true; } } if (!found) { JOptionPane.showMessageDialog(UpdateGUI.this, "Phone number not found!"); Phone.setText(null); NIC.setText(null); Fname.setText(null); Lname.setText(null); City.setSelectedIndex(0); } } catch (FileNotFoundException ex) { Logger.getLogger(UpdateGUI.class.getName()).log(Level.SEVERE, null, ex); } } 后,需要本地化支持的测试产生:

private void UpdatebuttonActionPerformed(java.awt.event.ActionEvent evt) {

}
private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

}

潜入Django的pip install Django==1.4.19函数会发现错误来自于尝试在 ... 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' 下找到目录trans_real.translation(),但该目录不包含翻译文件或{{1}目录:

django

相反,.po和.mo文件可以在virtualenv的根目录下找到:

~/venvs/myenv/local/lib/python2.7/site-packages/django/conf/locale

通常,目录LC_MESSAGES不包含.po-或.mo文件,所有这些文件都可以在(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 下的相应子目录中找到。

Pip版本是7.0.3,如果我将它降级到版本6.1.1(我们之前一直在使用),翻译文件可以(正确?)在(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 下找到,一切正常。

什么可能导致这种新的pip行为?

更新 看来~/venvs/myenv/local/lib/python2.7/site-packages/django包也受此影响。它的.html文件(只有那些)安装在virtualenv的根目录下,其余文件位于~/venvs/myenv/django下:

site-packages/django

这些html文件和django的.po / .mo文件之间是否存在一些共同点?

2 个答案:

答案 0 :(得分:1)

不完全是一个优雅的解决方案,但是如果你只是需要让你的项目启动并运行并且降级pip不是一个选项,那么符号链接将会这样做:

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

答案 1 :(得分:1)

当我使用Tox old django 1.2项目进行测试时遇到了这个问题,我创建了以下自动解决它的函数:

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