python, paths, and anaconda – prepare to astronomize
This is the beginning of a short series about the things I’ve learned in order to do astronomy research. This particular article may be insightful for citizen scientists or undergraduate astronomers interested in using and exploring their data via python.
Installing python via anaconda
The anaconda distribution is a suite of python (and R) packages, including conda, the python package manager, which helps organize both your base python and other python package installations and versions. Among anaconda’s packages is jupyter notebook, a web-based computational environment – it helps you write, execute, and save python code.
“But I can manage my versions and installs just fine! I don’t need a fancy environment to write code,” you say, lying such that the violent growth of your wooden nose breaks the sound barrier.
The anaconda installer will ask you where you’d like to install the distribution; I usually chose a fairly simple filepath, such as my users folder “C:\Users\William\Anaconda3” or the C: drive directly “C:\Anaconda3”. You can choose any location you’d like, but a more central folder will make future work a bit simpler.
Then, the installer will ask you whether you want to add anaconda to your PATH variable, and whether you want to register Anaconda as your default python installation. I would suggest no for the former, and yes for the latter.
Finishing the install wizard, you’ll have successfully installed all the fun things you need to start using python. After you finish your installation, we’ll go about adding python to our PATH variable so that we can run python directly from the command prompt.
Adding python to your PATH environment variable
Using your start search function (or control panel), make your way to your PC’s “Advanced system settings.” At the bottom of the window that will appear, click “Environment variables.”
Now, under “User variables” if you have user permissions, or “System variables” if you have administrator permissions, double click on the “path” variable
You’ll want to add two new environment variables; one leading to your base Anaconda3 folder, and one leading to Anaconda3\Scripts. The reason for this? This will allow you to use python, anaconda, conda, and all the other packages you install/manage via conda from your terminal.
Running python, launching jupyter
The result? You can now run python, conda, and anaconda commands from your command prompt. Simply typing “python” will open a python environment (you’ll know you’re in a python environment when your command prompt carrot “>” triples “>>>”). A common IDE (interactive development environment, a fancy name for a graphical interface for working with data and your code) comes with the default Anaconda distributions. Open your command prompt, and type “jupyter notebook” to get started; the IDE will open in your default browser. When you’re done, you can stop it running by pressing ctrl+c in your command prompt window.
A large portion of the research in the Follette Lab (where I research currently) is conducted through .ipynb (iPython notebooks) which are very good for playing with code, manipulating data, and testing functions; jupyter was built to run these, and it does it really well. They aren’t that great for quickly working .py files (a streamlined piece of python code called a script); while jupyter notebook is more than capable of editing scripts, it doesn’t have enough functionality as a text editor to compensate for the time it takes to launch a notebook server and maneuver jupyter’s interface. I use jupyter to work with notebooks, but…
the Atom text editor is nice
When I want to create and edit files and scripts in any coding language I use Atom. It’s very modular and therefore has a lot of cool capabilities. After installing atom, install the following packages via conda install: ipython, ipykernel, python-language-server and then install the atom packages via apm install: hydrogen, hydrogen-launcher, platformio-ide-terminal. After this, you should be able to shift+enter within a .py file and run code piecemeal the same way you’d run it in jupyter notebook.
Thank you for reading! I hope this helps you get started with using python on your PC. This series of posts may extend to working within jupyter notebook, writing code in python, or essential packages for doing some astro-stuff, but until then, happy coding.