ホーム 暮らし [Memorandum] Setting up Python virtual environments and package management in Anaconda; conda and pip

[Memorandum] Setting up Python virtual environments and package management in Anaconda; conda and pip

雨宮 千夏 雨宮 千夏 • 更新: • 公開: • 読了約 6 分
目次

This is a memorandum.
Basically, I use conda for package management. However, there are some packages that are only available via pip. You must prepare them in a virtual environment. Also, when using deep learning libraries such as mxnet, pytorch on Windows, it is recommended to use Python 3.7 (as of October 2021).

Listing virtual environments in Anaconda

conda info -e

Creating (building) a virtual environment

# your_env_nameに作成したい任意の仮想環境の名前を入力する
conda create -n your_env_name 
pythonを指定する場合
conda create -n your_env_name python=3.7.7
yamlファイルから環境構築する
conda env create -n your_env_name -f your_env_name.yaml
上手くいかない場合はcondaを更新する。
conda update conda
pipも更新する。
python -m pip install --upgrade pip

Activating/deactivating a virtual environment

# Linux shell または Anaconda Prompt
conda activate your_env_name
Windows Powershell
activate your_env_name
仮想環境の停止
conda deactivate

Installing packages

conda install package_name
version指定
conda install package_name==x.y.z
Proceed(Yes,No): をスキップする。
conda install -y package_name

Deleting unnecessary environments

# 環境の削除
conda remove -n your_env_name --all
不要なパッケージやキャッシュの削除
conda clean --all

Saving a virtual environment to a yaml file.

#プロジェクトファイル配下に保存する場合
conda env export -n your_env_name > your_env_name.yaml
深い階層で保存することも可能
conda env export -n your_env_name > ./env/your_env_name.yaml

Bulk updating packages with pip.

#Powershell
pip install --upgrade ((pip3 freeze) -replace '==.+','')
Linux
pip list -o | tail -n +3 | awk 'print $1' | xargs sudo pip install -U

Checking if dependencies are broken.

#condaからインストールしたパッケージも確認くださる便利コマンド
pip check

Troubleshooting

n2b821cc52508
pip install --upgrade mxnet~=1.7

When
ImportError: No module named _sysconfigdata_x86_64_conda_linux_gnu
occurs after installing mxnet.
It can be resolved by copying _sysconfigdata_x86_64_conda_cos6_linux_gnu.py from base to the virtual environment.

sudo find / -name _sysconfigdata_x86_64*
cp ~/anaconda3/lib/python3.8/_sysconfigdata_x86_64_conda_cos6_linux_gnu.py ~/anaconda3/envs/your_env/lib/python3.8/_sysconfigdata_x86_64_conda_linux_gnu.py

※ Please note that the filename of the source (base) and the destination (where Python looks) are different.

Reference articles

https://stackoverflow.com/questions/68261254/conda-error-sysconfigdata-x86-64-conda-linux-gnu


同じカテゴリの記事

雨宮 千夏
デジタルマーケター 雨宮 千夏
プロフィールを見る →

アイドルと音楽シーンの取材を担当。公式サイトの公演情報、リリース告知、公開チャートを照合してから構成しています。

おすすめの関連記事