How To Set Up A Conda Environment
Typical python projects uses multiple packages for various tasks. And some of the packages are shared between projects besides.
Sharing same packages betwixt projects tin cause problems.
How?
When you update ane of the packages used in a projection, it might cause compatibility problems in the other packages that use information technology. On upgrading, information technology can also cause dependency issues. That is, dependent packages that are use the lawmaking of the upgraded package can break.
This issue is effectively handled by using virtual environments.
Some of the popular virtual surroundings implementations for Python are:
one. Virtualenv
2. Conda
3. pipenv
4. venv
Several others exist.
However the virtually pop ones are Conda, Pipenv and venv as well. Specifically, Conda is popular amidst Data Scientists whereas pipenv is popular amongst software engineers.
Conda is a parcel manager and a virtual environment and it provides the convenience of allowing you to manage what version of Python the virtual surround (and as a result your project) uses also. So naturally, conda is very convenient and I use it my projects as well.
Install Conda
Y'all can install conda via anaconda or miniconda
Source: LinuxnetMag
How to use conda environs?
Well, you demand to know a few commands to create and activate the surroundings and effortlessly install and uninstall package versions you want to use.
Let's wait at them.
But before you lot start make sure you've installed Anaconda. If y'all utilize windows, in 'Beginning' yous need to type and start the 'Anaconda prompt'. If you are on Mac or Linux, you lot can do all of these in Final.
1. Create conda environment
Conda centrally manages the environments yous create, and so, you don't accept to bother about creating a binder for specific environments yourself. You tin can either start past creating an empty environment or mention the python version and packages you need at the time of creation itself.
(i) Create an empty surround
conda create --name {env_name} conda create --name mlenv
(ii) Create an environment + specific python version
conda create --name {env_name} {python==3.7.5} conda create --name mlenv python==3.7.v
This will also install packages similar pip
, wheel
, setuptools
. You tin can then actuate the environment (see below) and
(three) Create an environment + specific Python version + packages
conda create --name env_name python==3.7.v package_name1 package_name2
Example:
conda create --name mlenv python==3.7.5 pandas numpy
2. Actuate the environment
conda activate {env_name}
To deactivate whichever you are currently in, use:
conda deactivate
3. Install more than packages
One time activated yous can install more packages using either conda
or with pip
.
With Conda
conda install pkg_name1==1.x.y pkg_name2==1.x.y
With pip
pip install pkg_name2==ane.10.y pkg_name2==one.x.y
or install multiple packages from requirements.txt
.
pip install -r requirements.txt
Nevertheless, I don't recommend using pip
inside conda environment, particularly when yous desire to another person to exist able to replicate your environment and run the programs. See the "Sharing environments across platforms" department beneath if you lot want to know the exact reason.
4. See the list of packages and environments
(i) Show list of packages in electric current environs
conda listing
(ii) See list of packages in specific environment
conda list -n myenv
(iii) See list of environments
conda env listing # or conda info --envs
Sample Ouptut:
# conda environments: C:\Users\selva\.julia\conda\iii base * C:\Users\selva\AppData\Local\Continuum\anaconda3 envmnt C:\Users\selva\AppData\Local\Continuum\anaconda3\envs\envmnt juliaenv C:\Users\selva\AppData\Local\Continuum\anaconda3\envs\juliaenv mlcourse C:\Users\selva\AppData\Local\Continuum\anaconda3\envs\mlcourse
The current active surround will be marked with star (*)
.
five. Remove an environment
After making certain you lot are not in the environs:
conda env remove -n env_name
6. Build an identical environment.
To create an surroundings that is identical to an existing i, explicitly create a spec file of the surroundings you want to indistinguishable and use it at the fourth dimension of creating the new env.
Step 1: Create spec file
conda list --explicit > spec-file.txt
Step 2:
conda create --name myenv --file spec-file.txt
You can do this in the same machine or a different machine as well, if you accept the spec list.
If you want to install the packages in spec file, in an existing environment, run this.
conda install --name env_name --file spec-file.txt
7. Sharing environments across platforms (better way)
There is a common problem when you try to replicate your environment in another organisation / platform.
When you lot create an environment and packages, lets say you run something like this (insert your package names).
conda install python=three.vii pkg_name1 pkg_name2 pkg_name3
This downloads and installs numerous other dependent packages in order to make the packages you wanted to install work.
This tin easily introduce packages that may not be compatible across platforms.
So instead, utilize this:
conda env export --from-history > environment.yml
Adding --from-history
flag will install only the packages yous asked for using conda. It will NOT include the dependency packages or packages you installed using any other method.
Now, How to re-create the environment?
Pass the environment.yml
and the other person can re-create your environment by running:
conda env create -f environment.yml
8. Restore / Rollback to an earlier version of an environment
Conda maintains a history of changes you make to an environment, past changes, I mean the changes you made using the conda commands. It allows y'all to roll back changes past using revision numbers.
Upon activating an surroundings, first see the revisions and the revision numbers using this.
conda list --revisions
So, whorl back
conda install --rev three
How To Set Up A Conda Environment,
Source: https://www.machinelearningplus.com/deployment/conda-create-environment-and-everything-you-need-to-know-to-manage-conda-virtual-environment/
Posted by: eakinyout1935.blogspot.com
0 Response to "How To Set Up A Conda Environment"
Post a Comment