Work with Python3 in GLUON: Miniconda3 Guide

Virtual environments in Python are an essential tool, especially when working on servers. They offer several key advantages that make them indispensable for Python development and deployment in server environments.

Why Use Python Virtual Environments on Servers?

  1. Isolation of Dependencies: Each Python project can have its own dependencies, which might conflict with those of another project. Virtual environments keep these dependencies separate, ensuring that projects run reliably without interference.

  2. Control Over Python Versions: Different projects may require different Python versions. Virtual environments allow you to specify and manage the Python version used in each environment, ensuring compatibility and avoiding conflicts.

  3. Freedom to Install Packages: In server environments, you often do not have the liberty to install Python packages globally. Virtual environments solve this by allowing you to install packages in an isolated environment using tools like pip, without affecting the global Python installation.

  4. Simplified Dependency Management: Managing project-specific dependencies becomes much easier with virtual environments. You can freeze the dependencies in a requirements.txt file, making it straightforward to replicate the environment elsewhere.

Why Choose Miniconda3?

Miniconda3 stands out as an ideal choice for managing Python virtual environments on servers due to its simplicity and efficiency.

  • Lightweight and Minimalist: Miniconda3 provides a barebones installation of Conda, making it a lightweight choice, especially suitable for server environments where resources are a consideration.

  • Ease of Use: Miniconda3 offers a simple and straightforward approach to environment and package management, making it accessible even to those who are new to Python development.

  • Versatile and Powerful: Despite its simplicity, Miniconda3 is powerful enough to handle complex dependency management and environment configurations, making it a versatile tool for a wide range of Python projects.

Installing Miniconda3 on Linux

  1. Download Miniconda3 using wget

    [gluon_user@glui01 ~]$  wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
  2. Install Miniconda3

    • Run the installer script:

    [gluon_user@glui01 ~]$  bash Miniconda3-latest-Linux-x86_64.sh
    • Follow the prompts on the installer screens. You may need to agree to the license terms and specify the installation location.

  1. Verify Installation

    • To ensure Miniconda is installed correctly, open a new terminal session and type:

    [gluon_user@glui01 ~]$  conda list

    If the installation is successful, you’ll see a list of installed packages.

Creating Virtual Environments with Conda

Conda allows you to create isolated environments with specific versions of Python and other packages.

  1. Creating an Environment with a Specific Python Version

    • To create a new environment with a specific Python version:

    [gluon_user@glui01 ~]$   conda create --name myenv python=3.8

    Replace myenv with your desired environment name and 3.8 with the desired Python version.

  2. Creating an Environment with Specific Packages

    • To create an environment with specific packages, include them in the command:

    [gluon_user@glui01 ~]$  conda create --name myenv python=3.8 scipy=1.5

    This command creates an environment named myenv with Python 3.8 and SciPy 1.5.

Activating and Deactivating Environments

  1. Activating an Environment

    • Activate the environment using:

    [gluon_user@glui01 ~]$  conda activate myenv

    Replace myenv with the name of the environment you want to activate.

  2. Deactivating an Environment

    • Deactivate the current environment with:

    [gluon_user@glui01 ~]$  conda deactivate

Listing Available Conda Environments

Once you have created one or more environments, you can list all available environments to see which ones you can activate and use.

  1. List Environments

    • To view a list of all Conda environments, use the following command:

    [gluon_user@glui01 ~]$  conda env list
    • This command will display a list of all environments installed on your system. The environment currently activated will be marked with an asterisk (*).

  1. Understanding the List Output

    • The output will show each environment’s name and the path where it is located. The default environment is named base.

    • Example output:

    # conda environments:
    #
    base                  *  /home/user/miniconda3
    myenv                    /home/user/miniconda3/envs/myenv
    another-env              /home/user/miniconda3/envs/another-env
    

    In this example, base is the active environment, as indicated by the asterisk (*). myenv and another-env are other available environments.