Now that we have a foundational understanding of UNIX and filesystems, let’s explore the Python programming language and its ecosystem. Python is widely used in scientific computing, and understanding its installation, package management, and the use of virtual environments is crucial.
Python Installation and Package Management¶
Python can be installed in different ways on your computer. Usually, when we use python we also use external libraries. An external library is basically a collection of useful code written by someone else that you can acess without having to rewrite it yourself (you will learn more about these over the next few weeks). Python relies on package managers to install, update, and manage external libraries. The two primary package managers are:
pip: The default Python package installer.
- Example:
pip install package_name
- Example:
Conda: A cross-language package manager that simplifies dependency management.
- Example:
conda install package_name
- Example:
Understanding how to use these package managers is essential for incorporating external libraries into your Python projects. You might also encounter homebrew or MacPorts. These are two other popular package managers, but they are not as common in astronomy.
Using Conda as a Package Manager¶
When we use conda
, we have the option of using different distributions:
- Miniconda: A minimal installer for the Conda package manager, which only includes Conda and Python.
- Anaconda: A distribution that includes Python, Conda, and a large array of pre installed packages.
One of the features of conda (as opposed to pip) is that we can create virtual environments. You should all now have miniconda installed, so we can start making a virtual environment!
Python Virtual Environments¶
What is a Virtual Environment?¶
A virtual environment in Python is a self-contained directory that encapsulates a specific Python interpreter along with its associated libraries and scripts. It allows you to create isolated environments for different projects, each with its own set of dependencies, without affecting the system-wide Python installation.
Why Use Virtual Environments?¶
When working on multiple projects, each with its own set of dependencies, managing packages can become challenging. Virtual environments provide an isolated space for each project, allowing you to avoid conflicts between different project requirements. Code in pacakges and libraries can often change with new releases and versions, so it’s important to work in environments to keep track of your Python version and the versions of the external packages you use.
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.
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