{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Programming Environment: Linux\n", "===\n", "This section will show you how to run your first Python program on a Linux computer." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Previous: Programming Environment](http://nbviewer.ipython.org/urls/raw.github.com/ehmatthes/intro_programming/master/notebooks/programming_environment.ipynb) | \n", "[Home](http://nbviewer.ipython.org/urls/raw.github.com/ehmatthes/intro_programming/master/notebooks/index.ipynb) | \n", "[Next: Hello World](http://nbviewer.ipython.org/urls/raw.github.com/ehmatthes/intro_programming/master/notebooks/hello_world.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Contents\n", "===\n", "- [Python on Linux](#Python-on-Linux)\n", "- [Installing Python](#Installing-Python)\n", " - [Python 3](#Python-3)\n", " - [Installing Python 3](#Installing-Python-3)\n", "- [Installing Geany](#Installing-Geany)\n", " - [Configuring Geany to use Python 3](#Configuring-Geany-to-use-Python-3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python on Linux\n", "===\n", "Python on Linux is pretty straightforward, because the people who develop Linux want you to write your own programs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Installing Python\n", "===\n", "Python is probably already installed on your system. To find out if it is installed, open a terminal and type the word `python`. You'll probably see output that looks something like this:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " $ python\n", " Python 2.7.6 (default, Mar 22 2014, 22:59:38) \n", " [GCC 4.8.2] on linux2\n", " Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", " >>> " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From this output, we can see that Python is installed, and the currently installed version is Python 2.7.6. You're now running a Python terminal session. You can start typing Python commands here, and you'll see your output immediately:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " >>> print(\"Hello Python world!\")\n", " Hello Python world!\n", " >>> " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To exit the Python session and return to a terminal prompt, enter Control-D." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python 3\n", "---\n", "Python 3 is also installed on many newer Linux distributions. To see if you also have Python 3, enter `python3` at a terminal prompt:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " $ python3\n", " Python 3.4.0 (default, Apr 11 2014, 13:05:18) \n", " [GCC 4.8.2] on linux\n", " Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", " >>>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Installing Python 3\n", "---\n", "If your system doesn't have Python 3 installed, you can install it yourself. Here's how you can get Python 3 running on Ubuntu 12.04:\n", "\n", "Add the \"deadsnakes\" package archive (ppa) to your system. This archive has a number of older and newer versions of Python, including Python 3.4.\n", "\n", " sudo apt-get install python-software-properties\n", " sudo add-apt-repository ppa:fkrull/deadsnakes\n", " sudo apt-get update\n", " sudo apt-get install python3.4\n", " \n", "Now that Python 3.4 is installed on your system, you have two options. You can start a default Python 2.7 session by running the command 'python' in a terminal. You can start a Python 3.3 session by running the command 'python3.4' in a terminal." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " $ python3.4\n", " Python 3.4.0 (default, Apr 11 2014, 13:05:18) \n", " [GCC 4.8.2] on linux\n", " Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", " >>>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[top](#)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Installing Geany\n", "===\n", "Geany is a simple text editor, which makes it easy to run Python programs. Output is displayed in a separate terminal window, which gets you used to working in terminals as well." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Open a terminal, and install the package 'geany': `sudo apt-get install geany`\n", "- Press the windows button, and type 'geany'.\n", "- Drag the geany icon to the task bar on the left side of the screen. This creates a shortcut you can use to start geany.\n", "- Write a [Hello World](http://nbviewer.ipython.org/urls/raw.github.com/ehmatthes/intro_programming/master/notebooks/hello_world.ipynb) program, and save it as 'hello.py'.\n", "- There are three ways you can run a program in Geany:\n", " - Build > Execute\n", " - Press F5\n", " - Click the icon with three gears on it\n", "- You should see a terminal window pop up, with your output in it:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Hello Python world!\n", "\n", "\n", " ------------------\n", " (program exited with code: 0)\n", " Press return to continue\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Configuring Geany to use Python 3\n", "---\n", "You may have to configure Geany to use Python 3.\n", "\n", "Open Geany, and open a Python [Hello World](http://nbviewer.ipython.org/urls/raw.github.com/ehmatthes/intro_programming/master/notebooks/hello_world.ipynb) program. If you don't have one on your system, write one and save it as *hello.py*, and run the program. This makes sure Geany is trying to run Python programs. When you have a running *hello.py* program, go to Build >> Set Build Commands.\n", "\n", "Under 'Python commands', look for the 'Compile' line. Enter the following in the 'Command' box. Make sure you get the spaces right. You should have 'python3' followed by a space, and the rest of the command. If you have 'python 3', with a space between *python* and *3*, Geany will not be able to run your code.\n", "\n", " python3 -m py_compile \"%f\"\n", "\n", "Under 'Execute commands', look for the 'Execute' line. Enter the following in the 'Command' box, paying attention once again to the spaces.\n", "\n", " python3 \"%f\"\n", "\n", "Test your setup by running *hello.py* again." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[top](#)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - -\n", "[Previous: Programming Environment](http://nbviewer.ipython.org/urls/raw.github.com/ehmatthes/intro_programming/master/notebooks/programming_environment.ipynb) | \n", "[Home](http://nbviewer.ipython.org/urls/raw.github.com/ehmatthes/intro_programming/master/notebooks/index.ipynb) | \n", "[Next: Hello World](http://nbviewer.ipython.org/urls/raw.github.com/ehmatthes/intro_programming/master/notebooks/hello_world.ipynb)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }