Python instllation and set up

This page describes how to install and set up Python on the computer.

Download Anaconda

Anaconda is a free, easy-to-install package and environment manager, which is “a distribution of the Python and R programming languages for scientific computing, that aims to simplify package management and deployment.” by Wikipedia.

Follow the instructions of the Anaconda homepage to download and install Anaconda.

Create virtual environments

If you use Python framework and library without a virtual environment, these libraries will be added in one place. As a result, the libraries may interface with each other and suddently nor work. To avoid this issue and continue to use your Python programming, I recommend to create a virtual environment isolated for Python projects. This means that each project has its own libraries and frameworks, which makes it easier to manage Python enviroments.

How to create virtual environments

(This information is copied from PyCLIM webpage.)

Update conda

$ conda update conda

Create a virtual environment for each project. Enter your favorite project name in “env_weclim” of the command below.

$ conda create -n env_weclim python=3

Install packages in the created virtural enviroment

Numpy and Xarry are libraries for the multi-dimensional analysis. PANDAS, a popular data analysis tool, is also installed automatically when you install Xarray.

$ conda install -n env_weclim numpy xarray

Matplotlib and Cartopy are libraries for xy plots and geosphatial mapping.

$ conda install -n env_weclim matplotlib cartopy

Dask is a library for effectively computing in paralle.

$ conda install -n env_weclim dask

If you want to analyze a one-dimensinal dataset (such as time-series data), the above package will suffice. When performing multi-dimensional analysis using NetCDF format datasets such as climate model output, GeoCAT is a useful tool for data analysis and visualization.

$ conda install -n env_weclim -c conda-forge -c ncar geocat-comp geocat-viz geocat-f2py netCDF4 eofs

‘eof’ is a library for empirical orthogonal function (EOF) analysis to identify the main variations of variables.

‘geocat-datafiles’ is a libary for calculating ocean stream function.

$ conda install -n env_weclim -c ncar geocat-datafiles

Activate the virtual environment

Finally, activate the virtural environment named “env_weclim”.

$ conda activate env_weclim

This command should be run every time you start your computer and start a project.

Other tips

Deactivate the virtual environment

$ conda deactivate

Remove the virtual enviroment

$ conda env remove --name env_weclim

Check if Python works

Run a python script. Download and save this file: test.py and run the following python command.

$ python test.py

If ‘Hello, World!’ is displayed, Python is working.