Python Tutorials - Herong's Tutorial Examples - v2.14, by Herong Yang
Introduction to Conda Environment
This section provides a quick introduction about the Conda environment, its file directories and command programs. Steps to verify its Python version, and install new packages.
After installing Miniconda, we should take a quick tour on the "base" environment it offers.
1. Conda "base" environment program files are located in ~/miniconda3. They look very much like a mini operating system.
drwxrwxr-x. 2 herong herong 4096 bin drwxrwxr-x. 2 herong herong 30 compiler_compat drwxrwxr-x. 2 herong herong 19 condabin drwxrwxr-x. 2 herong herong 4096 conda-meta drwxrwxr-x. 2 herong herong 6 envs drwxrwxr-x. 4 herong herong 35 etc drwxrwxr-x. 9 herong herong 4096 include drwxrwxr-x. 15 herong herong 4096 lib -rw-r--r--. 1 herong herong 11245 May 19 15:29 LICENSE.txt drwxrwxr-x. 61 herong herong 8192 pkgs drwxrwxr-x. 10 herong herong 116 share drwxrwxr-x. 3 herong herong 22 shell drwxrwxr-x. 3 herong herong 146 ssl drwxrwxr-x. 3 herong herong 21 x86_64-conda_cos6-linux-gnu
2. Commands (some of them override system commands) provided by Conda are located in ~/miniconda3/bin.
lrwxrwxrwx. 1 herong herong 8 2to3 -> 2to3-3.8 -rwxrwxr-x. 1 herong herong 114 2to3-3.8 -rwxrwxr-x. 1 herong herong 197 activate lrwxrwxrwx. 1 herong herong 3 captoinfo -> tic -rwxrwxr-x. 1 herong herong 243 chardetect -rwxrwxr-x. 2 herong herong 14288 clear -rwxrwxr-x. 1 herong herong 524 conda -rwxrwxr-x. 1 herong herong 161 conda-env -rwxrwxr-x. 1 herong herong 247 cph -rwxrwxr-x. 1 herong herong 6214 c_rehash ... -rwxrwxr-x. 2 herong herong 21216 lzmadec -rwxrwxr-x. 1 herong herong 21128 lzmainfo lrwxrwxrwx. 1 herong herong 6 lzmore -> xzmore -rwxrwxr-x. 1 herong herong 8076 ncursesw6-config -rwxrwxr-x. 2 herong herong 836336 openssl -rwxrwxr-x. 1 herong herong 243 pip lrwxrwxrwx. 1 herong herong 8 pydoc -> pydoc3.8 lrwxrwxrwx. 1 herong herong 8 pydoc3 -> pydoc3.8 -rwxrwxr-x. 1 herong herong 97 pydoc3.8 lrwxrwxrwx. 1 herong herong 9 python -> python3.8 lrwxrwxrwx. 1 herong herong 9 python3 -> python3.8 -rwxrwxr-x. 2 herong herong 14707712 python3.8 -rwxrwxr-x. 1 herong herong 3498 python3.8-config lrwxrwxrwx. 1 herong herong 16 python3-config -> python3.8-config lrwxrwxrwx. 1 herong herong 4 reset -> tset -rwxrwxr-x. 2 herong herong 1508240 sqlite3 -rwxrwxr-x. 2 herong herong 29841 sqlite3_analyzer -rwxrwxr-x. 2 herong herong 14280 tabs lrwxrwxrwx. 1 herong herong 8 tclsh -> tclsh8.6 ...
3. Python version in Conda environment could be different:
herong$ python3 --version Python 3.6.8 herong$ conda activate (base) herong$ python3 --version Python 3.8.3
4. Python libraries, like NumPy, need to be installed in Conda environment. This is done by the "conda install ..." command, instead of the "pip install ...". A Python library is a Conda package, from Conda's point of view.
(base) herong$ python Python 3.8.3 (default, May 19 2020, 18:47:26) [GCC 7.3.0] :: Anaconda, Inc. on linux >>> import numpy ModuleNotFoundError: No module named 'numpy' >>> exit() (base) herong$ conda install numpy ## Package Plan ## environment location: /home/herong/miniconda3 added / updated specs: - numpy The following NEW packages will be INSTALLED: blas pkgs/main/linux-64::blas-1.0-mkl intel-openmp pkgs/main/linux-64::intel-openmp-2020.1-217 mkl pkgs/main/linux-64::mkl-2020.1-217 mkl-service pkgs/main/linux-64::mkl-service-2.3.0-py38he904b0f_0 mkl_fft pkgs/main/linux-64::mkl_fft-1.1.0-py38h23d657b_0 mkl_random pkgs/main/linux-64::mkl_random-1.1.1-py38h0573a6f_0 numpy pkgs/main/linux-64::numpy-1.19.1-py38hbc911f0_0 numpy-base pkgs/main/linux-64::numpy-base-1.19.1-py38hfa32c7d_0 The following packages will be UPDATED: ca-certificates 2020.1.1-0 --> 2020.6.24-0 certifi 2020.4.5.1-py38_0 --> 2020.6.20-py38_0 conda 4.8.3-py38_0 --> 4.8.4-py38_0 Proceed ([y]/n)? y Downloading and Extracting Packages ... (base) herong$ python Python 3.8.3 (default, May 19 2020, 18:47:26) [GCC 7.3.0] :: Anaconda, Inc. on linux >>> import numpy as np >>> np.v2.14 '1.19.1'
5. Create a new Conda environment called "graph" using "conda create" command:
# get out of the current Conda environment. (base) herong$ conda deactivate herong$ conda create -n graph Collecting package metadata (current_repodata.json): done Solving environment: done ## Package Plan ## environment location: /home/herong/miniconda3/envs/graph Preparing transaction: done Verifying transaction: done Executing transaction: done # # To activate this environment, use # # $ conda activate graph # # To deactivate an active environment, use # # $ conda deactivate
6. List available Conda environments: using "conda create" command:
herong$ conda info -e # conda environments: # base * /home/herong/miniconda3 graph /home/herong/miniconda3/envs/graph
7. Upgrade Conda platform using "conda update" command:
herong$ conda update conda ## Package Plan ## environment location: /home/herong/miniconda3 added / updated specs: - conda The following packages will be downloaded: package | build ---------------------------|----------------- _openmp_mutex-4.5 | 1_gnu 22 KB brotlipy-0.7.0 |py38h27cfd23_1003 323 KB ca-certificates-2021.7.5 | h06a4308_1 113 KB ... The following packages will be DOWNGRADED: pysocks 1.7.1-py38_0 --> 1.7.1-py38h06a4308_0 Proceed ([y]/n)? y Downloading and Extracting Packages libgcc-ng-9.3.0 | 4.8 MB | #### | 41% mkl_fft-1.3.0 | 177 KB | ########## | 100% ... Preparing transaction: ...working... done Verifying transaction: ...working... done Executing transaction: ...working... done
8. Run Python scripts in a specific Conda environment in a single command.
herong$ conda run -n graph python hello.py Hello world!
9. Remove a Conda environment using "conda env remove" command:
(graph) herong$ conda deactivate herong$ conda env remove -n graph Remove all packages in environment /usr/local/anaconda3/envs/graph: herong$ conda env list # conda environments: # base /usr/local/anaconda3 test /usr/local/anaconda3/envs/test
10. Renaming Conda environment name can not be done by a single direct "conda" command. You have to clone the old environment to a new environment with the new name.
herong$ conda create --name new_name --clone old_name herong$ conda remove --name old_name --all
Table of Contents
Variables, Operations and Expressions
Function Statement and Function Call
Iterators, Generators and List Comprehensions
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
►Anaconda - Python Environment Manager
►Introduction to Conda Environment
Share Conda Environment with Others
Manage Packages in Conda Environment
Run Conda Environment Command in Background