sys.path上的Unicode条目 [英] Unicode entries on sys.path

查看:96
本文介绍了sys.path上的Unicode条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图追踪py2exe中的一个错误,当可执行文件在包含日文字符的目录中时,它无法工作。


然后,我发现问题的一部分在于zipimporter

py2exe使用,最后我发现它甚至不能用Python工作

本身。 />

如果sys.path中的条目包含正常的西方字符,例如

的变音符号,它可以正常工作。但是当我从

a随机网页上复制了一些日文字符,并在此之后命名了一个目录时,它再也没有工作了




Windows命令提示符无法打印这些字符,

虽然Windows资源管理器显示它们没有问题。


这里'' s脚本,子目录包含文件''somemodule.py'',

但导入此失败:


import sys

sys.path = [u''\ u5b66 \ u6821 \ u30c7xx'']

print sys.path


import somemodule


似乎Python本身使用Windows默认转换规则将sys.path中的unicode条目转换为

普通字符串 - 这是一个

我可以通过更改机器上的某些区域设置来修复问题吗?


嗯,也许更多的是Windows问题而不是python问题...


谢谢,

托马斯

I was trying to track down a bug in py2exe where the executable did
not work when it is in a directory containing japanese characters.

Then, I discovered that part of the problem is in the zipimporter that
py2exe uses, and finally I found that it didn''t even work in Python
itself.

If the entry in sys.path contains normal western characters, umlauts for
example, it works fine. But when I copied some japanese characters from
a random web page, and named a directory after that, it didn''t work any
longer.

The windows command prompt is not able to print these characters,
although windows explorer has no problems showing them.

Here''s the script, the subdirectory contains the file ''somemodule.py'',
but importing this fails:

import sys
sys.path = [u''\u5b66\u6821\u30c7xx'']
print sys.path

import somemodule

It seems that Python itself converts unicode entries in sys.path to
normal strings using windows default conversion rules - is this a
problem that I can fix by changing some regional setting on my machine?

Hm, maybe more a windows question than a python question...

Thanks,
Thomas

解决方案

Thomas Heller写道:
Thomas Heller wrote:
似乎Python本身将sys.path中的unicode条目转换为使用Windows默认转换规则的普通字符串 - 是我可以通过更改机器上的某些区域设置来修复这个问题吗?


您可以在XP

区域设置的第三个选项卡上设置系统代码页(非unicode应用程序的字符集)。

当然,这假定在sys.path中有一个支持

所有目录的字符集。如果您在

sys.path上有日文字符,您当然需要将系统区域设置为日语

(是CP932吗?)。

更改此设置需要重新启动。

Hm,可能更多的是Windows问题而不是python问题......
It seems that Python itself converts unicode entries in sys.path to
normal strings using windows default conversion rules - is this a
problem that I can fix by changing some regional setting on my machine?
You can set the system code page on the third tab on the XP
regional settings (character set for non-unicode applications).
This, of course, assumes that there is a character set that supports
all directories in sys.path. If you have Japanese characters on
sys.path, you certainly need to set the system locale to Japanese
(is that CP932?).

Changing this setting requires a reboot.
Hm, maybe more a windows question than a python question...




这里真正的问题是:为什么Python不支持sys.path上的任意

Unicode字符串?原则上,它可以至少在Windows NT +(以及OSX上)上。欢迎补丁。


问候,

Martin



The real question here is: why does Python not support arbitrary
Unicode strings on sys.path? It could, in principle, atleast on
Windows NT+ (and also on OSX). Patches are welcome.

Regards,
Martin


文章< 41 *** ***********@v.loewis.de> ;,

" Martin v.Lowis" <毫安**** @ v.loewis.de>写道:
In article <41**************@v.loewis.de>,
"Martin v. Lowis" <ma****@v.loewis.de> wrote:
嗯,也许更多的是Windows问题而不是python问题...
Hm, maybe more a windows question than a python question...



真实这里的问题是:为什么Python不支持sys.path上的任意
Unicode字符串?它原则上至少可以在Windows NT +(以及OSX上)上使用。欢迎补丁。



The real question here is: why does Python not support arbitrary
Unicode strings on sys.path? It could, in principle, atleast on
Windows NT+ (and also on OSX). Patches are welcome.




在OSX 10.3.6上为我工作,因为它应该:在使用sys.path

条目之前,一个unicode字符串用Py_FileSystemDefaultEncoding编码。

我不知道它与zipimport一起工作得多好。


只是



Works for me on OSX 10.3.6, as it should: prior to using the sys.path
entry, a unicode string is encoded with Py_FileSystemDefaultEncoding.
I''m not sure how well it works together with zipimport, though.

Just


刚刚写道:
在文章< 41 ************** @ v.loewis.de>中,
Martin诉Lowis <毫安**** @ v.loewis.de>写道:

In article <41**************@v.loewis.de>,
"Martin v. Lowis" <ma****@v.loewis.de> wrote:

嗯,也许更像是一个Windows问题而不是一个蟒蛇问题...
真正的问题是:为什么Python不支持sys.path上的任意
Unicode字符串?它原则上至少可以在Windows NT +(以及OSX上)上使用。欢迎补丁。
Hm, maybe more a windows question than a python question...
The real question here is: why does Python not support arbitrary
Unicode strings on sys.path? It could, in principle, atleast on
Windows NT+ (and also on OSX). Patches are welcome.



在OSX 10.3.6上为我工作,因为它应该:在使用sys.path
条目之前,使用Py_FileSystemDefaultEncoding对unicode字符串进行编码。


Works for me on OSX 10.3.6, as it should: prior to using the sys.path
entry, a unicode string is encoded with Py_FileSystemDefaultEncoding.




对于此转换,mbcs将在Windows机器上使用,暗示

使用当前系统Ansi代码页进行此类转换。

(感兴趣的是:OSX上有什么?) 。对于包含字符

的unicode目录名,此转换可能对于此特定代码页中没有映射到该字符的内容非常无用。

Martin描述的技术可以解决这个

案例中日文字符的问题,但如果我有目录名,那么该怎么办?

另一种语言组,例如,简化中文,以及?
解决这个问题的唯一方法是允许 - 正如马丁建议的那样 -

sys中的任意unicode字符串。这些平台上的路径可能有

unicode文件名。


-

Vincen Wehren

我不确定它与zipimport一起工作得多好。

只需



For this conversion "mbcs" will be used on Windows machines, implying
that such conversions are made using the current system Ansi codepage.
(As a matter of interest: What is this on OSX?). This conversion is
likely to be useless for unicode directory names containing characters
that do not have a mapping to a character in this particular codepage.

The technique described by Martin may solve the problem for what in this
case are Japanese characters, but what if I have directory names from
another language group, such as simpliefied Chinese, as well?

The only way to get around this is to allow - as Martin suggests -
arbitrary unicode strings in sys.path on those platforms that may have
unicode file names.

--
Vincen Wehren
I''m not sure how well it works together with zipimport, though.
Just



这篇关于sys.path上的Unicode条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