Configuring Ubuntu for Python Development — How to Think Like a Computer Scientist: Learning with Python 3 (2024)

Note: the following instructions assume that you are connected tothe Internet and that you have both the

main

and

universe

package repositories enabled. All unix shell commands are assumed tobe running from your home directory ($HOME). Finally, any command thatbegins with

sudo

assums that you have administrative rights on yourmachine. If you do not — please ask your system administrator aboutinstalling the software you need.

What follows are instructions for setting up an Ubuntu 9.10 (Karmic) homeenvironment for use with this book. I use Ubuntu GNU/Linux for both developmentand testing of the book, so it is the only system about which I can personallyanswer setup and configuration questions.

In the spirit of software freedom and open collaboration, please contact me ifyou would like to maintain a similar appendix for your own favorite system. I’dbe more than happy to link to it or put it on the Open Book Project site,provided you agree to answer user feedback concerning it.

Thanks!

Jeffrey Elkner

Governor’s Career and Technical Academy in Arlington

Arlington, Virginia

Vim

Vim can be used very effectively for Pythondevelopment, but Ubuntu only comes with the vim-tiny package installed bydefault, so it doesn’t support color syntax highlighting or auto-indenting.

To use Vim, do the following:

  1. From the unix command prompt, run:

    $ sudo apt-get install vim-gnome
  2. Create a file in your home directory named .vimrc that contains thefollowing:

    syntax enablefiletype indent onset etset sw=4set smarttabmap <f2> :w\|!python %

When you edit a file with a .py extension, you should now have color systaxhighlighting and auto indenting. Pressing the key should run your program, andbring you back to the editor when the program completes.

To learn to use vim, run the following command at a unix commandprompt:

$ vimtutor

$HOME environment

The following creates a useful environment in your home directory foradding your own Python libraries and executable scripts:

  1. From the command prompt in your home directory, create bin andlib/python subdirectories by running the following commands:

    $ mkdir bin lib$ mkdir lib/python
  2. Add the following lines to the bottom of your .bashrc in your homedirectory:

    PYTHONPATH=$HOME/lib/pythonEDITOR=vimexport PYTHONPATH EDITOR

    This will set your prefered editor to Vim, add your own lib/pythonsubdirectory for your Python libraries to your Python path, and add your ownbin directory as a place to put executable scripts. You need to logout andlog back in before your local bin directory will be in your search path.

Making a Python script executable and runnable from anywhere

On unix systems, Python scripts can be made executable using the followingprocess:

  1. Add this line as the first line in the script:

    #!/usr/bin/env python3
  2. At the unix command prompt, type the following to make myscript.pyexecutable:

    $ chmod +x myscript.py
  3. Move myscript.py into your bin directory, and it will be runnable fromanywhere.

© Copyright 2012, Peter Wentworth, Jeffrey Elkner, Allen B. Downey and Chris Meyers. Created using Sphinx 1.1.3.

Configuring Ubuntu for Python Development — How to Think Like a Computer Scientist: Learning with Python 3 (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 5918

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.