📚

【Anaconda】仮想環境をコピーして再構築しよう

2025/01/03に公開

Anaconda を使用して Python のパッケージを管理しています。

最近、PC を変えたのですが、新しい PC でも同じように Anaconda を使用したい!

そこで、ほかの PC へ移行する方法を紹介します

設定手順

  1. コピーしたい仮想環境に切り替える
  2. パッケージ情報を .yml or .txt に出力する
  3. 新しい PC で 2. のファイルをもとに仮想環境を作成する

今回はWindows PowerShell を使用して、作業していきます

コピーしたい仮想環境に切り替える

conda activate 「コピーしたい仮想環境名」

で仮想環境を切り替え、

conda info -e

で確認します

(実行例)

// base → python-learning へ切り替える例

//現在使用している仮想環境を確認
// * がついているbaseという環境を使用中
(base) PS C:\Users> conda info -e
# conda environments:
#
base                  *  C:\Users\******\anaconda3
django-start             C:\Users\******\anaconda3\envs\django-start
django_blog              C:\Users\******\anaconda3\envs\django_blog
python-learning          C:\Users\******\anaconda3\envs\python-learning

// python-learning という仮想環境に切り替える
(base) PS C:\Users> conda activate python-learning

//現在使用している仮想環境を確認すると
// * がpython-learningという環境につく
(python-learning) PS C:\Users> conda info -e
# conda environments:
#
base                     C:\Users\******\anaconda3
django-start             C:\Users\******\anaconda3\envs\django-start
django_blog              C:\Users\******\anaconda3\envs\django_blog
python-learning       *  C:\Users\******\anaconda3\envs\python-learning

(python-learning) PS C:\Users>

パッケージ情報を .yml or .txt に出力する

出力するディレクトリに cd コマンドで移動して、

.ymlで出力する場合) conda env export > 「ファイル名」.yml

.txtで出力する場合) conda list --export >「ファイル名」.txt もしくは conda list --explicit > 「ファイル名」.txt

を実行します

(実行例)

// 今回はデスクトップに移動
(python-learning) PS C:\Users> cd .\******\Desktop\

// デスクトップにpython-learning.yml を出力
(python-learning) PS C:\Users\******\Desktop> conda env export > python-learning.yml
// デスクトップにpython-learning.txt を出力
(python-learning) PS C:\Users\******\Desktop> conda list --export > python-learning.txt

//ちなみに 「 > 」以降をつけず実行すると、パッケージ情報を確認できます
// .ymlの中身
(python-learning) PS C:\Users\******\Desktop>  conda env export
name: python-learning
channels:
  - defaults
dependencies:
  - autopep8=1.6.0=pyhd3eb1b0_1
  - ca-certificates=2022.07.19=haa95532_0
  - certifi=2022.6.15=py39haa95532_0
  - openssl=1.1.1q=h2bbff1b_0
  - pip=21.2.4=py39haa95532_0
  - python=3.9.12=h6244533_0
  - setuptools=61.2.0=py39haa95532_0
  - sqlite=3.38.3=h2bbff1b_0
  - toml=0.10.2=pyhd3eb1b0_0
  - tzdata=2022a=hda174b7_0
  - vc=14.2=h21ff451_1
  - vs2015_runtime=14.27.29016=h5e58377_2
  - wheel=0.37.1=pyhd3eb1b0_0
  - wincertstore=0.2=py39haa95532_2
  - pip:
    - lazy-object-proxy==1.7.1
    - pycodestyle==2.9.0
    - pyflakes==2.5.0
    - tomli==2.0.1
    - tomlkit==0.11.1
    - typing-extensions==4.3.0
    - wrapt==1.14.1
prefix: C:\Users\******\anaconda3\envs\python-learning
// .txtの中身
(python-learning) PS C:\Users\******\Desktop> conda list --export
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: win-64
autopep8=1.6.0=pyhd3eb1b0_1
ca-certificates=2022.07.19=haa95532_0
certifi=2022.6.15=py39haa95532_0
lazy-object-proxy=1.7.1=pypi_0
openssl=1.1.1q=h2bbff1b_0
pip=21.2.4=py39haa95532_0
pycodestyle=2.9.0=pypi_0
pyflakes=2.5.0=pypi_0
python=3.9.12=h6244533_0
setuptools=61.2.0=py39haa95532_0
sqlite=3.38.3=h2bbff1b_0
toml=0.10.2=pyhd3eb1b0_0
tomli=2.0.1=pypi_0
tomlkit=0.11.1=pypi_0
typing-extensions=4.3.0=pypi_0
tzdata=2022a=hda174b7_0
vc=14.2=h21ff451_1
vs2015_runtime=14.27.29016=h5e58377_2
wheel=0.37.1=pyhd3eb1b0_0
wincertstore=0.2=py39haa95532_2
wrapt=1.14.1=pypi_0

