Learn python in linux

Your First Program in Python 3 on Linux

This page tells you how to setup a Python programming environment for your Linux computer and provides a step-by-step guide for creating and running a simple «Hello, world» Python program. All of the software is freely available on the Web.

Overview

The Python programming environment required by this booksite consists of:

  • Python, that is, a Python compiler/interpreter.
  • The Python standard libraries.
  • IDLE, the Python Integrated DeveLopment Environment.
  • The Tkinter, NumPy, and Pygame libraries, which are used by the booksite programs that do graphics or audio processing.
  • The booksite library, that is, a set of modules that we developed specifically to support this booksite. You also will need the Python setuptools library to install the booksite library.
  • The terminal application that is bundled with your Linux distribution.

Downloading and Installing Python, Tkinter, NumPy, Pygame, and setuptools

Using your package manager (for example, Synaptic), download and install these packages: python3 , python3-pip , python3-tk , python3-numpy , python3-pygame , python3-setuptools , and idle3 . The package names may vary slightly across Linux distributions.

Note: It’s likely that your package manager will offer all of the required packages, with the possible exception of python3-pygame . If your package manager doesn’t offer python3-pygame , then first use your package manager to install all of the other required packages. Then open a terminal window and issue the python3 -m pip install -U pygame —user command. That command downloads and installs PyGame via pip , the Python-specific package manager.

Follow-up to the previous note: An alternative installation strategy is to use your package manager to install only the python-3 , python3-tk , idle3 , and python3-pip packages, and then use pip to install the others by issuing these commands in a terminal window:

python3 -m pip install -U numpy --user python3 -m pip install -U setuptools --user python3 -m pip install -U pygame --user

Perform these steps to test your installations:

Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>>

Downloading and Installing the Booksite Library

Perform these steps to download and install the booksite library:

  • Use your Web browser to download this introcs-1.0.zip file to your /home/yourusername/Downloads directory.
  • In your file manager, double click on the /home/yourusername/Downloads/introcs-1.0.zip file to unzip the file, thus creating a directory named /home/yourusername/Downloads/introcs-1.0 .
  • Open a terminal window.
  • At any time the terminal application has a working directory. Initially the working directory is /home/yourusername . In the terminal window issue the cd Downloads command to change your working directory to /home/yourusername/Downloads , and then issue the cd introcs-1.0 command to change your working directory to /home/yourusername/Downloads/introcs-1.0 . (Incidentally, the cd .. command changes your working directory to the «parent» of the current working directory.)
  • Issue the ls command to display the names of all files in your working directory. Make sure that a file named setup.py is in your working directory.
  • Issue the python3 setup.py install —user command. The computer copies the files defining the booksite modules to a directory where Python can find them, and writes status messages to your terminal window to indicate its progress.

Perform these steps to test your installation of the booksite library:

  • In the terminal window issue the python3 command.
  • At the Python >>> prompt, type the statement import stdio followed by the Enter key. If Python generates no error messages, then you have installed the booksite library properly.
  • At the Python >>> prompt, type exit() followed by the Enter key to exit Python.
  • Close the terminal window.

Configuring IDLE

So far you’ve downloaded and installed all of the software that you’ll need. You should perform one more step before creating your first program: configure the IDLE programming environment. Follow these instructions:

  • Open a Terminal window.
  • In the Terminal window issue the command idle3 .
  • Click on the Options → Configure IDLE menu item.
  • Click on the General tab.
  • Click on the Open Edit Window radio button.
  • Click on the Ok button.
  • Close the IDLE window.
  • Close the Terminal window.

Composing Your First Program

Having installed Python, the Python standard libraries, IDLE, Tkinter, NumPy, Pygame, and the booksite libraries, and having configured IDLE, you are ready to compose your first Python program. Perform these instructions:

  • Using your file manager, create a directory named /home/yourusername/hello .
  • Open a Terminal window.
  • Issue the cd hello command to make the /home/yourusername/hello directory your working directory.
  • Issue the command idle3 & to launch IDLE. Note the trailing ampersand. The trailing ampersand tells the computer to run the idle3 program in the background, thereby leaving your Terminal application free to handle additional commands while IDLE is running.
  • In IDLE, type the four-line Python program helloworld.py exactly as it appears below. Use the arrow keys, mouse, or touchpad to move within the text that you have typed. Use the Delete key to delete text. Be careful; the smallest typing mistake might cause the program to fail.
import stdio # Write 'Hello, World' to standard output. stdio.writeln('Hello, World')

