为什么 Google Colab shell 命令不起作用?

Fra*_*kie 19 python locale google-colaboratory turi-create

重现步骤:

在 GPU 上打开新的 Colab 笔记本

!ls #works
!pip install -q turicreate
import turicreate as tc
!ls #doesn't work
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-22-16fdbe588ee8> in <module>()
----> 1 get_ipython().system('ls')
      2 # !nvcc --version

2 frames
/usr/local/lib/python3.6/dist-packages/google/colab/_system_commands.py in _run_command(cmd, clear_streamed_output)
    165   if locale_encoding != _ENCODING:
    166     raise NotImplementedError(
--> 167         'A UTF-8 locale is required. Got {}'.format(locale_encoding))
    168 
    169   parent_pty, child_pty = pty.openpty()

NotImplementedError: A UTF-8 locale is required. Got ANSI_X3.4-1968
Run Code Online (Sandbox Code Playgroud)

不幸的是,我对为什么会发生这种情况毫无意义。有线索吗?我还将作为 turicreate 项目中的潜在问题发布。

编辑:

它看起来确实像评论中所建议的那样覆盖了我的区域设置。在导入之前我可以这样做:

import locale
locale.getdefaultlocale()
(en_US, UTF-8)
Run Code Online (Sandbox Code Playgroud)

但在我得到之后:

locale.getdefaultlocale()
(None, None)
Run Code Online (Sandbox Code Playgroud)

虽然我现在不知道如何重置语言环境,因为我已经无法使用 shell 命令了?

Ste*_*ich 43

我从类似的问题中得到了不同的解决方法

首先检查当前区域设置:

import locale
print(locale.getpreferredencoding())
Run Code Online (Sandbox Code Playgroud)

解决方法是创建一个返回所需本地即的函数UTF-8

import locale
def getpreferredencoding(do_setlocale = True):
    return "UTF-8"
locale.getpreferredencoding = getpreferredencoding
Run Code Online (Sandbox Code Playgroud)

参考资料:
OS 模块 UTF 模式
Locale 模块


小智 19

遇到了同样的错误,在 colab 中运行以下代码对我有用

import locale
locale.getpreferredencoding = lambda: "UTF-8"
Run Code Online (Sandbox Code Playgroud)

来源:链接


San*_*eet 3

这是 Turicreate 的问题。相关问题在这里打开:https ://github.com/apple/turicreate/issues/1862

摘要是: 启动时turicreate 将 LC_ALL 环境变量设置为 C ()

解决此问题的方法:

import turicreate as tc
import os
del os.environ['LC_ALL']
Run Code Online (Sandbox Code Playgroud)