新しい PC で 2. のファイルをもとに仮想環境を作成する

(.yml をもとに仮想環境を作成) conda env create -n 新たな環境名 -f ファイル名.yml

(.txt をもとに仮想環境を作成) conda create -n 環境名 -f ファイル名.txt※なんかうまくいかなかったです

.yml をもとに仮想環境を作成してみたらエラーが出ました

(エラー内容)

Pip subprocess error:
ERROR: Could not install packages due to an OSError: [WinError 5] アクセスが拒否されました。: 'C:\\Users\\****\\AppData\\Local\\Temp\\pip-uninstall-ykqgnrqg\\pycodestyle.exe'
Consider using the `--user` option or check the permissions.


failed

CondaEnvException: Pip failed

check the permissions.

と出ているので、権限系のエラーっぽいです。

なので、PowerShell を管理者権限で実行してみます

うーん、先ほどと同じエラーが出ます...

もう少しエラーを見てみます

(エラー内容)

  Attempting uninstall: pycodestyle
    Found existing installation: pycodestyle 2.8.0
    Uninstalling pycodestyle-2.8.0:
      Successfully uninstalled pycodestyle-2.8.0

Pip subprocess error:
ERROR: Could not install packages due to an OSError: [WinError 5] アクセスが拒否されました。: 'C:\\Users\\******\\AppData\\Local\\Temp\\pip-uninstall-ykqgnrqg\\pycodestyle.exe'
Consider using the `--user` option or check the permissions.


failed

CondaEnvException: Pip failed

pycodestyle のインストールに失敗してるっぽい

2.8.0 をアンインストール →2.9.0 をインストールする過程で失敗している

バージョンにこだわりはないので、yml 内の pycodestyle のバージョンを 2.8.0 に変更してみます

※ここからは管理者権限でなくても OK!

//変更前
- pycodestyle==2.9.0

//変更後
- pycodestyle==2.8.0

まず、仮想環境が中途半端にできているので、消します

// さっき作った仮想環境が中途半端にできているので削除する
(base) PS C:\Users\******\Desktop> conda info -e
# conda environments:
#
base                  *  C:\Users\******\anaconda3
django-start             C:\Users\******\anaconda3\envs\django-start
django_blog              C:\Users\******\anaconda3\envs\django_blog
python-learning          C:\Users\******\anaconda3\envs\python-learning
python-learning-from-yml     C:\Users\******\anaconda3\envs\python-learning-from-yml

(base) PS C:\Users\******\Desktop> conda remove -n python-learning-from-yml --all

Remove all packages in environment C:\Users\******\anaconda3\envs\python-learning-from-yml:


## Package Plan ##

  environment location: C:\Users\******\anaconda3\envs\python-learning-from-yml


The following packages will be REMOVED:

  autopep8-1.6.0-pyhd3eb1b0_1
  ca-certificates-2022.07.19-haa95532_0
  certifi-2022.6.15-py39haa95532_0
  openssl-1.1.1q-h2bbff1b_0
  pip-21.2.4-py39haa95532_0
  pycodestyle-2.8.0-pyhd3eb1b0_0
  python-3.9.12-h6244533_0
  setuptools-61.2.0-py39haa95532_0
  sqlite-3.38.3-h2bbff1b_0
  toml-0.10.2-pyhd3eb1b0_0
  tzdata-2022a-hda174b7_0
  vc-14.2-h21ff451_1
  vs2015_runtime-14.27.29016-h5e58377_2
  wheel-0.37.1-pyhd3eb1b0_0
  wincertstore-0.2-py39haa95532_2


Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
//削除確認
// python-learning-from-yml がないことを確認
(base) PS C:\Users\******\Desktop> conda info -e
# conda environments:
#
base                  *  C:\Users\******\anaconda3
django-start             C:\Users\******\anaconda3\envs\django-start
django_blog              C:\Users\******\anaconda3\envs\django_blog
python-learning          C:\Users\******\anaconda3\envs\python-learning

再作成します