Running Your First Program

The final step is to run your program. It is possible to run some Python programs from within IDLE, but you should run the programs associated with this booksite directly from a terminal window. To do that, perform these steps:

  • Within the same terminal window, issue the ls command to display the names of all files in the working directory. Confirm that the working directory contains your helloworld.py file.
  • Issue the python3 helloworld.py command to run your program. If the computer writes «Hello, World» to the terminal window, then the execution of your helloworld.py program was successful. If the computer instead writes error messages, then use your text editor to correct your program, and issue the python3 helloworld.py command again. Repeat until your program runs successfully. If your program runs successfully the first time you try, then intentionally introduce an error into your program, just so you get some experience with correcting errors.
  • Close the IDLE window.
  • Close the terminal window.

You now have installed and configured a reasonable Python environment, and have used it to compose and run a Python program. Congratulations! You are a Python programmer!

Downloading the Booksite Example Programs (optional)

We recommend that you download the booksite example programs, that is, the example Python programs that are presented incrementally throughout the booksite. Having done so, you can run those programs to help you learn about them. Perform these instructions:

  • Use your Web browser to download this introcs-python.zip file to your /home/yourusername/Downloads directory.
  • In your file manager, double-click on the /home/yourusername/Downloads/introcs-python.zip file, thus creating the /home/yourusername/Downloads/introcs-python directory containing the booksite example programs.

Then perform these steps to test your download of the booksite example programs:

  • Open a terminal window.
  • Issue the cd Downloads and cd introcs-python commands to make /home/yourusername/Downloads/introcs-python your working directory.
  • Issue the ls command. Confirm that the working directory contains a file named bouncingball.py .
  • Issue the python3 bouncingball.py command. If Python launches a stddraw window showing an animated bouncing ball, then your download of the booksite example programs was successful.
  • Close the stddraw window.
  • Close the terminal window.

Downloading the Booksite Example Data (optional)

We recommend that you download the booksite example data, that is, the data files used by the booksite example programs that are presented incrementally throughout the booksite. Perform these instructions:

  • Use your Web browser to download this introcs-data.zip file to your /home/yourusername/Downloads directory.
  • In your file manager, double-click on the /home/yourusername/Downloads/introcs-data.zip file, thus creating the /home/yourusername/Downloads/introcs-data directory containing the booksite example data files.

Downloading the Booksite Library: Part 2 (optional)

Previously on this page we described how to download and install the booksite library so Python can find it. Now we describe how to download the booksite library so you can find it — for the sake of studying the code that implements it, should you so desire. Perform these instructions:

  • Use your Web browser to download this stdlib-python.zip file to your /home/yourusername/Downloads directory.
  • In your file manager, double-click on the /home/yourusername/Downloads/stdlib-python.zip file, thus creating the /home/yourusername/Downloads/stdlib-python directory containing the booksite library.

We invite you to study the code that implements the booksite library. But don’t be concerned if some of the code is cryptic. The code that implements the booksite library uses some features of Python that are beyond the scope of the textbook and this booksite.

Q & A

Q. My Linux distribution doesn’t come with a package manager. Can I install the required software without one?

A.Yes, but the procedure for doing so depends upon which Linux distribution you’re using.

With DEB-based distributions (Debian, Ubuntu, etc.), you typically would use the apt-get command to download and install specified .deb files. The more primitive alternative is to download the .deb files using some other mechanism, and then install them using the dpkg command.

With RPM-based distributions (Red Hat, Fedora, CentOS, SUSE, etc.), you typically would use the yum command to download and install specified .rpm files. The more primitive alternative is to download the .rpm files using some other mechanism, and then install them using the rpm command.

With Ebuild-based distributions (Gentoo, etc.), you typically would use the emerge command to download source code.

Q. How do I break out of an infinite loop when running my program from the terminal?

A. Type Ctrl-c. That is, while pressing the Ctrl key, type the c key.

Q. I downloaded files using my browser, but can’t find them. Where are they?

A. Many browsers by default place downloaded files in the directory /home/yourusername/Downloads .

Q. Must I use IDLE to create my Python programs? Can I use some other text editor?

A. You need not use IDLE to create your Python programs; it is fine to use some other text editor. However if you do use some other text editor, then make sure you change its settings so it (1) uses a four-space indentation scheme, and (2) indents using spaces instead of tabs. The Wikipedia Comparison of text editors page provides summary descriptions of many text editors.

Источник

Читайте также:  Выключение linux через ssh
Оцените статью
Adblock
detector