How to Setup Conda Environment

When starting out, many new programmers are drawn to running their Python scripts directly through the default Python 2.x or 3.x interpreter. However, as your coding projects become increasingly intricate and multifaceted, the use of Virtual Environments becomes invaluable. They offer a way to manage your various codebases in an orderly fashion, ensuring that each project operates within its own distinct environment, free from the influence of external dependencies.

Prerequisite

Conda should be installed on you system

1. Create conda environment

Conda centrally manages the environments you create, so, you don’t have to bother about creating a folder for specific environments yourself. You can either start by creating an empty environment or mention the python version and packages you need at the time of creation itself.

1.1 Create an empty environment
conda create --name {env_name}
conda create --name myfirstenv 
1.2 Create an environment + specific python version
conda create --name {env_name} {python==3.7.5}
conda create --name myfirstenv python==3.7.5
1.3 Create an environment + specific Python version + packages

conda create –name env_name python==3.7.5 package_name1 package_name2

conda create --name myfirstenv  python==3.7.5 pandas numpy

2. Activate the conda environment

To activate the conda environment create in previous step use the below command
conda activate {env_name}

conda activate myfirstenv

3. Install more packages

Once activated you can install more packages using either conda or with pip

With Conda

conda install python==3.7.5 pandas

With pip

pip install python==3.7.5 pandas

or install multiple packages from requirements.txt.

pip install -r requirements.txt

However, I don’t recommend using pip inside conda environment, especially when you want to another person to be able to replicate your environment and run the programs.

4. See the list of packages and environments

4.1 Show list of packages in current environment

conda list

4.2 See list of packages in specific environment

conda list -n myfirstenv

4.3 See list of environments

conda env list

Sample Ouptut:

(myfirstenv) C:\Users\rahul>conda env list
# conda environments:
#
base                     C:\Users\rahul\anaconda3
generic-env              C:\Users\rahul\anaconda3\envs\generic-env
myfirstenv            *  C:\Users\rahul\anaconda3\envs\myfirstenv
spark-connect            C:\Users\rahul\anaconda3\envs\spark-connect

The current active environment will be marked with star (*)

5. Remove an environment

After making sure you are not in the environment:

conda env remove -n myfirstenv

6. Build an identical environment.

To create an environment that is identical to an existing one, explicitly create a spec file of the environment you want to duplicate and use it at the time of creating the new env.

Step 1: Create spec file

conda list --explicit > spec-file.txt

Step 2: Transfer the spec file to some other machine where you want to create the same environment

conda create --name myfirstenv --file spec-file.txt

You can do this in the same machine or a different machine as well, if you have the spec file.

If you want to install the packages in spec file, in an existing environment, run this.

conda install --name myfirstenv --file spec-file.txt

Leave a Reply

Your email address will not be published. Required fields are marked *