(base) PS C:\Users\******\Desktop> conda env create -n python-learning-from-yml -f python-learning.yml
Collecting package metadata (repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.14.0
  latest version: 22.11.1

Please update conda by running

    $ conda update -n base -c defaults conda


Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Installing pip dependencies: - Ran pip subprocess with arguments:
['C:\\Users\\******\\anaconda3\\envs\\python-learning-from-yml\\python.exe', '-m', 'pip', 'install', '-U', '-r', 'C:\\Users\\******\\Desktop\\condaenv.xmygcpnb.requirements.txt']
Pip subprocess output:
Collecting lazy-object-proxy==1.7.1
  Using cached lazy_object_proxy-1.7.1-cp39-cp39-win_amd64.whl (22 kB)
Requirement already satisfied: pycodestyle==2.8.0 in c:\users\******\anaconda3\envs\python-learning-from-yml\lib\site-packages (from -r C:\Users\******\Desktop\condaenv.xmygcpnb.requirements.txt (line 2)) (2.8.0)
Collecting pyflakes==2.5.0
  Using cached pyflakes-2.5.0-py2.py3-none-any.whl (66 kB)
Collecting tomli==2.0.1
  Using cached tomli-2.0.1-py3-none-any.whl (12 kB)
Collecting tomlkit==0.11.1
  Using cached tomlkit-0.11.1-py3-none-any.whl (34 kB)
Collecting typing-extensions==4.3.0
  Using cached typing_extensions-4.3.0-py3-none-any.whl (25 kB)
Collecting wrapt==1.14.1
  Using cached wrapt-1.14.1-cp39-cp39-win_amd64.whl (35 kB)
Installing collected packages: wrapt, typing-extensions, tomlkit, tomli, pyflakes, lazy-object-proxy
Successfully installed lazy-object-proxy-1.7.1 pyflakes-2.5.0 tomli-2.0.1 tomlkit-0.11.1 typing-extensions-4.3.0 wrapt-1.14.1

done
#
# To activate this environment, use
#
#     $ conda activate python-learning-from-yml
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Retrieving notices: ...working... done

ちゃんと作成できたみたいです

// python-learning-from-yml がある
(python-learning) PS C:\Users\******\Desktop> conda info -e
# conda environments:
#
base                     C:\Users\******\anaconda3
django-start             C:\Users\******\anaconda3\envs\django-start
django_blog              C:\Users\******\anaconda3\envs\django_blog
python-learning       *  C:\Users\******\anaconda3\envs\python-learning
python-learning-from-yml     C:\Users\******\anaconda3\envs\python-learning-from-yml
// python-learning-from-yml に切り替える
(python-learning) PS C:\Users\******\Desktop> conda activate python-learning-from-yml
// インストールされたパッケージを確認
(python-learning-from-yml) PS C:\Users\******\Desktop> conda list
# packages in environment at C:\Users\******\anaconda3\envs\python-learning-from-yml:
#
# Name                    Version                   Build  Channel
autopep8                  1.6.0              pyhd3eb1b0_1
ca-certificates           2022.07.19           haa95532_0
certifi                   2022.6.15        py39haa95532_0
lazy-object-proxy         1.7.1                    pypi_0    pypi
openssl                   1.1.1q               h2bbff1b_0
pip                       21.2.4           py39haa95532_0
pycodestyle               2.8.0              pyhd3eb1b0_0
pyflakes                  2.5.0                    pypi_0    pypi
python                    3.9.12               h6244533_0
setuptools                61.2.0           py39haa95532_0
sqlite                    3.38.3               h2bbff1b_0
toml                      0.10.2             pyhd3eb1b0_0
tomli                     2.0.1                    pypi_0    pypi
tomlkit                   0.11.1                   pypi_0    pypi
typing-extensions         4.3.0                    pypi_0    pypi
tzdata                    2022a                hda174b7_0
vc                        14.2                 h21ff451_1
vs2015_runtime            14.27.29016          h5e58377_2
wheel                     0.37.1             pyhd3eb1b0_0
wincertstore              0.2              py39haa95532_2
wrapt                     1.14.1                   pypi_0    pypi
(python-learning-from-yml) PS C:\Users\******\Desktop>

.txt をもとに作成するのはあきらめました。。。

実行すると、こちらもエラーが出ました

(base) PS C:\Users\******\Desktop> conda create -n python-learning-from-txt -f python-learning.txt
Collecting package metadata (current_repodata.json): done
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed

PackagesNotFoundError: The following packages are not available from current channels:

  - python-learning.txt

Current channels:

  - 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

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

エラーの内容から

anaconda が標準的に用意しているダウンロードサイトからはもうダウンロードできなくなっているパッケージがあるらしい

まあ、yml で作成できたからいいか(よくない)

下記の記事を参考にすれば行けそうですが、 今回のように一気にインストールする場合はどれが対象かわからないため、今回は諦めます。。

https://1978works.com/2020/05/17/how-to-solve-the-problem-when-message-not-available-from-current-channels-is-displayed/

余談

pip と conda に互換性はないため、 Anaconda を使用している場合はpip は極力使わないほうがいいらしい

https://hitokageblog.com/condalisterror/

参考文献

https://qiita.com/ozaki_physics/items/13466d6d1954a0afeb3b

https://note.com/camelsnote/n/n9dec434c90ad

Discussion