Environments#

Safe, longevity-inspiring code is written in an environment. Why?

Primarily, it is because software is always evolving, and both the language (Python) and packages (python codes) have new versions released nearly-continuously. Because of this, it is important to track for a given code you are writing which versions of Python and associated packages are compatible with your code, so that in the future, you know you can create such an environment again and your code will work.

As discussed in the Installation guide, we are using conda as our environment manager. What conda allows us to do is create isolated “buckets,” with a particular version of Python and a particular set of packages (of particular versions) installed. These buckets are isolated from one another; when you install a package in one environment, it won’t be in the others.

Note

It is always a good idea to create a new environment when you are starting a new research project, installing a particularly complex code that has a lot of dependencies, or developing your own piece of software.

You should by now have miniconda installed, meaning you are ready to create an environment and install python. Let’s do it!

Exercise One: Make an Environment

In your shell/terminal (Terminal on Mac, Ubuntu on WSL), write

conda create -n YOURNAMEHERE python=3.11 numpy scipy matplotlib astropy pandas jupyterlab ipython ipykernel nb_conda_kernels

replacing the YOURNAMEHERE with the name you want to give this environment (good choices are research or umbrella or bootcamp or something to do with your summer project.

When you run this, you will eventually get a prompt asking for a Y/n input; type Y and hit enter.

What this commmand does is create a new environment with the name you give it. We’ve specifically told it to use Python 3.11. We’ve also given it a list of packages that can be installed via conda; I chose these because they are universally useful in astronomy and we will need them anyway. You can specify their versions here, as we did for python, but for now, we just want the latest versions.

Exercise Two: Activate Your Environment

Once your environment is created, you’ll see instructions for “activating” it. Follow them, e.g.,

conda activate myenvironment

where you enter your environments name.

Once inside the environment, you should see its name on the left hand side of your terminal prompt, in parenthesis. Lets install a few more things, but this time, using pip, the standard python package installer

pip install tqdm 
pip install astroquery

Conda vs. Pip installs

When do we install a package using conda, and when do we use pip? This is a bit messy, but in general, I suggest checking first if a conda version of the package is available. If it is, try installing via conda first. If not, or if that fails, then fall back on using pip (Inside your environment of course!). Using pip inside an environment will still mean you are only installing to that environment, but some of conda’s ways of measuring package dependencies and compatibility don’t extend to things installed with pip. Generally, we create environments with the “big things” installed with conda (as above), then install smaller, or pip-only things, inside with pip.

You’ve made it to the end of Environments! To summarize our main points:

  • Environments allow us to create isolated buckets in which to install packages (and versions of python)

  • This is useful, as packages can often conflict with one another, or require a specific version

  • We should make new environments for each research project we start, along with perhaps a base ‘research’ env.

  • Creating envs is easy with conda create, we start off with a set of packages we always need, then activate our env to jump inside it, pip install anything else we need, and then are off to the races!