python, paths, and anaconda

This is the beginning of a 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. Even if you *think* you can manage your versions correctly, conda helps you be safe, and even enables you to manage separate environments with different versions of the same code you might need for different projects.

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 running some task, like .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.

Comments

2 responses to “python, paths, and anaconda”

  1. Jer Avatar
    Jer

    I’ve been using python since 2.5, I think you may have pushed me into looking into jupyter. I’ve got a huge data set that I need to come up with a way to create a front end that lets folks query and compare testruns. Keep the python blogs coming…

    1. willb Avatar

      Some folks in my lab are currently using flask + jupyter to make a webpage query service for the telescope we use. If I can prod them to see how they’re organizing their querying I’ll let you know jer

Leave a Reply

Your email address will not be published. Required fields are marked *