Work with Python3 in GRAVITON: 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?
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.
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.
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.
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
Download Miniconda3 using wget
First, access the Miniconda download page (https://docs.conda.io/en/latest/miniconda.html) to find the link to the latest Linux installer.
Use wget to download Miniconda3.
graviton_user@gr01:~$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
Install Miniconda3
Run the installer script:
graviton_user@gr01:~$ 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.
Verify Installation
To ensure Miniconda is installed correctly, open a new terminal session and type:
graviton_user@gr01:~$ 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.
Creating an Environment with a Specific Python Version
To create a new environment with a specific Python version:
graviton_user@gr01:~$ conda create --name myenv python=3.8
Replace myenv with your desired environment name and 3.8 with the desired Python version.
Creating an Environment with Specific Packages
To create an environment with specific packages, include them in the command:
graviton_user@gr01:~$ 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
Activating an Environment
Activate the environment using:
graviton_user@gr01:~$ conda activate myenv
Replace myenv with the name of the environment you want to activate.
Deactivating an Environment
Deactivate the current environment with:
graviton_user@gr01:~$ 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.
List Environments
To view a list of all Conda environments, use the following command:
graviton_user@gr01:~$ 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 (*).
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.
Conclusion
This guide covers the basics of installing Miniconda3 on a Linux server and managing Python virtual environments using Conda. These steps allow you to create environments with specific versions of Python and packages, ensuring isolated and consistent workspaces for your Python projects.