condaでyamlから環境を再構築しようとしてエラーが出た時の対処法


起こった問題

conda env export > hoge.yml

を使って仮想環境に含まれているパッケージのリストをyamlファイルに書き出し、

conda env create -n envname -f hoge.yml

を実行して hoge.yml に書かれた仮想環境を再構築しようとしたところ、以下のエラーが出現して実行できませんでした。

# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

    Traceback (most recent call last):
      File "C:\Users\user\miniconda3\lib\site-packages\conda\exceptions.py", line 1079, in __call__
        return func(*args, **kwargs)
      File "C:\Users\user\miniconda3\lib\site-packages\conda_env\cli\main.py", line 80, in do_call
        exit_code = getattr(module, func_name)(args, parser)
      File "C:\Users\user\miniconda3\lib\site-packages\conda_env\cli\main_create.py", line 87, in execute
        spec = specs.detect(name=name, filename=filename, directory=os.getcwd())
      File "C:\Users\user\miniconda3\lib\site-packages\conda_env\specs\__init__.py", line 43, in detect
        if spec.can_handle():
      File "C:\Users\user\miniconda3\lib\site-packages\conda_env\specs\yaml_file.py", line 18, in can_handle
        self._environment = env.from_file(self.filename)
      File "C:\Users\user\miniconda3\lib\site-packages\conda_env\env.py", line 160, in from_file
        return from_yaml(yamlstr, filename=filename)
      File "C:\Users\user\miniconda3\lib\site-packages\conda_env\env.py", line 141, in from_yaml
        data = yaml_safe_load(yamlstr)
      File "C:\Users\user\miniconda3\lib\site-packages\conda\common\serialize.py", line 67, in yaml_safe_load
        return yaml.safe_load(string, version="1.2")
      File "C:\Users\user\miniconda3\lib\site-packages\ruamel_yaml\main.py", line 980, in safe_load
        return load(stream, SafeLoader, version)
      File "C:\Users\user\miniconda3\lib\site-packages\ruamel_yaml\main.py", line 933, in load
        loader = Loader(stream, version, preserve_quotes=preserve_quotes)
      File "C:\Users\user\miniconda3\lib\site-packages\ruamel_yaml\loader.py", line 39, in __init__
        Reader.__init__(self, stream, loader=self)
      File "C:\Users\user\miniconda3\lib\site-packages\ruamel_yaml\reader.py", line 85, in __init__
        self.stream = stream  # type: Any  # as .read is called
      File "C:\Users\user\miniconda3\lib\site-packages\ruamel_yaml\reader.py", line 117, in stream
        self.check_printable(val)
      File "C:\Users\user\miniconda3\lib\site-packages\ruamel_yaml\reader.py", line 250, in check_printable
        raise ReaderError(
    ruamel_yaml.reader.ReaderError: unacceptable character #x0000: special characters are not allowed
      in "<unicode string>", position 3

`$ C:\Users\user\miniconda3\Scripts\conda-env-script.py create -n test2 -f hoge.yml`

  environment variables:
                 CIO_TEST=<not set>
  CONDA_AUTO_UPDATE_CONDA=false
        CONDA_DEFAULT_ENV=default
                CONDA_EXE=C:\Users\user\miniconda3\Scripts\conda.exe
             CONDA_PREFIX=C:\Users\user\miniconda3\envs\default
           CONDA_PREFIX_1=C:\Users\user\miniconda3
    CONDA_PROMPT_MODIFIER=(default)
         CONDA_PYTHON_EXE=C:\Users\user\miniconda3\python.exe
               CONDA_ROOT=C:\Users\user\miniconda3
              CONDA_SHLVL=2
           CURL_CA_BUNDLE=<not set>
                 HOMEPATH=\Users\user
      MIC_LD_LIBRARY_PATH=C:\Program Files (x86)\Common Files\Intel\Shared
                          Libraries\compiler\lib\mic
                     PATH=C:\Users\user\miniconda3;C:\Users\user\miniconda3\Library\mingw-w64\bi
                          n;C:\Users\user\miniconda3\Library\usr\bin;C:\Users\user\miniconda3\Li
                          brary\bin;C:\Users\user\miniconda3\Scripts;C:\Users\user\miniconda3\bi
                          n;C:\Users\user\miniconda3\envs\default;C:\Users\user\miniconda3\envs\
                          default\Library\mingw-w64\bin;C:\Users\user\miniconda3\envs\default\Li
                          brary\usr\bin;C:\Users\user\miniconda3\envs\default\Library\bin;C:\Use
                          rs\user\miniconda3\envs\default\Scripts;C:\Users\user\miniconda3\envs\
                          default\bin;C:\Users\user\miniconda3\condabin;C:\Program Files
                          (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\W
                          INDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System3
                          2\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program
                          Files\Java\jdk-12.0.1\bin;C:\Program Files\Docker\Docker\resources\bin
                          ;C:\ProgramData\DockerDesktop\version-bin;C:\Users\user\AppData\Local\
                          Programs\Python\Python37\Scripts;C:\Users\user\AppData\Local\Programs\
                          Python\Python37;C:\Users\user\.pyenv\pyenv-
                          win\bin;C:\Users\user\.pyenv\pyenv-win\shims;C:\Program Files
                          (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\W
                          INDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System3
                          2\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program
                          Files\Java\jdk-12.0.1\bin;C:\Program Files\Docker\Docker\resources\bin
                          ;C:\ProgramData\DockerDesktop\version-bin;C:\Users\user\scoop\shims;C:
                          \Users\user\AppData\Local\Microsoft\WindowsApps;C:\Program Files
                          (x86)\FAHClient;C:\Program Files\JetBrains\PyCharm Community Edition
                          2020.1.3\bin;C:\Users\user\AppData\Local\Programs\Microsoft VS
                          Code\bin;C:\Program Files (x86)\GitHub CLI;C:\Users\user\AppData\Local
                          \Programs\EmEditor;C:\Users\user\miniconda3;C:\Users\user\miniconda3\S
                          cripts;C:\Users\user\miniconda3\Library\bin;.
             PSMODULEPATH=C:\Users\user\scoop\modules;C:\Users\user\Documents\WindowsPowerShell\
                          Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32
                          \WindowsPowerShell\v1.0\Modules
               PYTHONPATH=C:\Users\user\programming\tools\modules\python;C:\Users\user\programmi
                          ng\tools\scripts\python;
       REQUESTS_CA_BUNDLE=<not set>
            SSL_CERT_FILE=<not set>

     active environment : default
    active env location : C:\Users\user\miniconda3\envs\default
            shell level : 2
       user config file : C:\Users\user\.condarc
 populated config files : C:\Users\user\.condarc
          conda version : 4.9.2
    conda-build version : not installed
         python version : 3.8.5.final.0
       virtual packages : __win=0=0
                          __archspec=1=x86_64
       base environment : C:\Users\user\miniconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
                          https://conda.anaconda.org/conda-forge/win-64
                          https://conda.anaconda.org/conda-forge/noarch
          package cache : C:\Users\user\miniconda3\pkgs
                          C:\Users\user\.conda\pkgs
                          C:\Users\user\AppData\Local\conda\conda\pkgs
       envs directories : C:\Users\user\miniconda3\envs
                          C:\Users\user\.conda\envs
                          C:\Users\user\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/4.9.2 requests/2.24.0 CPython/3.8.5 Windows/10 Windows/10.0.18362
          administrator : False
             netrc file : None
           offline mode : False


An unexpected error has occurred. Conda has prepared the above report.

If submitted, this report will be used by core maintainers to improve
future releases of conda.
Would you like conda to send this report to the core maintainers?

[y/N]:

これの解決策を見つけたのでメモしておきます。

解決方法

このページ の "AFTownsend commented on 15 May 2019" のコメントを参考に
VSCodeでhoge.ymlのエンコーディングを見てみると、UTF-16 LE になってました。

そこで、エンコーディングの所をクリックしてUTF-8に変更して保存しなおしたら、
問題なく

conda env create -n envname -f hoge.yml

を使って仮想環境を再構築できるようになりました。

参考ページ

エラーの解決法

yamlからの環境構築