Python - UMKCgeg/Wiki GitHub Wiki
Python is an easy to learn programming language that is very useful for astronomy.
Writing/Running code
Python is installed on all the GEG machines, so you shouldn't have to do anything to get Python set up.
There are several ways to run Python code. The simplest way is in just in the command line. Just type python
in the command line, and you'll get the Python interpreter. Here you can experiment and try things out. The GEG machines have IPython, which is a souped-up version of the interpreter. Call ipython
to access this. To exit both IPython and the regular Python interpreter, call exit()
.
While the interpreter is a good way to mess around and try things, you'll probably want to write a script at some point. Write your code in your text editor of choice, and save the file with a .py
extension. Then from the terminal, call python your_script.py
. This will run it.
Some people like to have more than just a text editor to edit their code. IDEs (Interactive Development Environments) provide many tools to make writing your code easier. Good autocomplete, a debugger, the ability to run code from within the application, and the ability to easily refactor code are some reasons to consider an IDE. PyCharm is one of the most powerful IDEs out there, and is widely used. It's fantastic. There is a free version. Spyder is another one, targeted specifically at scientific Python.
Another option that's gaining popularity is the Jupyter notebook. This unorthodox format combines text, code, plots, and anything you can imagine into one file. They allow the ease of interactive mode with the convenience of having all the code saved in a file. They are particularly useful for data exploration or testing things. For many GEG members, much of their Python is done in notebooks. The Python Tutorial (also linked below) contains an intro to Jupyter notebooks, among other things.
Python Package Management
Installing and managing Python packages is very important, but can be really difficult with Python. See the Python Package Management page for more info.
Python Tutorials
Members of the GEG have written some introductory Python tutorials that are hosted here. These assume some knowledge of Python, but introduce Jupyter notebooks and Python plotting, and will be expanded in the future.
There are also Astropy Tutorials on AstroBetter than introduce some useful things Astropy can do.
Other Python related pages
- Source Extractor from within Python: How to write a wrapper for Source Extractor
- EzGal: How to use Stellar Population Synthesis Models
- PyRAF: A wrapper for IRAF in Python
- Publicizing Your Code: Has info on how to make your Python code easily accessible to others with
pip
. - Python tips and tricks: A list of some small but useful tricks for Python.
Python scripts uploaded
This section lists the scripts that have been uploaded to this wiki.
- center.ipynb: This Notebook shows how to find the center of an overdensity using only a catalog.
Other Useful Links
- Sinistra: A Python library that has many useful astronomy related functions and classes that may not be found in other packages. There are a lot of simple things that are pretty simple to do, but having them already done is a time saver.
- Betterplotlib: A Python library that acts as a wrapper to matplotlib to make Python plotting nicer and easier.