![Print Print](https://www.e-education.psu.edu/ngapython/sites/all/modules/print/icons/print_icon.png)
Another option for packaging and distributing your Python programs is to use conda. Just like pip, it is a package manager. In addition, it is also an environment manager. What that means is that you can use conda to create virtual environments for Python, while specifying the packages you want to have available in that environment. Conda comes installed with ArcGIS Pro. We can double check that it is by opening the Python Command Prompt and then typing in:
cd Scripts
followed by:
conda –-version
The output should show the conda version.
In order to find out what packages are installed type in:conda list
Your output should look something like Figure 2.34:
Figure 2.34 Conda Package List
The first column shows the package name, the second the version of the package. The third column provides clues on how the package was installed. You will see that for some of the packages installed, Esri is listed, showing they are related to the Esri installation. The list option of conda is useful, not only to find out if the package you need is already installed but also to confirm that you have the appropriate version.
Conda has the functionality to create different environments. Think of an environment as a sandbox – you can set up the environment with a specific Python version and different packages. That allows you to work in environments with different packages and Python versions without affecting other applications. The default environment used by conda is called base environment. We do not need to create a new environment, but, should you need to, the process is simple – here is an example:
conda create -n gisenv python=3.6 arcpy numpy
the –n flag is followed by the name of the environment (in this case gisenv), then you would choose the Python version which matches the one you already have installed (3.5, 3.6 etc.) and follow that up with a list of packages you want to add to it. If you later find out you need other packages to be added, you could use the install option of conda, for example:
conda install –n gisenv matplotlib
To activate an environment, you would run:
activate gisenv
And to deactivate an environment, simply:
deactivate
Full reference to the command line arguments and flags are helpful.
To see the commands usage, you access the documentation through the command: conda env [-h] command ... as
conda env –h
In the conda prompt.
Output: positional arguments: {create,export,list,remove,update,config} create Create an environment based on an environment definition file. If using an environment.yml file (the default), you can name the environment in the first line of the file with 'name: envname' or you can specify the environment name in the CLI command using the -n/--name argument. The name specified in the CLI will override the name specified in the environment.yml file. Unless you are in the directory containing the environment definition file, use -f to specify the file path of the environment definition file you want to use. export Export a given environment list List the Conda environments remove Remove an environment update Update the current environment based on environment file config Configure a conda environment optional arguments: -h, --help Show this help message and exit. Once you know the command you want to use, you can add the parameter and view its help. conda env export –h Export a given environment Options: optional arguments: -h, --help Show this help message and exit. -c CHANNEL, --channel CHANNEL Additional channel to include in the export --override-channels Do not include .condarc channels -f FILE, --file FILE --no-builds Remove build specification from dependencies --ignore-channels Do not include channel names with package names. --from-history Build environment spec from explicit specs in history Target Environment Specification: -n ENVIRONMENT, --name ENVIRONMENT Name of environment. -p PATH, --prefix PATH Full path to environment location (i.e. prefix). Output, Prompt, and Flow Control Options: --json Report all output as json. Suitable for using conda programmatically. -v, --verbose Use once for info, twice for debug, three times for trace. -q, --quiet Do not display progress bar. examples: conda env export conda env export --file SOME_FILE
Once we have made sure that everything is working correctly in this new environment and we built our export command, we can export a YAML file using the command:
conda env export > “C:\Users\...\Documents\ArcGIS\AC37.yml”
This creates a file that contains the name, channel and version of each package in the environment and can be used to create or restore an environment to this state. It also includes packages that were installed via pip, and will attempt to install them through conda first when the file is used to create the environment.
It is important to note that using pip can break a conda environment which results in conda not being able to resolve dependencies. There are times when packages are only found in pip and for those instances, you can create the conda environment first, and then use pip. This is not guaranteed and could take several tries to find the right package version numbers that work nicely together.
There are other options you can use with environments – a great resource for the different options is Conda's Managing Environments page
Lesson content developed by Jan Wallgrun and James O’Brien