Install Python:
You will need terminal access for this process. Contact the workshop instructor if you have no experience working in a terminal.
Test if you already have Anaconda or Miniconda installed. In your terminal type the folloing commands:
$ bash
$ which conda
If this returns nothing, please proceed to the next step. If it produces a path such as /Users/yourname/anaconda/bin/conda (yours may be different), skip the next step and go straight to step 2.
-
Install Miniconda Python 3 by following the directions here. A video tutorial is available here. You do not need root access. We recommend Miniconda rather than Anaconda because it is a faster and more light-weight installation.
-
Create an conda environment by running the following commands (the first may take a few minutes to execute). Replace
your_env_name
with something useful, saycarpentries
:
$ conda create --name your_env_name python -y
$ conda activate your_env_name
$ pip install ipython numpy matplotlib astropy jupyter
Note: This should be enough to get you through the workshop but just in case, this is how you would install a new packages:
$ conda activate your_env_name
$ pip install new_package
Note: This class does not use the popular astroconda collection of packages. If you need to install that, the instructions are the first 4 yellow boxes on this page. You can use an existing astroconda environment for this class.
-
Clean up your workspace. Check your default profile files (.cshrc, .bashrc, .bash_profile) and comment any entries that define or add to $PYTHONPATH. Start a fresh Terminal window and type echo $PYTHONPATH - you should not see a long jumble of paths to unknown corners of your hard drive. If you don’t know what this means, skip to the next one.
-
Get yourself a nicer terminal. iTerm is really nice for OSX. For Windows, here is Git Bash.
-
For this class we can use Nano, Emacs or VIM, but if you want a nicer text editor, download Sublime Text, Atom, BBEdit or TextWrangler. We will not cover IDEs such as PyCharm or Spyder.
Obtain lesson materials
- Download intro_python_data.tar.gz.
- Create a folder called
swc-python
on your Desktop. - Move downloaded file into this newly created folder.
- Unzip the files (gunzip file.tar.gz) and untar (tar -xvf file.tar) or all in one go (tar -zxvf file.tar.gz).
You should now see a new folder called data
in your swc-python
directory on your
Desktop.
Navigate to the data
folder
If you’re using a Unix shell application, such as the Terminal or iTerm app in macOS, Console or Terminal in Linux, or Git Bash on Windows, execute the following command:
$ cd ~/Desktop/swc-python/data
On Windows, you can use its native Command Prompt program. The easiest way to start it up is by
pressing Windows Logo Key+R, entering cmd
, and hitting Enter. In
the Command Prompt, use the following command to navigate to the data
folder:
$ cd /D %userprofile%\Desktop\swc-python\data
Activate your Python environment
In the terminal, type the following commands:
$ bash
$ conda activate your_env_name
Option 1: Launch Plain Vanilla Python interpreter
To start working with Python, we need to launch a program that will interpret and execute our Python commands. To launch a “plain vanilla” Python interpreter, execute:
$ python
If you are using Git Bash on Windows, you have to call Python via winpty
:
$ winpty python
Option 2: Start IPython interpreter
IPython is an alternative solution situated somewhere in between the plain vanilla Python interpreter and Jupyter notebooks. It provides an interactive command-line based interpreter with various convenience features and commands. You should have IPython on your system if you installed Anaconda Distribution.
To start using IPython, execute:
$ ipython
Option 3: Start Jupyter notebook
Jupyter notebooks provide a browser-based interface for working with Python. If you would like to use a notebook during the lesson, make sure to install Anaconda Distribution.
To start a Jupyter server, execute:
$ jupyter notebook
Then create a new notebook by clicking “New” button on the right and selecting “Python 3” from the drop-down menu: