blob: 44a0d29503411f2f195f3e4e988d1922a8dd9678 [file] [log] [blame]
Guido van Rossuma7925f11994-01-26 10:20:16 +00001Subject: FAQ: Python -- an object-oriented language
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002Newsgroups: comp.lang.python,comp.answers,news.answers
3Followup-to: comp.lang.python
Guido van Rossuma7925f11994-01-26 10:20:16 +00004From: guido@cwi.nl (Guido van Rossum)
Guido van Rossum7be34a81995-05-31 15:17:12 +00005Reply-to: guido@cnri.reston.va.us (Guido van Rossum)
Guido van Rossum05151e01995-09-28 13:24:46 +00006Expires: Wed, 1 Nov 1995 00:00:00 GMT
Guido van Rossume7d92d51995-10-11 18:06:54 +00007Supersedes: <DFMAv8.3Hp@cwi.nl>
Guido van Rossuma7925f11994-01-26 10:20:16 +00008Approved: news-answers-request@MIT.Edu
9
10Archive-name: python-faq/part1
Guido van Rossum07779351995-02-07 16:59:56 +000011Submitted-by: Guido van Rossum <guido@cwi.nl>
Guido van Rossume7d92d51995-10-11 18:06:54 +000012Version: 1.29
13Last-modified: 11 October 1995
Guido van Rossuma7925f11994-01-26 10:20:16 +000014
15This article contains answers to Frequently Asked Questions about
16Python (an object-oriented interpreted programming language -- see
17the answer to question 1.1 for a short overview).
18
Guido van Rossuma6c707c1995-01-02 17:32:28 +000019Copyright 1993-1995 Guido van Rossum. Unchanged electronic
Guido van Rossuma7925f11994-01-26 10:20:16 +000020redistribution of this FAQ is allowed. Printed redistribution only
21with permission of the author. No warranties.
22
23Author's address:
Guido van Rossume7d92d51995-10-11 18:06:54 +000024 Guido van Rossum
25 C.N.R.I.
26 1895 Preston White Drive
27 Reston, VA 22091
28 U.S.A.
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000029Email: <guido@python.org>, <guido@cnri.reston.va.us>, <guido@cwi.nl>
Guido van Rossuma7925f11994-01-26 10:20:16 +000030
31The latest version of this FAQ is available by anonymous ftp from
Guido van Rossume7d92d51995-10-11 18:06:54 +000032<URL:ftp://ftp.python.org/pub/doc/FAQ>. It will also be posted
Guido van Rossuma6c707c1995-01-02 17:32:28 +000033regularly to the newsgroups comp.answers <URL:news:comp.answers> and
34comp.lang.python <URL:news:comp.lang.python>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000035
Guido van Rossuma6c707c1995-01-02 17:32:28 +000036Many FAQs, including this one, are available by anonymous ftp
37<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/>. The name under
38which a FAQ is archived appears in the Archive-name line at the top of
39the article. This FAQ is archived as python-faq/part1
40<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/python-faq/part1>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000041
42There's a mail server on that machine which will send you files from
43the archive by e-mail if you have no ftp access. You send a e-mail
Guido van Rossuma6c707c1995-01-02 17:32:28 +000044message to <mail-server@rtfm.mit.edu> containing the single word help
45in the message body to receive instructions.
Guido van Rossuma7925f11994-01-26 10:20:16 +000046
47This FAQ is divided in the following chapters:
48
49 1. General information and availability
50 2. Python in the real world
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000051 3. Building Python and Other Known Bugs
Guido van Rossuma7925f11994-01-26 10:20:16 +000052 4. Programming in Python
53 5. Extending Python
54 6. Python's design
55 7. Using Python on non-UNIX platforms
56
57To find the start of a particular chapter, search for the chapter number
58followed by a dot and a space at the beginning of a line (e.g. to
59find chapter 4 in vi, type /^4\. /).
60
61Here's an overview of the questions per chapter:
62
63 1. General information and availability
64 1.1. Q. What is Python?
65 1.2. Q. Why is it called Python?
66 1.3. Q. How do I obtain a copy of the Python source?
67 1.4. Q. How do I get documentation on Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +000068 1.5. Q. Are there other ftp sites that mirror the Python distribution?
69 1.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +000070 1.7. Q. Is there a WWW page devoted to Python?
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +000071 1.8. Q. Is the Python documentation available on the WWW?
72 1.9. Q. Is there a book on Python, or will there be one out soon?
73 1.10. Q. Are there any published articles about Python that I can quote?
74 1.11. Q. Are there short introductory papers or talks on Python?
75 1.12. Q. How does the Python version numbering scheme work?
76 1.13. Q. How do I get a beta test version of Python?
Guido van Rossum05151e01995-09-28 13:24:46 +000077 1.14. Q. Are there copyright restrictions on the use of Python?
78 1.15. Q. Why was Python created in the first place?
79 1.16. Q. What happened to Tim Peters (the author of python-mode.el, and many
Guido van Rossum7be34a81995-05-31 15:17:12 +000080 entertaining and enlightening pieces of Python email)?
Guido van Rossuma7925f11994-01-26 10:20:16 +000081
82 2. Python in the real world
83 2.1. Q. How many people are using Python?
84 2.2. Q. Have any significant projects been done in Python?
85 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000086 2.4. Q. How stable is Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000087 2.5. Q. When will the next version be released?
88 2.6. Q. What new developments are expected for Python in the future?
89 2.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000090 2.8. Q. What is the future of Python?
Guido van Rossum05151e01995-09-28 13:24:46 +000091 2.9. Q. What is the PSA, anyway?
92 2.10. Q. How do I join the PSA?
93 2.11. Q. What are the benefits of joining the PSA?
Guido van Rossuma7925f11994-01-26 10:20:16 +000094
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000095 3. Building Python and Other Known Bugs
Guido van Rossum91f60831994-02-15 15:52:27 +000096 3.1. Q. Is there a test set?
97 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000098 operations, but when playing with floating point operations I cannot
99 find anything wrong with them.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000100 3.3. Q. Link errors after rerunning the configure script.
101 3.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000102 script (after the script name).
Guido van Rossum7be34a81995-05-31 15:17:12 +0000103 3.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000104 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000105 3.6. Q. Python built with gcc for the DEC Alpha doesn't work.
106 3.7. Q. I use VPATH but some targets are built in the source directory.
107 3.8. Q. Trouble building or linking with the GNU readline library.
108 3.9. Q. Trouble building Python on Linux.
109 3.10. Q. Trouble with prototypes on Ultrix.
110 3.11. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
111 3.12. Q. Other trouble building Python on platform X.
112 3.13. Q. How to configure dynamic loading on Lixux.
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000113 3.14. Q. Under Solaris 2.x, using GCC, how do I use shared libraries?
114 3.15. Q. Errors when linking with a shared library containing C++ code.
Guido van Rossum05151e01995-09-28 13:24:46 +0000115 3.16. Q. Tk menus or radiobuttons don't work properly in Python 1.2.
116 3.17. Q. I built with tkintermodule.c enabled but get "Tkinter not found".
117 3.18. Q. I built with Tk 4.0 but Tkinter complains about the Tk version.
118 3.19. Q. Tk doesn't work right on DEC Alpha.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000119
120 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000121 4.1. Q. Is there a source code level debugger with breakpoints, step,
122 etc.?
123 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000124 C and others in Python (e.g. through inheritance)? (Also phrased as:
125 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000126 4.3. Q. Is there a curses/termcap package for Python?
127 4.4. Q. Is there an equivalent to C's onexit() in Python?
128 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000129 nested function seemingly can't access the local variables of the
130 outer function. What is going on? How do I pass local data to a
131 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000132 4.6. Q. How do I iterate over a sequence in reverse order?
133 4.7. Q. My program is too slow. How do I speed it up?
134 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000135 again (into the same Python process), the changes don't seem to take
136 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000137 4.9. Q. How do I find the current module name?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000138 4.10. Q. I have a module in which I want to execute some extra code
139 when it is run as a script. How do I find out whether I am running as
140 a script?
141 4.11. Q. I try to run a program from the Demo directory but it fails
142 with ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000143 4.12. Q. I have successfully built Python with STDWIN but it can't
144 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000145 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000146 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000147 4.15. Q. Is it possible to write obfuscated one-liners in Python?
148 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
149 4.17. Q. My class defines __del__ but it is not called when I delete the
150 object.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000151 4.18. Q. How do I change the shell environment for programs called
152 using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000153 4.19. Q. What is a class?
154 4.20. Q. What is a method?
155 4.21. Q. What is self?
156 4.22. Q. What is a unbound method?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000157 4.23. Q. How do I call a method defined in a base class from a derived
158 class that overrides it?
159 4.24. Q. How do I call a method from a base class without using the
160 name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000161 4.25. Q. How can I organize my code to make it easier to change the base
162 class?
163 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000164 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
165 4.28. Q. How can I create a stand-alone binary from a Python script?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000166 4.29. Q. What WWW tools are there for Python?
167 4.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +0000168 and output?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000169 4.31. Q. How do I call a function if I have the arguments in a tuple?
170 4.32. Q. How do I enable font-lock-mode for Python in Emacs?
171 4.33. Q. Is there an inverse to the format operator (a la C's scanf())?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000172
173 5. Extending Python
174 5.1. Q. Can I create my own functions in C?
175 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000176 5.3. Q. How can I execute arbitrary Python statements from C?
177 5.4. Q. How can I evaluate an arbitrary Python expression from C?
178 5.5. Q. How do I extract C values from a Python object?
179 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossum796b2591995-01-20 23:05:52 +0000180 5.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000181 Extensions manual?
182 5.8. Q. How do I call an object's method from C?
183 5.9. Q. How do I catch the output from print_error()?
184 5.10. Q. How do I access a module written in Python from C?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000185 5.11. Q. How do I interface to C++ objects from Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000186
187 6. Python's design
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000188 6.1. Q. Why isn't there a generic copying operation for objects in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000189 6.2. Q. Why isn't there a generic way to implement persistent objects
190 in Python? (Persistent == automatically saved to and restored from
191 disk.)
192 6.3. Q. Why isn't there a switch or case statement in Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +0000193 6.4. Q. Why does Python use indentation for grouping of statements?
Guido van Rossum3de27361994-07-25 14:19:33 +0000194 6.5. Q. Why are Python strings immutable?
195 6.6. Q. Why don't strings have methods like index() or sort(), like
196 lists?
197 6.7. Q. Why does Python use methods for some functionality
198 (e.g. list.index()) but functions for other (e.g. len(list))?
199 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or
200 files)?
201 6.9. Q. Why must 'self' be declared and used explicitly in method
202 definitions and calls?
Guido van Rossum061f1821994-10-06 16:03:45 +0000203 6.10. Q. Can't you emulate threads in the interpreter instead of
204 relying on an OS-specific thread implementation?
205 6.11. Q. Why can't lambda forms contain statements?
206 6.12. Q. Why is there no more efficient way of iterating over a dictionary
207 than first constructing the list of keys()?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000208 6.13. Q. Can Python be compiled to machine code, C or some other language?
Guido van Rossum05151e01995-09-28 13:24:46 +0000209 6.14. Q. Why doesn't Python use proper garbage collection?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000210
211 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000212 7.1. Q. Is there a Mac version of Python?
213 7.2. Q. Is there a DOS version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000214 7.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossum91f60831994-02-15 15:52:27 +0000215 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000216 7.5. Q. Is there a Windows 95 version of Python?
217 7.6. Q. Is there an OS/2 version of Python?
218 7.7. Q. Is there a VMS version of Python?
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000219 7.8. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossume530c581995-04-10 12:32:16 +0000220 7.9. Q. Where are the source or Makefiles for the non-UNIX versions?
221 7.10. Q. What is the status and support for the non-UNIX versions?
222 7.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000223 Where's the library?
Guido van Rossume530c581995-04-10 12:32:16 +0000224 7.12. Q. Where's the documentation for the Mac or PC version?
225 7.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000226 creating or editing programs apart from entering it interactively, and
227 there seems to be no way to save code that was entered interactively.
228 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000229
230To find a particular question, search for the question number followed
231by a dot, a space, and a Q at the beginning of a line (e.g. to find
232question 4.2 in vi, type /^4\.2\. Q/).
233
234
2351. General information and availability
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000236 =======================================
Guido van Rossuma7925f11994-01-26 10:20:16 +0000237
2381.1. Q. What is Python?
239
240A. Python is an interpreted, interactive, object-oriented programming
241language. It incorporates modules, exceptions, dynamic typing, very
242high level dynamic data types, and classes. Python combines
243remarkable power with very clear syntax. It has interfaces to many
244system calls and libraries, as well as to various window systems, and
245is extensible in C or C++. It is also usable as an extension language
246for applications that need a programmable interface. Finally, Python
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000247is portable: it runs on many brands of UNIX, on the Mac, and on PCs
248under MS-DOS, Windows, Windows NT, and OS/2.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000249
250To find out more, the best thing to do is to start reading the
251tutorial from the documentation set (see a few questions further
252down).
253
2541.2. Q. Why is it called Python?
255
Guido van Rossum796b2591995-01-20 23:05:52 +0000256A. Apart from being a computer scientist, I'm also a fan of "Monty
Guido van Rossuma7925f11994-01-26 10:20:16 +0000257Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000258the -- unlikely -- case you didn't know). It occurred to me one day
259that I needed a name that was short, unique, and slightly mysterious.
260And I happened to be reading some scripts from the series at the
261time... So then I decided to call my language Python. But Python is
262not a joke. And don't you associate it with dangerous reptiles
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000263either! (If you need an icon, use an image of the 16-ton weight from
264the TV series or of a can of SPAM :-)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000265
2661.3. Q. How do I obtain a copy of the Python source?
267
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000268A. The latest complete Python source distribution is always available
269by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000270<URL:ftp://ftp.python.org/pub/python/src/python1.2.tar.gz>. It is a
Guido van Rossume530c581995-04-10 12:32:16 +0000271gzipped tar file containing the complete C source, LaTeX
272documentation, Python library modules, example programs, and several
273useful pieces of freely distributable software. This will compile and
274run out of the box on most UNIX platforms. (See section 7 for
275non-UNIX information.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000276
277Occasionally a set of patches is issued which has to be applied using
278the patch program. These patches are placed in the same directory,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000279e.g. <URL:ftp://ftp.python.org/pub/python/src/patch1.1.1>. (At the time
Guido van Rossume530c581995-04-10 12:32:16 +0000280of writing, no patches exist for 1.2.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000281
282An index of said ftp directory can be found in the file INDEX. An
283HTML version of the index can be found in the file index.html,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000284<URL:ftp://ftp.python.org/pub/python/index.html>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000285
2861.4. Q. How do I get documentation on Python?
287
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000288A. The LaTeX source for the documentation is part of the source
289distribution. If you don't have LaTeX, the latest Python
290documentation set is always available by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000291<URL:ftp://ftp.python.org/pub/python/doc/postscript.tar.gz>. It is a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000292gzipped tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000293the library manual, and the tutorial. Note that the library manual is
294the most important one of the set, as much of Python's power stems
295from the standard or built-in types, functions and modules, all of
296which are described here. PostScript for a high-level description of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000297Python is in the file nluug-paper.ps (a separate file on the ftp
298site).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000299
Guido van Rossumc50158e1994-05-31 09:18:50 +00003001.5. Q. Are there other ftp sites that mirror the Python distribution?
301
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000302A. The following anonymous ftp sites keep mirrors of the Python
303distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000304
Guido van Rossume7d92d51995-10-11 18:06:54 +0000305USA:
306
307 <URL:ftp://ftp.python.org/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000308 <URL:ftp://gatekeeper.dec.com/pub/plan/python/>
Guido van Rossume7d92d51995-10-11 18:06:54 +0000309 <URL:ftp://ftp.uu.net/languages/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000310 <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
Guido van Rossume7d92d51995-10-11 18:06:54 +0000311 <URL:ftp://ftp.sterling.com/programming/languages/python/>
312 <URL:ftp://uiarchive.cso.uiuc.edu/pub/lang/python/>
313 <URL:ftp://ftp.pht.com/mirrors/python/python/>
314
315Europe:
316
317 <URL:ftp://ftp.cwi.nl/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000318 <URL:ftp://ftp.funet.fi/pub/languages/python/>
319 <URL:ftp://ftp.sunet.se/pub/lang/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000320 <URL:ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python/>
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000321 <URL:ftp://ftp.ibp.fr/pub/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000322 <URL:ftp://ftp.switch.ch/software/sources/python/>
323 <URL:ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python/>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000324
Guido van Rossume7d92d51995-10-11 18:06:54 +0000325Australia:
326
327 <URL:ftp://ftp.dstc.edu.au/pub/python/>
328
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000329Or try archie on the string "python".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000330
Guido van Rossumc50158e1994-05-31 09:18:50 +00003311.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000332
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000333A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
334and a mailing list. The newsgroup and mailing list are gatewayed into
335each other -- if you can read news it's unnecessary to subscribe to
336the mailing list. Send e-mail to <python-list-request@cwi.nl> to
Guido van Rossume530c581995-04-10 12:32:16 +0000337(un)subscribe to the mailing list. Hypermail archives of (nearly)
338everything posted to the mailing list (and thus the newsgroup) are
339available on our WWW server,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000340<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
341are also available by ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000342<URL:ftp://ftp.python.org/pub/python/mail/mailinglist.gz>. The
Guido van Rossume530c581995-04-10 12:32:16 +0000343uncompressed versions of these files can be read with the standard
344UNIX Mail program ("Mail -f file") or with nn ("nn file"). To read
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000345them using MH, you could use "inc -file file". (The archival service
346has stopped archiving new articles around the end of April 1995. I
347hope to revive it on the PSA server www.python.org sometime in the
348future.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000349
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003501.7. Q. Is there a WWW page devoted to Python?
351
Guido van Rossum7be34a81995-05-31 15:17:12 +0000352A. Yes, <URL:http://www.python.org/> is the official Python home page.
353At the time of writing, this page is not yet completely operational;
354you may have a look at the old Python home page:
355<URL:http://www.cwi.nl/~guido/Python.html> or at the U.S. copy:
356<URL:http://www.python.org/~guido/Python.html>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000357
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003581.8. Q. Is the Python documentation available on the WWW?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000359
Guido van Rossum7be34a81995-05-31 15:17:12 +0000360A. Yes, see <URL:http://www.python.org/> (Python's home page). It
361contains pointers to hypertext versions of the whole documentation set
362(as hypertext, not just PostScript).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000363
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000364If you wish to browse this collection of HTML files on your own
365machine, it is available bundled up by anonymous ftp,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000366e.g. <URL:ftp://ftp.python.org/pub/python/doc/html.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000367
368An Emacs-INFO set containing the library manual is also available by
Guido van Rossum7be34a81995-05-31 15:17:12 +0000369ftp, e.g. <URL:ftp://ftp.python.org/pub/python/doc/lib-info.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000370
3711.9. Q. Is there a book on Python, or will there be one out soon?
372
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000373A. Mark Lutz is writing a Python book for O'Reilly and Associates, to
374be published early 1996. See the outline (in PostScript):
375<URL:http://www.python.org/workshops/1995-05/outlinep.eps>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000376
3771.10. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000378
379A. So far the only refereed and published article that describes
380Python in some detail is:
381
382 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
383 Servers Using the Python Programming Language", CWI Quarterly, Volume
384 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
385
386LaTeX source for this paper is available as part of the Python source
387distribution.
388
Guido van Rossum05151e01995-09-28 13:24:46 +0000389See also the next section (supposedly Aaron Watters' paper has been
390refereed).
391
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003921.11. Q. Are there short introductory papers or talks on Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000393
Guido van Rossum05151e01995-09-28 13:24:46 +0000394A. A recent, very entertaining introduction to Python is the tutorial by
395Aaron Watters in UnixWorld Online:
396
397 Aaron R. Watters: "The What, Why, Who, and Where of Python",
398 <URL:http://www.wcmh.com/uworld/archives/95/tutorial/005.html>
399
400An olded paper is:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000401
402 Guido van Rossum, "An Introduction to Python for UNIX/C
403 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000404 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000405
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000406PostScript for this paper and for the slides used for the accompanying
407presentation is available by ftp as
Guido van Rossum7be34a81995-05-31 15:17:12 +0000408<URL:ftp://ftp.python.org/pub/python/doc/nluug-paper.ps> and
409<URL:ftp://ftp.python.org/pub/python/doc/nluug-slides.ps>, respectively.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000410
411Slides for a talk on Python that I gave at the Usenix Symposium on
Guido van Rossum05151e01995-09-28 13:24:46 +0000412Very High Level Languages in Santa Fe, NM, USA in October 1994 are
Guido van Rossum7be34a81995-05-31 15:17:12 +0000413available as <URL:ftp://ftp.python.org/pub/python/doc/vhll-slides.ps>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000414
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00004151.12. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000416
Guido van Rossum05151e01995-09-28 13:24:46 +0000417A. Python versions are numbered A.B.C or A.B. A is the major version
418number -- it is only incremented for major changes in functionality or
419source structure. B is the minor version number, incremented for less
Guido van Rossum95f61a71994-01-26 17:23:37 +0000420earth-shattering changes to a release. C is the patchlevel -- it is
Guido van Rossum05151e01995-09-28 13:24:46 +0000421incremented for each new patch release. Not all releases have patch
422releases. Note that in the past, patches have added significant
423changes; in fact the changeover from 0.9.9 to 1.0.0 was the first time
424that either A or B changed!
Guido van Rossum95f61a71994-01-26 17:23:37 +0000425
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000426Beta versions have an additional suffix of "-beta-N" for some small
427number N. Note that (for instance) all versions labeled 1.2-beta-N
Guido van Rossum05151e01995-09-28 13:24:46 +0000428*precede* the actual release of 1.2. 1.3b1 is short for 1.3-beta-1.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000429
4301.13. Q. How do I get a beta test version of Python?
431
Guido van Rossum05151e01995-09-28 13:24:46 +0000432A. If there are any beta releases, they are published in the normal
433source directory (e.g. <URL:ftp://ftp.python.org/pub/python/src/>).
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000434
Guido van Rossum05151e01995-09-28 13:24:46 +00004351.14. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000436
437A. Hardly. You can do anything you want with the source, as long as
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000438you leave the copyrights in, and display those copyrights in any
439documentation about Python that you produce. Also, don't use the
440author's institute's name in publicity without prior written
441permission, and don't hold them responsible for anything (read the
442actual copyright for a precise legal wording).
443
444In particular, if you honor the copyright rules, it's OK to use Python
445for commercial use, to sell copies of Python in source or binary form,
446or to sell products that enhance Python or incorporate Python (or part
447of it) in some form. I would still like to know about all commercial
448use of Python!
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000449
Guido van Rossum05151e01995-09-28 13:24:46 +00004501.15. Q. Why was Python created in the first place?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000451
452A. Here's a *very* brief summary of what got me started:
453
454- I had extensive experience with implementing an interpreted language
455in the ABC group at CWI, and from working with this group I had
456learned a lot about language design. This is the origin of many
457Python features, including the use of indentation for statement
458grouping and the inclusion of very-high-level data types (although the
459details are all different in Python).
460
461- I had a number of gripes about the ABC language, but also liked many
462of its features. It was impossible to extend the ABC language (or its
463implementation) to remedy my complaints -- in fact its lack of
464extensibility was one of its biggest problems.
465
466- I had some experience with using Modula-2+ and talked with the
467designers of Modula-3 (and read the M3 report). M3 is the origin of
468the syntax and semantics used for exceptions, and some other Python
469features.
470
471- I was working in the Amoeba distributed operating system group at
472CWI. We needed a better way to do system administration than by
473writing either C programs or Bourne shell scripts, since Amoeba had
474its own system call interface which wasn't easily accessible from the
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000475Bourne shell. My experience with error handling in Amoeba made me
476acutely aware of the importance of exceptions as a programming
477language feature.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000478
479- It occurred to me that a scripting language with a syntax like ABC
480but with access to the Amoeba system calls would fill the need. I
481realized that it would be foolish to write an Amoeba-specific
482language, so I decided that I needed a language that was generally
483extensible.
484
485- During the 1989 Christmas holidays, I had a lot of time on my hand,
486so I decided to give it a try. During the next year, while still
487mostly working on it in my own time, Python was used in the Amoeba
488project with increasing success, and the feedback from colleagues made
489me add many early improvements.
490
491- In February 1991, after just over a year of development, I decided
492to post to USENET. The rest is in the Misc/HISTORY file.
493
Guido van Rossum05151e01995-09-28 13:24:46 +00004941.16. Q. What happened to Tim Peters (the author of python-mode.el, and many
Guido van Rossum7be34a81995-05-31 15:17:12 +0000495entertaining and enlightening pieces of Python email)?
496
497A. He worked at KSR (Kendall Square Research, a start-up building a
498new kind of massively parallel processor). When KSR folded down a
499couple of years ago, Tim lost his email access. He hasn't surfaced
500on the net since then.
501
Guido van Rossume7d92d51995-10-11 18:06:54 +0000502 Missing-him-too-ly yours...
Guido van Rossum7be34a81995-05-31 15:17:12 +0000503
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000504PS: support for Python's Emacs mode (Misc/python-mode.el in the
505distribution) has been taken up by Barry Warsaw. Questions about it
506should be mailed to <python-mode@python.org>.
507
Guido van Rossuma7925f11994-01-26 10:20:16 +0000508
5092. Python in the real world
510===========================
511
5122.1. Q. How many people are using Python?
513
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000514A. I don't know, but the maximum number of simultaneous subscriptions
515to the Python mailing list before it was gatewayed into the newsgroup
516was about 180 (several of which were local redistribution lists). I
517believe that many active Python users don't bother to subscribe to the
518list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000519is even less meaningful. I see new names on the newsgroup all the
520time and my best guess is that there are currently at least several
521thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000522
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000523Another statistic is the number of accesses to the Python WWW server.
524Have a look at <URL:http://www.python.org/stats/>.
525
Guido van Rossuma7925f11994-01-26 10:20:16 +00005262.2. Q. Have any significant projects been done in Python?
527
528A. Here at CWI (the home of Python), we have written a 20,000 line
529authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00005305,000 line multimedia teleconferencing tool, as well as many many
531smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000532
533The University of Virginia uses Python to control a virtual reality
534engine. Contact: Matt Conway <conway@virginia.edu>.
535
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000536The ILU project at Xerox PARC can generate Python glue for ILU
537interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
538
Guido van Rossumac3f2121995-04-10 11:53:42 +0000539The University of California, Irvine uses a student administration
540system called TELE-Vision written entirely in Python. Contact: Ray
541Price <rlprice@uci.edu>.
542
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000543See also the next question.
544
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000545If you have done a significant project in Python that you'd like to be
546included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000547
5482.3. Q. Are there any commercial projects going on using Python?
549
550A. Several companies have revealed to me that they are planning or
Guido van Rossum796b2591995-01-20 23:05:52 +0000551considering use of Python in a future product.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000552
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000553Sunrise Software has a product out using Python -- they use Python
Guido van Rossumac3f2121995-04-10 11:53:42 +0000554for a GUI management application and an SNMP network management
555application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000556
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000557Infoseek uses Python to implement their commercial WWW information
558retrieval service <URL:http://www.infoseek.com/>. Contact:
559<info@infoseek.com>.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000560
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000561Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
562Contact: <Paul.Everitt@cminds.com>. Or see their WWW server
563<URL:http://www.cminds.com/>.
564
Guido van Rossumac3f2121995-04-10 11:53:42 +0000565KaPRE in Boulder, CO is using Python for on-site customization of C++
566applications, rapid-prototyping/development,
567language-based-components, and possibly more. This is pretty solid:
568Python's being shipped with their tool-set now, to beta sites.
569Contact: <lutz@KaPRE.COM> (Mark Lutz).
570
571Individuals at many other companies are using Python for internal
572development or for as yet unannounced products (witness their
573contributions to the Python mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000574
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000575SGI has advertised in the Python list looking for Python programmers
576for a project involving interactive television.
577
578See also the workshop minutes at
579<URL:http://www.python.org/workshops/> -- in general the WWW server is
580more up to date than the FAQ for these issues.
581
Guido van Rossuma7925f11994-01-26 10:20:16 +0000582Python has also been elected as an extension language by MADE, a
583consortium supported by the European Committee's ESPRIT program and
584consisting of Bull, CWI and some other European companies. Contact:
585Ivan Herman <ivan@cwi.nl>.
586
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000587If you'd like to be included in the list above, send me email!
588
Guido van Rossum95f61a71994-01-26 17:23:37 +00005892.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000590
Guido van Rossum3de27361994-07-25 14:19:33 +0000591A. Very stable. While the current version number would suggest it is
592in the early stages of development, in fact new, stable releases
Guido van Rossume7d92d51995-10-11 18:06:54 +0000593(numbered 0.9.x through 1.3) have been coming out roughly every 3 to
Guido van Rossuma6c707c1995-01-02 17:32:28 +00005946 months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000595
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005962.5. Q. When will the next version be released?
597
Guido van Rossume7d92d51995-10-11 18:06:54 +0000598A. Version 1.3 is being released on 13 October 1995. It is too early
599to predict when the next release will be necessary, but you can expect
600something awesome within half a year!
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000601
6022.6. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000603
Guido van Rossum7be34a81995-05-31 15:17:12 +0000604A. See my Work-In-Progress web page, currently at
605<URL:http://www.python.org:~guido/WIP.html>, and the pages for the
606Second Python Workshop (best reached via the Python home page,
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000607<URL:http://www.python.org/>). Also follow the newsgroup discussions!
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000608
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00006092.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000610
611A. In general, no. There are already millions of lines of Python code
612around the world, so any changes in the language that invalidates more
613than a very small fraction of existing programs has to be frowned
614upon. Even if you can provide a conversion program, there still is
615the problem of updating all documentation. Providing a gradual
616upgrade path is the only way if a feature has to be changed.
617
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00006182.8. Q. What is the future of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000619
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000620A. If I knew, I'd be rich :-)
621
622Seriously, the formation of the PSA (Pyton Software Activity, see
623<URL:http://www.python.org/psa/>) ensures some kind of support even in
624the (unlikely! event that I'd be hit by a bus (actually, here in the
625US, a car accident would be more likely :-), were to join a nunnery,
626or would be head-hunted. A large number of Python users have become
627experts at Python programming as well as maintenance of the
628implementation, and would easily fill the vacuum created by my
629disappearance.
630
631In the mean time, I have no plans to disappear -- rather, I am
632committed to improving Python, and my current benefactor, CNRI (see
633<URL:http://www.cnri.reston.va.us>) is just as committed to continue
634its support of Python and the PSA. In fact, we have great plans for
635Python -- we just can't tell yet!
636
Guido van Rossum05151e01995-09-28 13:24:46 +00006372.9. Q. What is the PSA, anyway?
638
639A. The Python Software Activity <URL:http://www.python.org/psa/> was
640created by a number of Python aficionados who want Python to be more
641than the product and responsibility of a single individual. It has
642found a home at CNRI <URL:http://www.cnri.reston.va.us>. Anybody who
643wishes Python well should join the PSA.
644
6452.10. Q. How do I join the PSA?
646
647A. The full scoop is available on the web, see
648<URL:http://www.python.org/psa/Joining.html>. Summary: send a check
649of at least $50 to CNRI/PSA, 1895 Preston White Drive, Suite 100, in
650Reston, VA 22091. Full-time students pay $25. Companies can join for
651a mere $500.
652
6532.11. Q. What are the benefits of joining the PSA?
654
655A. Like National Public Radio, if not enough people join, Python will
656wither. Your name will be mentioned on the PSA's web server.
657Workshops organized by the PSA <URL:http://www.python.org/workshops/>
658are only accessible to PSA members (you can join at the door). The
659PSA is working on additional benefits, such as reduced prices for
660books and software, and early access to beta versions of Python.
661
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000662
6633. Building Python and Other Known Bugs
664=======================================
Guido van Rossuma7925f11994-01-26 10:20:16 +0000665
Guido van Rossum91f60831994-02-15 15:52:27 +00006663.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000667
668A. Yes, simply do "import testall" (or "import autotest" if you aren't
669interested in the output). The standard modules whose name begins
670with "test" together comprise the test. The test set doesn't test
671*all* features of Python but it goes a long way to confirm that a new
672port is actually working. The Makefile contains an entry "make test"
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000673which runs the autotest module. NOTE: if "make test" fails, run the
674tests manually ("import testall") to see what goes wrong before
675reporting the error.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000676
Guido van Rossum91f60831994-02-15 15:52:27 +00006773.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000678operations, but when playing with floating point operations I cannot
679find anything wrong with them.
680
681A. The test set makes occasional unwarranted assumptions about the
682semantics of C floating point operations. Until someone donates a
683better floating point test set, you will have to comment out the
684offending floating point tests and execute similar tests manually.
685
Guido van Rossum7be34a81995-05-31 15:17:12 +00006863.3. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000687
688A. It is generally necessary to run "make clean" after a configuration
689change.
690
Guido van Rossum7be34a81995-05-31 15:17:12 +00006913.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000692script (after the script name).
693
694A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000695Don't. The reason for the complaint is that GNU getopt, unlike System
696V getopt and other getopt implementations, doesn't consider a
697non-option to be the end of the option list. A quick (and compatible)
698fix for scripts is to add "--" to the interpreter, like this:
699
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000700 #! /usr/local/bin/python --
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000701
702You can also use this interactively:
703
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000704 python -- script.py [options]
705
706Note that a working getopt implementation is provided in the Python
707distribution (in Python/getopt.c) but not automatically used.
Guido van Rossum24349991994-02-02 14:12:45 +0000708
Guido van Rossum7be34a81995-05-31 15:17:12 +00007093.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000710glmodule.c, but python hasn't been built or installed yet.
711
712A. Comment out the line mentioning glmodule.c in Setup and build a
713python without gl first; install it or make sure it is in your $PATH,
714then edit the Setup file again to turn on the gl module, and make
715again. You don't need to do "make clean"; you do need to run "make
716Makefile" in the Modules subdirectory (or just run "make" at the
717toplevel).
718
Guido van Rossum7be34a81995-05-31 15:17:12 +00007193.6. Q. Python built with gcc for the DEC Alpha doesn't work.
Guido van Rossum3de27361994-07-25 14:19:33 +0000720
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000721People have reported problems with gcc 2.5.8 up to 2.6.3. The DEC
722OSF/1 cc compiler does not have these problems so it's likely a gcc
Guido van Rossume530c581995-04-10 12:32:16 +0000723bug. The latest news is that this has been fixed in Python 1.2 by a
724source change (I gave up waiting for a fixed gcc).
Guido van Rossum3de27361994-07-25 14:19:33 +0000725
Guido van Rossum7be34a81995-05-31 15:17:12 +00007263.7. Q. I use VPATH but some targets are built in the source directory.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000727
728A. On some systems (e.g. Sun), if the target already exists in the
729source directory, it is created there instead of in the build
730directory. This is usually because you have previously built without
731VPATH. Try running "make clobber" in the source directory.
732
Guido van Rossum7be34a81995-05-31 15:17:12 +00007333.8. Q. Trouble building or linking with the GNU readline library.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000734
Guido van Rossumac3f2121995-04-10 11:53:42 +0000735A. Consider using readline 2.0. Some hints:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000736
737- You can use the GNU readline library to improve the interactive user
738interface: this gives you line editing and command history when
Guido van Rossum796b2591995-01-20 23:05:52 +0000739calling python interactively. You need to configure and build the GNU
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000740readline library before running the configure script. Its sources are
741no longer distributed with Python; you can ftp them from any GNU
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000742mirror site, or from its home site
743<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
744higher version number -- using version 1.x is not recommended). Pass
745the Python configure script the option --with-readline=DIRECTORY where
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000746DIRECTORY is the absolute pathname of the directory where you've built
747the readline library. Some hints on building and using the readline
748library:
749
750- On SGI IRIX 5, you may have to add the following
751to rldefs.h:
752
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000753 #ifndef sigmask
754 #define sigmask(sig) (1L << ((sig)-1))
755 #endif
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000756
757- On most systems, you will have to add #include "rldefs.h" to the
758top of several source files, and if you use the VPATH feature, you
759will have to add dependencies of the form foo.o: foo.c to the
760Makefile for several values of foo.
761
762- The readline library requires use of the termcap library. A
763known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000764cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000765conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000766STDWIN) by adding a line saying '#define werase w_erase' to the
767stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000768GL conflict has been solved in the Python configure script by a
769hack that forces use of the static version of the termcap library.
770
Guido van Rossumac3f2121995-04-10 11:53:42 +0000771- Check the newsgroup gnu.bash.bug <URL:news:gnu.bash.bug> for
772specific problems with the readline library (I don't read this group
773but I've been told that it is the place for readline bugs).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000774
Guido van Rossum7be34a81995-05-31 15:17:12 +00007753.9. Q. Trouble building Python on Linux.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000776
Guido van Rossumac3f2121995-04-10 11:53:42 +0000777A. If you're building Python 1.2, Slackware 2.2 has a buggy bash
778(version 1.14.3) which breaks a sed script that is used to build
779Modules/Makefile. Replace /bin/sh with /bin/ash in both makesetup and
780Makefile.pre.in.
781
782In 1.1 and 1.1.1, there's a bug in the reference counting logic of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000783ternary pow() which is only tripped by very picky mallocs, like the
784GNU malloc on Linux. This has been fixed in 1.2. To continue the
Guido van Rossumac3f2121995-04-10 11:53:42 +0000785tests in 1.1(.1), just disable the tests of pow() with three arguments
786from Lib/test/test_b2.py.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000787
Guido van Rossumac3f2121995-04-10 11:53:42 +0000788Apart from this, Python builds and runs fine on most Linux versions
789(if you run into trouble on an old Linux version, consider upgrading).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000790
Guido van Rossum7be34a81995-05-31 15:17:12 +00007913.10. Q. Trouble with prototypes on Ultrix.
Guido van Rossum72eb83c1994-10-07 11:33:28 +0000792
793A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
794HAVE_PROTOTYPES.
795
Guido van Rossum7be34a81995-05-31 15:17:12 +00007963.11. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000797
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000798A. (This often manifests itself as a weird error from the
799compileall.py script run by "make libinstall".)
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000800
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000801Don't use gcc, use the Next C compiler (cc). Even though it is
802derived from (an old version of) gcc, its interpretation of the
803"-posix" switch is different; in this particular case, cc is right and
804gcc is wrong.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000805
Guido van Rossum7be34a81995-05-31 15:17:12 +00008063.12. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000807
Guido van Rossum796b2591995-01-20 23:05:52 +0000808A. Please email the details to <guido@cwi.nl> and I'll look into it.
809Please provide as many details as possible. In particular, if you
810don't tell me what type of computer and what operating system (and
811version) you are using it will be difficult for me to figure out what
812is the matter. If you get a specific error message, please email it
813to me too.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000814
Guido van Rossum7be34a81995-05-31 15:17:12 +00008153.13. Q. How to configure dynamic loading on Lixux.
Guido van Rossum07779351995-02-07 16:59:56 +0000816
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000817A. There are two answers, depending on whether you are using the new
818ELF object format or not.
Guido van Rossum07779351995-02-07 16:59:56 +0000819
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000820For ELF, this seems to be the procedure (thanks to Martin von Loewis):
Guido van Rossum07779351995-02-07 16:59:56 +0000821
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000822Compile Python to an ELF binary.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000823
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000824In addition, you have to use the following flags:
Guido van Rossum07779351995-02-07 16:59:56 +0000825
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000826 - when linking python: -rdynamic -ldl
827 - when compiling an object that goes into a shared module: -fPIC
828 - when linking a shared module: -shared -ldl
Guido van Rossum07779351995-02-07 16:59:56 +0000829
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000830Furthermore, it appears that some Python releases did not understand
831that Linux has dynamic linking. Python 1.2 did it right, but you
832should check wether the generated config.h indicates the use of -ldl
833(i.e. dlopen, dlsym). Finally, you can load a shared module by saying
834'use foo'. Make sure the module is in your PYTHONPATH.
Guido van Rossum07779351995-02-07 16:59:56 +0000835
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000836For pre-ELF systems (thanks to Andrew Kuchling):
Guido van Rossum07779351995-02-07 16:59:56 +0000837
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000838Pre-ELF Linux requires that you use the GNU DLD library. The stages
839of using dynamic libraries on Linux are:
840
841 1) Get dld 3.2.5 from a Linux site. Be careful here; the most
842 recent GNU version is 3.2.3, and doesn't support Linux; be sure to
843 get it from a Linux mirror, not a GNU mirror (3.2.4 should also
844 work). Compile it and install the library libdld.a somewhere; I
845 used /usr/local/lib.
846
847 Suitable URLs for the dld distribution are currently:
848 <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.src.tar.gz> and
849 <URL:ftp://tsx-11.mit.edu/pub/linux/sources/libs/dld-3.2.5.src.tar.gz>.
850 There's also a binary distribution of it:
851 <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.bin.tar.gz>.
852
853 2) Get Jack Jansen's DL library; its location is given in the
854 _Extending Python_ manual as <URL:ftp://ftp.cwi.nl/pub/dynload/>.
855 Compile it and install libdl.a wherever you put libdld.a.
856
857 3) Run Python's configure script, giving it the --with-dl-dld option,
858 which requires a parameter giving the directory where you put the
859 libraries.
860
861 4) Recompile Python.
862
8633.14. Q. Under Solaris 2.x, using GCC, how do I use shared libraries?
864
865A. Use the linker in /usr/ucb/ld, not the GNU linker. The latter
866cannot create shared libraries.
867
8683.15. Q. Errors when linking with a shared library containing C++ code.
869
870A. Link the main Python binary with C++. Change the definition of
871LINKCC in Modules/Makefile to be your C++ compiler. You may have to
872edit config.c slightly to make it compilable with C++.
Guido van Rossum07779351995-02-07 16:59:56 +0000873
Guido van Rossum05151e01995-09-28 13:24:46 +00008743.16. Q. Tk menus or radiobuttons don't work properly in Python 1.2.
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000875
876A. There's a bug in Tkinter.py; remove ClassType and InstanceType from
877the definition of CallableTypes near the top of Lib/tkinter/Tkinter.py.
878
8793.17. Q. I built with tkintermodule.c enabled but get "Tkinter not found".
880
881A. Tkinter.py (note: upper case T) lives in a subdirectory of Lib,
882Lib/tkinter. If you are using the default module search path, you
883probably didn't enable the line in the Modules/Setup file defining
884TKPATH; if you use the environment variable PYTHONPATH, you'll have to
885add the proper tkinter subdirectory.
886
8873.18. Q. I built with Tk 4.0 but Tkinter complains about the Tk version.
888
889A. Several things could cause this. You most likely have a Tk 3.6
890installation that wasn't completely eradicated by the Tk 4.0
891installation (which tends to add "4.0" to its installed files). You
892may have the Tk 3.6 support library installed in the place where the
893Tk 4.0 support files should be (default /usr/local/lib/tk/); you may
894have compiled Python with the old tk.h header file (yes, this actually
895compiles!); you may actually have linked with Tk 3.6 even though Tk
8964.0 is also around. Similar for Tcl 7.4 vs. Tcl 7.3.
897
8983.19. Q. Tk doesn't work right on DEC Alpha.
899
900A. You probably compiled either Tcl, Tk or Python with gcc. Don't.
901For this platform, which has 64-bit integers, gcc is known to generate
902broken code. The standard cc (which comes bundled with the OS!)
903works. If you still prefer gcc, at least try recompiling with cc
904before reporting problems to the newsgroup or the author; if this
905fixes the problem, report the bug to the gcc developers instead. (As
906far as we know, there are no problem with gcc on other platforms --
907the instabilities seem to be restricted to the DEC Alpha.) See also
908question 3.6.
909
Guido van Rossuma7925f11994-01-26 10:20:16 +0000910
9114. Programming in Python
912========================
913
Guido van Rossum24349991994-02-02 14:12:45 +00009144.1. Q. Is there a source code level debugger with breakpoints, step,
915etc.?
916
917A. Yes. Check out module pdb; pdb.help() prints the documentation (or
918you can read it as Lib/pdb.doc). If you use the STDWIN option,
919there's also a windowing interface, wdb. You can write your own
920debugger by using the code for pdb or wdb as an example.
921
9224.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000923C and others in Python (e.g. through inheritance)? (Also phrased as:
924Can I use a built-in type as base class?)
925
926A. No, but you can easily create a Python class which serves as a
927wrapper around a built-in object, e.g. (for dictionaries):
928
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000929 # A user-defined class behaving almost identical
930 # to a built-in dictionary.
931 class UserDict:
932 def __init__(self): self.data = {}
933 def __repr__(self): return repr(self.data)
934 def __cmp__(self, dict):
935 if type(dict) == type(self.data):
936 return cmp(self.data, dict)
937 else:
938 return cmp(self.data, dict.data)
939 def __len__(self): return len(self.data)
940 def __getitem__(self, key): return self.data[key]
941 def __setitem__(self, key, item): self.data[key] = item
942 def __delitem__(self, key): del self.data[key]
943 def keys(self): return self.data.keys()
944 def items(self): return self.data.items()
945 def values(self): return self.data.values()
946 def has_key(self, key): return self.data.has_key(key)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000947
Guido van Rossum24349991994-02-02 14:12:45 +00009484.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000949
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000950A. Yes -- Lance Ellinghaus has written a module that interfaces to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000951System V's "ncurses". If you know a little curses and some Python,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000952it's straightforward to use. It is part of the standard Python
953distribution, but not configured by default -- you must enable it by
954editing Modules/Setup. It requires a System V curses implementation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000955
956You could also consider using the "alfa" (== character cell) version
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000957of STDWIN. (Standard Window System Interface, a portable windowing
958system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000959will also prepare your program for porting to windowing environments
960such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000961
Guido van Rossum24349991994-02-02 14:12:45 +00009624.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000963
964A. Yes, if you import sys and assign a function to sys.exitfunc, it
965will be called when your program exits, is killed by an unhandled
966exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
967
Guido van Rossum24349991994-02-02 14:12:45 +00009684.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000969nested function seemingly can't access the local variables of the
970outer function. What is going on? How do I pass local data to a
971nested function?
972
973A. Python does not have arbitrarily nested scopes. When you need to
974create a function that needs to access some data which you have
975available locally, create a new class to hold the data and return a
976method of an instance of that class, e.g.:
977
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000978 class MultiplierClass:
979 def __init__(self, factor):
980 self.factor = factor
981 def multiplier(self, argument):
982 return argument * self.factor
Guido van Rossuma7925f11994-01-26 10:20:16 +0000983
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000984 def generate_multiplier(factor):
985 return MultiplierClass(factor).multiplier
Guido van Rossuma7925f11994-01-26 10:20:16 +0000986
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000987 twice = generate_multiplier(2)
988 print twice(10)
989 # Output: 20
990
991An alternative solution uses default arguments, e.g.:
992
993 def generate_multiplier(factor):
994 def multiplier(arg, fact = factor):
995 return arg*fact
996 return multiplier
997
998 twice = generate_multiplier(2)
999 print twice(10)
1000 # Output: 20
Guido van Rossuma7925f11994-01-26 10:20:16 +00001001
Guido van Rossum24349991994-02-02 14:12:45 +000010024.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001003
1004A. If it is a list, the fastest solution is
1005
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001006 list.reverse()
1007 try:
1008 for x in list:
1009 "do something with x"
1010 finally:
1011 list.reverse()
Guido van Rossuma7925f11994-01-26 10:20:16 +00001012
1013This has the disadvantage that while you are in the loop, the list
1014is temporarily reversed. If you don't like this, you can make a copy.
1015This appears expensive but is actually faster than other solutions:
1016
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001017 rev = list[:]
1018 rev.reverse()
1019 for x in rev:
1020 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +00001021
1022If it isn't a list, a more general but slower solution is:
1023
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001024 i = len(list)
1025 while i > 0:
1026 i = i-1
1027 x = list[i]
1028 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +00001029
1030A more elegant solution, is to define a class which acts as a sequence
1031and yields the elements in reverse order (solution due to Steve
1032Majewski):
1033
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001034 class Rev:
1035 def __init__(self, seq):
1036 self.forw = seq
1037 def __len__(self):
1038 return len(self.forw)
1039 def __getitem__(self, i):
1040 return self.forw[-(i + 1)]
Guido van Rossuma7925f11994-01-26 10:20:16 +00001041
1042You can now simply write:
1043
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001044 for x in Rev(list):
1045 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +00001046
Guido van Rossum061f1821994-10-06 16:03:45 +00001047Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +00001048call overhead...
1049
Guido van Rossum24349991994-02-02 14:12:45 +000010504.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001051
1052A. That's a tough one, in general. There are many tricks to speed up
1053Python code; I would consider rewriting parts in C only as a last
1054resort. One thing to notice is that function and (especially) method
1055calls are rather expensive; if you have designed a purely OO interface
1056with lots of tiny functions that don't do much more than get or set an
1057instance variable or call another method, you may consider using a
1058more direct way, e.g. directly accessing instance variables. Also see
1059the standard module "profile" (described in the file
1060"python/lib/profile.doc") which makes it possible to find out where
1061your program is spending most of its time (if you have some patience
1062-- the profiling itself can slow your program down by an order of
1063magnitude).
1064
Guido van Rossum24349991994-02-02 14:12:45 +000010654.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +00001066again (into the same Python process), the changes don't seem to take
1067place. What is going on?
1068
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001069A. For reasons of efficiency as well as consistency, Python only reads
Guido van Rossum796b2591995-01-20 23:05:52 +00001070the module file on the first time a module is imported. (Otherwise a
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001071program consisting of many modules, each of which imports the same
Guido van Rossum796b2591995-01-20 23:05:52 +00001072basic module, would read the basic module over and over again.) To
1073force rereading of a changed module, do this:
Guido van Rossuma7925f11994-01-26 10:20:16 +00001074
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001075 import modname
1076 reload(modname)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001077
1078Warning: this technique is not 100% fool-proof. In particular,
1079modules containing statements like
1080
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001081 from modname import some_objects
Guido van Rossuma7925f11994-01-26 10:20:16 +00001082
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001083will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001084
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000010854.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +00001086
Guido van Rossum061f1821994-10-06 16:03:45 +00001087A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +00001088(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001089'__main__' you are running as a script.
1090
Guido van Rossuma6c707c1995-01-02 17:32:28 +000010914.10. Q. I have a module in which I want to execute some extra code
1092when it is run as a script. How do I find out whether I am running as
1093a script?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001094
1095A. See the previous question. E.g. if you put the following on the
1096last line of your module, main() is called only when your module is
1097running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +00001098
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001099 if __name__ == '__main__': main()
Guido van Rossum24349991994-02-02 14:12:45 +00001100
Guido van Rossuma6c707c1995-01-02 17:32:28 +000011014.11. Q. I try to run a program from the Demo directory but it fails
1102with ImportError: No module named ...; what gives?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001103
Guido van Rossum061f1821994-10-06 16:03:45 +00001104A. This is probably an optional module (written in C!) which hasn't
1105been configured on your system. This especially happens with modules
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001106like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
1107many other modules, see Modules/Setup.in for info on how to add these
1108modules to your Python, if it is possible at all. Sometimes you will
1109have to ftp and build another package first (e.g. STDWIN). Sometimes
1110the module only works on specific platforms (e.g. gl only works on SGI
1111machines).
1112
1113NOTE: if the complaint is about "Tkinter" (upper case T) and you have
1114already configured module "tkinter" (lower case t), the solution is
1115*not* to rename tkinter to Tkinter or vice versa. There is probably
Guido van Rossum796b2591995-01-20 23:05:52 +00001116something wrong with your module search path. Check out the value of
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001117sys.path.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001118
Guido van Rossum061f1821994-10-06 16:03:45 +00001119For X-related modules (Xt and Xm) you will have to do more work: they
1120are currently not part of the standard Python distribution. You will
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001121have to ftp the Extensions tar file, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +00001122<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz> and follow
1123the instructions there.
Guido van Rossum061f1821994-10-06 16:03:45 +00001124
1125See also the next question.
1126
11274.12. Q. I have successfully built Python with STDWIN but it can't
1128find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001129
1130A. There's a subdirectory of the library directory named 'stdwin'
1131which should be in the default module search path. There's a line in
1132Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +00001133unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001134STDWIN-related lines so it's easy to miss it.
1135
11364.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001137
1138A. Depending on what platform(s) you are aiming at, there are several.
1139
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001140- There's a neat object-oriented interface to the Tcl/Tk widget set,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001141called Tkinter. As of python 1.1, it is part of the standard Python
1142distribution -- all you need to do is enable it in Modules/Setup
1143(provided you have already installed Tk and Tcl). This is probably
1144the easiest to install and use, and the most complete widget set. It
1145is also very likely that in the future the standard Python GUI API
1146will be based on or at least look very much like the Tkinter
1147interface. For more info about Tk, including pointers to the source,
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001148see the Tcl/Tk home page <URL:http://www.sunlabs.com/research/tcl/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001149
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001150- The standard Python distribution comes with an interface to STDWIN,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001151a platform-independent low-level windowing interface. You have to ftp
1152the source for STDWIN separately,
1153e.g. <URL:ftp://ftp.cwi.nl/pub/stdwin/> or gatekeeper.dec.com in
1154pub/misc/stdwin <URL:ftp://gatekeeper.dec.com/pub/misc/stdwin/>.
1155STDWIN runs under X11 or the Mac; a Windows port has been attempted
1156but I can't seem to get it working. Note that STDWIN is really not
1157powerful enough to implement a modern GUI (no widgets, etc.) and that
1158I don't have the time to maintain or extend it, so you may be better
1159off using Tkinter or the Motif interface, unless you require
1160portability to the Mac (which is also offered by SUIT, by the way --
1161see below).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001162
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001163- For SGI IRIX only, there's an interface to the complete GL (Graphics
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001164Library -- low level but very good 3D capabilities) as well as to
1165FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001166Overmars -- ftp'able from <URL:ftp://ftp.cs.ruu.nl/pub/SGI/FORMS/>).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001167
1168- There's an interface to X11, including the Athena and Motif widget
1169sets (and a few individual widgets, like Mosaic's HTML widget and
1170SGI's GL widget) in the Extensions set, which is separately ftp'able
Guido van Rossum7be34a81995-05-31 15:17:12 +00001171<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001172
1173- There's an interface to SUIT, the U of Virginia's Simple User
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001174Interface Toolkit; it can be ftp'ed from
1175<URL:ftp://uvacs.cs.virginia.edu/pub/suit/python/SUIT_python.tar.Z>.
1176A PC binary of Python 1.0.2 compiled with DJGPP and with SUIT support
1177built-in has been made available by Antonio Costa
1178<URL:ftp://asterix.inescn.pt/pub/PC/python/pyt102su.exe> (a
1179self-extracting archive). Note that the UVa people themselves have
1180expressed doubts about SUIT, and are planning to build a Python GUI
1181API based upon Tk (though not necessarily on Tkinter); see
1182<URL:http://server.cs.virginia.edu/~tnb2d/IT/IT.html>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001183
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001184- There's an interface to WAFE, a Tcl interface to the X11 Motif and
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001185Athena widget sets. Last I heard about it it was included in the WAFE
11861.0 prerelease
1187<URL:ftp://ftp.wu-wien.ac.at/pub/src/X11/wafe/wafe-1.0.tar.gz-prerelease>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001188
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001189- The NT port by Mark Hammond (see question 7.4) includes an interface
1190to the Microsoft Foundation Classes and a Python programming
1191environment using it that's written mostly in Python. See
1192<URL:ftp://ftp.python.org/pub/python/nt/>.
1193
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001194- There's an interface to wxWindows. wxWindows is a portable GUI
1195class library written in C++. It supports XView, Motif, MS-Windows as
1196targets. There is some support for Macs and CURSES as well. wxWindows
1197preserves the look and feel of the underlying graphics toolkit. See
1198the wxPython WWW page at
1199<URL:http://www.aiai.ed.ac.uk/~jacs/wx/wxpython/wxpython.html>.
1200
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001201- There's an object-oriented GUI based on the Microsoft Foundation
1202Classes model called WPY. Programs written in WPY run unchanged and
1203with native look and feel on NT, Windows 3.1 (using win32s) and on
1204Unix (using Tk). Source and binaries for NT and Linux are available
1205in <URL:ftp://ftp.python.org/pub/python/wpy>.
1206
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001207- Python has been mentioned on the "Futurism" subpage of the Fresco
1208home page <URL:http://www.faslab.com/fresco/HomePage.html>. "Pesto"
1209is a Python interface to the CORBA dynamic invocation interface, and
1210thus Fresco. A Pesto prototype is running and is currently being
Guido van Rossum7be34a81995-05-31 15:17:12 +00001211packaged up for inclusion in the Fresco snapshot. See also the Pesto
1212web pages: <URL:http://www.faslab.com/fresco/pesto/Index.html>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001213
Guido van Rossum061f1821994-10-06 16:03:45 +000012144.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001215
1216A. There's an interface to SYBASE by John Redford
1217<jredford@lehman.com>.
1218
Guido van Rossum796b2591995-01-20 23:05:52 +00001219There's an interface to metalbase by Lance Ellinghaus
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001220<lance@markv.com>; it is part of the separate Extensions distribution
Guido van Rossum7be34a81995-05-31 15:17:12 +00001221<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001222
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001223Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to
Guido van Rossume530c581995-04-10 12:32:16 +00001224mSQL (mini-SQL).
Guido van Rossum7be34a81995-05-31 15:17:12 +00001225<URL:ftp://ftp.python.org/pub/python/contrib/PymSQL.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001226
Guido van Rossum7be34a81995-05-31 15:17:12 +00001227Tom Culliton <culliton@clark.net> has written an Oracle module.
1228See <URL:ftp://ftp.clark.net/pub/culliton/oramod.tgz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001229
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000012304.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001231
1232A. Yes. See the following three examples, due to Ulf Bartelt:
1233
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001234 # Primes < 1000
1235 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
1236 map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001237
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001238 # First 10 Fibonacci numbers
1239 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
1240 range(10))
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001241
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001242 # Mandelbrot set
1243 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
1244 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
1245 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
1246 i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y
1247 >=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(
1248 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
1249 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
1250 # \___ ___/ \___ ___/ | | |__ lines on screen
1251 # V V | |______ columns on screen
1252 # | | |__________ maximum of "iterations"
1253 # | |_________________ range on y axis
1254 # |____________________________ range on x axis
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001255
1256Don't try this at home, kids!
1257
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000012584.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001259
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001260A. Not directly. In many cases you can mimic a?b:c with "a and b or
1261c", but there's a flaw: if b is zero (or empty, or None -- anything
1262that tests false) then c will be selected instead. In many cases you
1263can prove by looking at the code that this can't happen (e.g. because
1264b is a constant or has a type that can never be false), but in general
1265this can be a problem.
1266
1267Steve Majewski (or was it Tim Peters?) suggested the following
1268solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
1269is never false, so the wrong path is never taken; then applying [0] to
1270the whole thing gets the b or c that you really wanted. Ugly, but it
1271gets you there in the rare cases where it is really inconvenient to
1272rewrite your code using 'if'.
1273
12744.17. Q. My class defines __del__ but it is not called when I delete the
1275object.
1276
1277A. There are several possible reasons for this.
1278
1279- The del statement does not necessarily call __del__ -- it simply
1280decrements the object's reference count, and if this reaches zero
1281__del__ is called.
1282
1283- If your data structures contain circular links (e.g. a tree where
1284each child has a parent pointer and each parent has a list of
1285children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +00001286have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001287pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +00001288call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001289than once for the same object.
1290
1291- If the object has ever been a local variable (or argument, which is
1292really the same thing) to a function that caught an expression in an
1293except clause, chances are that a reference to the object still exists
1294in that function's stack frame as contained in the stack trace.
1295Normally, deleting (better: assigning None to) sys.exc_traceback will
1296take care of this. If you a stack was printed for an unhandled
1297exception in an interactive interpreter, delete sys.last_traceback
1298instead.
1299
1300- There is code that deletes all objects when the interpreter exits,
1301but if your Python has been configured to support threads, it is not
1302called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +00001303your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001304
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001305- Finally, there are some obscure bugs if your __del__ method does
1306complicated things such as updating dictionaries or lists or
1307references globals. I hope to have fixed these in release 1.2.
1308
13094.18. Q. How do I change the shell environment for programs called
1310using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001311
1312A. Modifying the environment passed to subshells was left out of the
1313interpreter because there seemed to be no well-established portable
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001314way to do it (in particular, some systems, have putenv(), others have
1315setenv(), and some have none at all).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001316
1317However if all you want is to pass environment variables to the
1318commands run by os.system() or os.popen(), there's a simple solution:
1319prefix the command string with a couple of variable assignments and
Guido van Rossum796b2591995-01-20 23:05:52 +00001320export statements. The following would be universal for popen:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001321
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001322 import os
1323 from commands import mkarg # nifty routine to add shell quoting
1324 def epopen(cmd, mode, env = {}):
1325 # env is a dictionary of environment variables
1326 prefix = ''
Guido van Rossum796b2591995-01-20 23:05:52 +00001327 for key, value in env.items():
1328 prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:])
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001329 prefix = prefix + 'export %s\n' % key
1330 return os.popen(prefix + cmd, mode)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001331
13324.19. Q. What is a class?
1333
1334A. A class is the particular object type that is created by executing
Guido van Rossum796b2591995-01-20 23:05:52 +00001335a class statement. Class objects are used as templates, to create
1336class instance objects, which embody both the data structure and
1337program routines specific to a datatype.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001338
13394.20. Q. What is a method?
1340
1341A. A method is a function that you normally call as
Guido van Rossum796b2591995-01-20 23:05:52 +00001342x.name(arguments...) for some object x. The term is used for methods
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001343of classes and class instances as well as for methods of built-in
Guido van Rossum796b2591995-01-20 23:05:52 +00001344objects. (The latter have a completely different implementation and
1345only share the way their calls look in Python code.) Methods of
1346classes (and class instances) are defined as functions inside the
1347class definition.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001348
13494.21. Q. What is self?
1350
1351A. Self is merely a conventional name for the first argument of a
1352method -- i.e. a function defined inside a class definition. A method
1353defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
1354some instance x of the class in which the definition occurs;
1355the called method will think it is called as meth(x, a, b, c).
1356
13574.22. Q. What is a unbound method?
1358
1359A. An unbound method is a method defined in a class that is not yet
1360bound to an instance. You get an unbound method if you ask for a
1361class attribute that happens to be a function. You get a bound method
1362if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +00001363instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001364an unbound method only knows which class it wants for its first
1365argument (a derived class is also OK). Calling an unbound method
1366doesn't "magically" derive the first argument from the context -- you
1367have to provide it explicitly.
1368
Guido van Rossuma6c707c1995-01-02 17:32:28 +000013694.23. Q. How do I call a method defined in a base class from a derived
1370class that overrides it?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001371
1372A. If your class definition starts with "class Derived(Base): ..."
1373then you can call method meth defined in Base (or one of Base's base
1374classes) as Base.meth(self, arguments...). Here, Base.meth is an
1375unbound method (see previous question).
1376
Guido van Rossuma6c707c1995-01-02 17:32:28 +000013774.24. Q. How do I call a method from a base class without using the
1378name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001379
1380A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
1381self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +00001382a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001383self.__class__.__bases__[0] is your class, not its base class -- so
1384(assuming you are doing this from within Derived.meth) you would start
1385a recursive call.
1386
13874.25. Q. How can I organize my code to make it easier to change the base
1388class?
1389
1390A. You could define an alias for the base class, assign the real base
1391class to it before your class definition, and use the alias throughout
1392your class. Then all you have to change is the value assigned to the
1393alias. Incidentally, this trick is also handy if you want to decide
1394dynamically (e.g. depending on availability of resources) which base
1395class to use. Example:
1396
1397BaseAlias = <real base class>
1398class Derived(BaseAlias):
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001399 def meth(self):
1400 BaseAlias.meth(self)
1401 ...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001402
14034.26. Q. How can I find the methods or attributes of an object?
1404
1405A. This depends on the object type.
1406
1407For an instance x of a user-defined class, instance attributes are
1408found in the dictionary x.__dict__, and methods and attributes defined
1409by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1410range(len(x.__class__.__bases__))). You'll have to walk the tree of
1411base classes to find *all* class methods and attributes.
1412
1413Many, but not all built-in types define a list of their method names
1414in x.__methods__, and if they have data attributes, their names may be
1415found in x.__members__. However this is only a convention.
1416
1417For more information, read the source of the standard (but
1418undocumented) module newdir.
1419
14204.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1421
1422A. os.read() is a low-level function which takes a file descriptor (a
1423small integer). os.popen() creates a high-level file object -- the
1424same type used for sys.std{in,out,err} and returned by the builtin
1425open() function. Thus, to read n bytes from a pipe p created with
1426os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001427
Guido van Rossum061f1821994-10-06 16:03:45 +000014284.28. Q. How can I create a stand-alone binary from a Python script?
1429
1430The demo script "Demo/scripts/freeze.py" does what you want. (It's
1431actually not a demo but a support tool -- there is some extra code in
1432the interpreter to accommodate it.) It requires that you have the
1433Python build tree handy, complete with all the lib*.a files.
1434
1435This works by scanning your source recursively for import statements
1436(both forms) and looking for the modules on the standard Python path
1437as well as in the source directory (for built-in modules). It then
1438"compiles" the modules written in Python to C code (array initializers
1439that can be turned into code objects using the marshal module) and
1440creates a custom-made config file that only contains those built-in
1441modules which are actually used in the program. It then compiles the
1442generated C code and links it with the rest of the Python interpreter
1443to form a self-contained binary which acts exactly like your script.
1444
Guido van Rossum7be34a81995-05-31 15:17:12 +00001445Hint: the freeze program only works if your script's filename ends in
1446".py".
Guido van Rossum061f1821994-10-06 16:03:45 +00001447
Guido van Rossum7be34a81995-05-31 15:17:12 +000014484.29. Q. What WWW tools are there for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +00001449
Guido van Rossum7be34a81995-05-31 15:17:12 +00001450A. There's a whole lot in the 1.2 release; see the section on
1451"Internet and WWW" in the Library Reference Manual. Highlights: a CGI
1452parser, a url retriever, an html parser.
Guido van Rossum061f1821994-10-06 16:03:45 +00001453
1454Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1455called Dancer. An alpha version can be FTP'ed from
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001456<URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a
1457few articles about Dancer in the (hyper)mail archive
1458<URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.)
Guido van Rossum061f1821994-10-06 16:03:45 +00001459
Guido van Rossum7be34a81995-05-31 15:17:12 +000014604.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +00001461and output?
1462
1463A. This is really a UNIX question. Also, in general, it is unwise to
1464do so, because you can easily cause a deadlock where the parent
1465process is blocked waiting for output from the child, while the child
1466is blocked waiting for input from the child. This can be caused
1467because the parent expects the child to output more text than it does,
1468or it can be caused by data being stuck in stdio buffers due to lack
1469of flushing. The Python parent can of course explicitly flush the data
1470it sends to the child before it reads any output, but if the child is
1471a naive C program it can easily have been written to never explicitly
1472flush its output, even if it is interactive, since flushing is
1473normally automatic.
1474
1475In many cases, all you really need is to run some data through a
1476command and get the result back. Unless the data is infinite in size,
1477the easiest (and often the most efficient!) way to do this is to write
1478it to a temporary file and run the command with that temporary file as
1479input. The standard module tempfile exports a function mktemp() which
1480generates unique temporary file names.
1481
1482If after reading all of the above you still want to connect two pipes
1483to a subprocess's standard input and output, here's a simple solution,
1484due to Jack Jansen:
1485
Guido van Rossume7d92d51995-10-11 18:06:54 +00001486 import os
1487 import sys
1488 import string
Guido van Rossum796b2591995-01-20 23:05:52 +00001489
Guido van Rossume7d92d51995-10-11 18:06:54 +00001490 MAXFD = 100 # Max number of file descriptors in this system
Guido van Rossum796b2591995-01-20 23:05:52 +00001491
Guido van Rossume7d92d51995-10-11 18:06:54 +00001492 def popen2(cmd):
1493 cmd = string.split(cmd)
1494 p2cread, p2cwrite = os.pipe()
1495 c2pread, c2pwrite = os.pipe()
1496 pid = os.fork()
1497 if pid == 0:
1498 # Child
1499 os.close(0)
1500 os.close(1)
1501 if os.dup(p2cread) <> 0:
1502 sys.stderr.write('popen2: bad read dup\n')
1503 if os.dup(c2pwrite) <> 1:
1504 sys.stderr.write('popen2: bad write dup\n')
1505 for i in range(3, MAXFD):
1506 try:
1507 os.close(i)
1508 except:
1509 pass
1510 try:
1511 os.execv(cmd[0], cmd)
1512 finally:
1513 os._exit(1)
1514 os.close(p2cread)
1515 tochild = os.fdopen(p2cwrite, 'w')
1516 os.close(c2pwrite)
1517 fromchild = os.fdopen(c2pread, 'r')
1518 return fromchild, tochild
Guido van Rossum796b2591995-01-20 23:05:52 +00001519
1520Note that many interactive programs (e.g. vi) don't work well with
1521pipes substituted for standard input and output. You will have to use
1522pseudo ttys ("ptys") instead of pipes. There is some undocumented
1523code to use these in the library module pty.py -- I'm afraid you're on
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001524your own here.
1525
1526A different answer is a Python interface to Don Libes' "expect"
1527library. A prerelease of this is available on the Python ftp mirror
1528sites in the contrib subdirectory as expy-0.3.tar.gz, e.g.
1529<URL:ftp://ftp.python.org/pub/python/contrib/expy-0.3.tar.gz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001530
Guido van Rossum7be34a81995-05-31 15:17:12 +000015314.31. Q. How do I call a function if I have the arguments in a tuple?
Guido van Rossumac3f2121995-04-10 11:53:42 +00001532
1533A. Use the built-in function apply(). For instance,
1534
1535 func(1, 2, 3)
1536
1537is equivalent to
1538
1539 args = (1, 2, 3)
1540 apply(func, args)
1541
1542Note that func(args) is not the same -- it calls func() with exactly
1543one argument, the tuple args, instead of three arguments, the integers
15441, 2 and 3.
1545
Guido van Rossum7be34a81995-05-31 15:17:12 +000015464.32. Q. How do I enable font-lock-mode for Python in Emacs?
1547
1548A. Assuming you're already using python-mode and font-lock-mode
1549separately, all you need to do is put this in your .emacs file:
1550
Guido van Rossume7d92d51995-10-11 18:06:54 +00001551 (defun my-python-mode-hook ()
1552 (setq font-lock-keywords python-font-lock-keywords)
1553 (font-lock-mode 1))
1554 (add-hook 'python-mode-hook 'my-python-mode-hook)
Guido van Rossum7be34a81995-05-31 15:17:12 +00001555
15564.33. Q. Is there an inverse to the format operator (a la C's scanf())?
1557
1558A. Not as such.
1559
1560For simple input parsing, the easiest approach is usually to split
1561the line into whitespace-delimited words using string.split(), and to
1562convert decimal strings to numeric values using string.atoi(),
1563string.atol() or string.atof(). (Python's atoi() is 32-bit and its
1564atol() is arbitrary precision.) If you want to use another delimiter
1565than whitespace, use string.splitfield() (possibly combining it with
1566string.strip() which removes surrounding whitespace from a string).
1567
1568For more complicated input parsing, regular expressions (see module
1569regex) are better suited and more powerful than C's scanf().
1570
Guido van Rossum061f1821994-10-06 16:03:45 +00001571
Guido van Rossuma7925f11994-01-26 10:20:16 +000015725. Extending Python
1573===================
1574
15755.1. Q. Can I create my own functions in C?
1576
1577A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001578variables, exceptions and even new types in C. This is explained in
1579the document "Extending and Embedding the Python Interpreter" (the
1580LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001581
15825.2. Q. Can I create my own functions in C++?
1583
1584A. Yes, using the C-compatibility features found in C++. Basically
1585you place extern "C" { ... } around the Python include files and put
1586extern "C" before each function that is going to be called by the
1587Python interpreter. Global or static C++ objects with constructors
1588are probably not a good idea.
1589
Guido van Rossum7ce61c11994-06-13 15:13:56 +000015905.3. Q. How can I execute arbitrary Python statements from C?
1591
1592A. The highest-level function to do this is run_command() which takes
1593a single string argument which is executed in the context of module
1594__main__ and returns 0 for success and -1 when an exception occurred
1595(including SyntaxError). If you want more control, use run_string();
1596see the source for run_command() in Python/pythonrun.c.
1597
15985.4. Q. How can I evaluate an arbitrary Python expression from C?
1599
1600A. Call the function run_string() from the previous question with the
1601start symbol eval_input; it then parses an expression, evaluates it
1602and returns its value. See exec_eval() in Python/bltinmodule.c.
1603
16045.5. Q. How do I extract C values from a Python object?
1605
1606A. That depends on the object's type. If it's a tuple,
1607gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1608i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1609i). For strings, getstringsize(o) returns its length and
1610getstringvalue(o) a pointer to its value (note that Python strings may
1611contain null bytes so strlen() is not safe). To test which type an
1612object is, first make sure it isn't NULL, and then use
1613is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1614
16155.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1616
1617A. You can't. Use t = newtupleobject(n) instead, and fill it with
1618objects using settupleitem(t, i, o) -- note that this "eats" a
1619reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001620setlistitem(l, i, o). Note that you *must* set all the tuple items to
1621some value before you pass the tuple to Python code --
1622newtupleobject(n) initializes them to NULL, which isn't a valid Python
1623value.
1624
Guido van Rossum796b2591995-01-20 23:05:52 +000016255.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001626Extensions manual?
1627
1628A. It's a typo, I meant newtupleobject() (see previous question).
1629
16305.8. Q. How do I call an object's method from C?
1631
1632A. Here's a function (untested) that might become part of the next
1633release in some form. It uses <stdarg.h> to allow passing the
1634argument list on to vmkvalue():
1635
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001636 object *call_method(object *inst, char *methodname, char *format, ...)
1637 {
1638 object *method;
1639 object *args;
1640 object *result;
1641 va_list va;
1642 method = getattr(inst, methodname);
1643 if (method == NULL) return NULL;
1644 va_start(va, format);
1645 args = vmkvalue(format, va);
1646 va_end(va);
1647 if (args == NULL) {
1648 DECREF(method);
1649 return NULL;
1650 }
1651 result = call_object(method, args);
1652 DECREF(method);
1653 DECREF(args);
1654 return result;
1655 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001656
1657This works for any instance that has methods -- whether built-in or
1658user-defined. You are responsible for eventually DECREF'ing the
1659return value.
1660
1661To call, e.g., a file object's "seek" method with arguments 10, 0
1662(assuming the file object pointer is "f"):
1663
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001664 res = call_method(f, "seek", "(OO)", 10, 0);
1665 if (res == NULL) {
1666 ... an exception occurred ...
1667 }
1668 else {
1669 DECREF(res);
1670 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001671
1672Note that since call_object() *always* wants a tuple for the argument
1673list, to call a function without arguments, pass "()" for the format,
1674and to call a function with one argument, surround the argument in
1675parentheses, e.g. "(i)".
1676
16775.9. Q. How do I catch the output from print_error()?
1678
1679A. (Due to Mark Hammond):
1680
1681* in Python code, define an object that supports the "write()" method.
1682FWIW, there seems to be a small problem that requires the 'softspace'
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001683attribute to be defined too (fixed in 1.2).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001684
1685* redirect sys.stdout and sys.stderr to this object.
1686
Guido van Rossum061f1821994-10-06 16:03:45 +00001687* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001688work.
1689
Guido van Rossum061f1821994-10-06 16:03:45 +00001690Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001691
16925.10. Q. How do I access a module written in Python from C?
1693
1694A. You can get a pointer to the module object as follows:
1695
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001696 module = import_module("<modulename>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001697
1698If the module hasn't been imported yet (i.e. it is not yet present in
1699sys.modules), this initializes the module; otherwise it simply returns
1700the value of sys.modules["<modulename>"]. Note that it doesn't enter
1701the module into any namespace -- it only ensures it has been
1702initialized and is stored in sys.modules.
1703
1704You can then access the module's attributes (i.e. any name defined in
1705the module) as follows:
1706
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001707 attr = getattr(module, "<attrname>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001708
1709Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001710
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000017115.11. Q. How do I interface to C++ objects from Python?
1712
1713A. Depending on your requirements, there are many approaches. Begin
1714by reading the "Extending and Embedding" document (Doc/ext.tex, see
1715also <URL:http://www.python.org/doc/>). Realize that for the Python
1716run-time system, there isn't a whole lot of difference between C and
1717C++ -- so the strategy to build a new Python type around a C structure
1718(pointer) type will also work for C++ objects.
1719
1720Automatic generation of interfaces between Python and C++ is still at
1721the horizon -- parsing C++ header files requires an almost complete
1722C++ parser, and many features aren't easily translated from C++ to
1723Python: certain forms of operator oveloading, function overloading
1724(best approached by a varargs function which explicitly type-checks
1725its arguments), and reference arguments are just a number of features
1726that are hard to translate correctly if at all.
1727
1728The hardest problem is to transparently translate the C++ class
1729hierarchy to Python, so that Python programs derive classes from C++
1730classes. Given suitable constraints, this may be possible, but it
1731would require more space than I have in this FAQ to explain how.
1732In any case, you can get quite a bit done without this, using just the
1733existing classes from Python.
1734
1735If this all seems rather daunting, that may be because it is -- C++
1736isn't exactly a baby to handle without gloves! However, people have
1737accomplished amazing feats of interfacing between Python and C++, and
1738a detailed question posted to the Python list is likely to elicit some
1739interesting and useful responses.
1740
Guido van Rossuma7925f11994-01-26 10:20:16 +00001741
17426. Python's design
1743==================
1744
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000017456.1. Q. Why isn't there a generic copying operation for objects in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001746
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00001747A. Historically, there wasn't. However, starting in Python 1.2,
1748there's a library module "copy" which supports both shallow and deep
1749copying of most common Python objects (though not things like open
1750files, sockets or windows), including an extensible mechanism to copy
1751class instances.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001752
17536.2. Q. Why isn't there a generic way to implement persistent objects
1754in Python? (Persistent == automatically saved to and restored from
1755disk.)
1756
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00001757A. Like the previous question, historically, there wasn't. The
1758library module "pickle" now solves this in a very general way (though
1759you still can't store things like open files, sockests or windows),
1760and the library module "shelve" uses pickle and (g)dbm to create
1761presistent mappings containing arbitrary Python objects. There are
1762some problems with shelve when using gdbm which will be solved in
1763Python 1.3.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001764
Guido van Rossuma7925f11994-01-26 10:20:16 +000017656.3. Q. Why isn't there a switch or case statement in Python?
1766
1767A. You can do this easily enough with a sequence of
1768if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001769statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001770to do range tests.
1771
Guido van Rossumc50158e1994-05-31 09:18:50 +000017726.4. Q. Why does Python use indentation for grouping of statements?
1773
1774A. Basically I believe that using indentation for grouping is
1775extremely elegant and contributes a lot to the clarity of the average
1776Python program. Most people learn to love this feature after a while.
1777Some arguments for it:
1778
1779- Since there are no begin/end brackets there cannot be a disagreement
1780between grouping perceived by the parser and the human reader. I
1781remember long ago seeing a C fragment like this:
1782
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001783 if (x <= y)
1784 x++;
1785 y--;
1786 z++;
Guido van Rossumc50158e1994-05-31 09:18:50 +00001787
1788and staring a long time at it wondering why y was being decremented
1789even for x > y... (And I wasn't a C newbie then either.)
1790
Guido van Rossum7be34a81995-05-31 15:17:12 +00001791- Since there are no begin/end brackets, Python is much less prone to
1792coding-style conflicts. In C there are loads of different ways to
1793place the braces (including the choice whether to place braces around
1794single statements in certain cases, for consistency). If you're used
1795to reading (and writing) code that uses one style, you will feel at
1796least slightly uneasy when reading (or being required to write)
1797another style.
Guido van Rossumc50158e1994-05-31 09:18:50 +00001798
1799- Many coding styles place begin/end brackets on a line by themself.
1800This makes programs considerably longer and wastes valuable screen
1801space, making it harder to get a good overview over a program.
1802Ideally, a function should fit on one basic tty screen (say, 20
1803lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1804This is not solely due to the lack of begin/end brackets (the lack of
1805declarations also helps, and the powerful operations of course), but
1806it certainly helps!
1807
Guido van Rossum3de27361994-07-25 14:19:33 +000018086.5. Q. Why are Python strings immutable?
1809
1810A. There are two advantages. One is performance: knowing that a
1811string is immutable makes it easy to lay it out at construction time
1812-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001813the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001814other is that strings in Python are considered as "elemental" as
1815numbers. No amount of activity will change the value 8 to anything
1816else, and in Python, no amount of activity will change the string
1817"eight" to anything else. (Adapted from Jim Roskind)
1818
18196.6. Q. Why don't strings have methods like index() or sort(), like
1820lists?
1821
1822A. Good question. Strings currently don't have methods at all
1823(likewise tuples and numbers). Long ago, it seemed unnecessary to
1824implement any of these functions in C, so a standard library module
1825"string" written in Python was created that performs string related
1826operations. Since then, the cry for performance has moved most of
1827them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001828string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001829performance except during initialization). Some of these functions
1830(e.g. index()) could easily be implemented as string methods instead,
1831but others (e.g. sort()) can't, since their interface prescribes that
1832they modify the object, while strings are immutable (see the previous
1833question).
1834
18356.7. Q. Why does Python use methods for some functionality
1836(e.g. list.index()) but functions for other (e.g. len(list))?
1837
1838A. Functions are used for those operations that are generic for a
1839group of types and which should work even for objects that don't have
1840methods at all (e.g. numbers, strings, tuples). Also, implementing
1841len(), max(), min() as a built-in function is actually less code than
1842implementing them as methods for each type. One can quibble about
1843individual cases but it's really too late to change such things
1844fundamentally now.
1845
18466.8. Q. Why can't I derive a class from built-in types (e.g. lists or
1847files)?
1848
1849A. This is caused by the relatively late addition of (user-defined)
1850classes to the language -- the implementation framework doesn't easily
1851allow it. See the answer to question 4.2 for a work-around. This
1852*may* be fixed in the (distant) future.
1853
18546.9. Q. Why must 'self' be declared and used explicitly in method
1855definitions and calls?
1856
1857A. By asking this question you reveal your C++ background. :-)
1858When I added classes, this was (again) the simplest way of
1859implementing methods without too many changes to the interpreter. I
1860borrowed the idea from Modula-3. It turns out to be very useful, for
1861a variety of reasons.
1862
1863First, it makes it more obvious that you are using a method or
1864instance attribute instead of a local variable. Reading "self.x" or
1865"self.meth()" makes it absolutely clear that an instance variable or
1866method is used even if you don't know the class definition by heart.
1867In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00001868declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00001869in Python, there are no local variable declarations, so you'd have to
1870look up the class definition to be sure.
1871
1872Second, it means that no special syntax is necessary if you want to
1873explicitly reference or call the method from a particular class. In
1874C++, if you want to use a method from base class that is overridden in
1875a derived class, you have to use the :: operator -- in Python you can
1876write baseclass.methodname(self, <argument list>). This is
1877particularly useful for __init__() methods, and in general in cases
1878where a derived class method wants to extend the base class method of
1879the same name and thus has to call the base class method somehow.
1880
1881Lastly, for instance variables, it solves a syntactic problem with
1882assignment: since local variables in Python are (by definition!) those
1883variables to which a value assigned in a function body (and that
1884aren't explicitly declared global), there has to be some way to tell
1885the interpreter that an assignment was meant to assign to an instance
1886variable instead of to a local variable, and it should preferably be
1887syntactic (for efficiency reasons). C++ does this through
1888declarations, but Python doesn't have declarations and it would be a
1889pity having to introduce them just for this purpose. Using the
1890explicit "self.var" solves this nicely. Similarly, for using instance
1891variables, having to write "self.var" means that references to
1892unqualified names inside a method don't have to search the instance's
1893directories.
1894
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000018956.10. Q. Can't you emulate threads in the interpreter instead of
1896relying on an OS-specific thread implementation?
1897
1898A. Unfortunately, the interpreter pushes at least one C stack frame
1899for each Python stack frame. Also, extensions can call back into
1900Python at almost random moments. Therefore a complete threads
1901implementation requires thread support for C.
1902
Guido van Rossum061f1821994-10-06 16:03:45 +000019036.11. Q. Why can't lambda forms contain statements?
1904
1905A. Python lambda forms cannot contain statements because Python's
Guido van Rossum796b2591995-01-20 23:05:52 +00001906syntactic framework can't handle statements nested inside expressions.
Guido van Rossum061f1821994-10-06 16:03:45 +00001907
1908However, in Python, this is not a serious problem. Unlike lambda
1909forms in other languages, where they add functionality, Python lambdas
1910are only a shorthand notation if you're too lazy to define a function.
1911
1912Functions are already first class objects in Python, and can be
1913declared in a local scope. Therefore the only advantage of using a
1914lambda form instead of a locally-defined function is that you'll have
1915to invent a name for the function -- but that's just a local variable
1916to which the function object (which is exactly the same type of object
1917that a lambda form yields) is assigned!
1918
19196.12. Q. Why is there no more efficient way of iterating over a dictionary
1920than first constructing the list of keys()?
1921
1922A. Have you tried it? I bet it's fast enough for your purposes! In
1923most cases such a list takes only a few percent of the space occupied
1924by the dictionary -- it needs only 4 bytes (the size of a pointer) per
1925key -- a dictionary costs 8 bytes per key plus between 30 and 70
1926percent hash table overhead, plus the space for the keys and values --
1927by necessity all keys are unique objects and a string object (the most
1928common key type) costs at least 18 bytes plus the length of the
1929string. Add to that the values contained in the dictionary, and you
1930see that 4 bytes more per item really isn't that much more memory...
1931
1932A call to dict.keys() makes one fast scan over the dictionary
1933(internally, the iteration function does exist) copying the pointers
1934to the key objects into a pre-allocated list object of the right size.
1935The iteration time isn't lost (since you'll have to iterate anyway --
1936unless in the majority of cases your loop terminates very prematurely
1937(which I doubt since you're getting the keys in random order).
1938
1939I don't expose the dictionary iteration operation to Python
1940programmers because the dictionary shouldn't be modified during the
1941entire iteration -- if it is, there's a very small chance that the
1942dictionary is reorganized because the hash table becomes too full, and
1943then the iteration may miss some items and see others twice. Exactly
1944because this only occurs rarely, it would lead to hidden bugs in
1945programs: it's easy never to have it happen during test runs if you
1946only insert or delete a few items per iteration -- but your users will
1947surely hit upon it sooner or later.
1948
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000019496.13. Q. Can Python be compiled to machine code, C or some other language?
1950
1951A. Not easily. Python's high level data types, dynamic typing of
1952objects and run-time invocation of the interpreter (using eval() or
1953exec) together mean that a "compiled" Python program would probably
1954consist mostly of calls into the Python run-time system, even for
1955seemingly simple operations like "x+1". Thus, the performance gain
1956would probably be minimal.
1957
1958Internally, Python source code is always translated into a "virtual
1959machine code" or "byte code" representation before it is interpreted
1960(by the "Python virtual machine" or "bytecode interpreter"). In order
1961to avoid the overhead of parsing and translating modules that rarely
1962change over and over again, this byte code is written on a file whose
1963name ends in ".pyc" whenever a module is parsed (from a file whose
1964name ends in ".py"). When the corresponding .py file is changed, it
1965is parsed and translated again and the .pyc file is rewritten. There
1966is no performance difference once the .pyc file has been loaded (the
1967bytecode read from the .pyc file is exactly the same as the bytecode
1968created by direct translation). The only difference is that loading
1969code from a .pyc file is faster than parsing and translating a .py
1970file, so the presence of precompiled .pyc files will generally improve
1971start-up time of Python scripts. If desired, the Lib/compileall.py
1972module/script can be used to force creation of valid .pyc files for a
1973given set of modules.
1974
1975If you are looking for a way to translate Python programs in order to
1976distribute them in binary form, without the need to distribute the
1977interpreter and library as well, have a look at the freeze.py script
1978in the Tools/freeze directory. This creates a single binary file
1979incorporating your program, the Python interpreter, and those parts of
1980the Python library that are needed by your program. Of course, the
1981resulting binary will only run on the same type of platform as that
1982used to create it.
1983
1984Hints for proper usage of freeze.py:
1985
1986- the script must be in a file whose name ends in .py
1987
1988- you must have installed Python fully:
1989
Guido van Rossume7d92d51995-10-11 18:06:54 +00001990 make install
1991 make libinstall
1992 make inclinstall
1993 make libainstall
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00001994
Guido van Rossum05151e01995-09-28 13:24:46 +000019956.14. Q. Why doesn't Python use proper garbage collection?
1996
1997A. It's looking less and less likely that Python will ever get
1998"automatic" garbage collection (GC). For one thing, unless this were
1999added to C as a standard feature, it's a portability pain in the ass.
2000And yes, I know about the Xerox library. It has bits of assembler
2001code for *most* *common* platforms. Not for all. And although it is
2002mostly transparent, it isn't completely transparent (when I once
2003linked Python with it, it dumped core).
2004
2005"Proper" GC also becomes a problem when Python gets embedded into
2006other applications. While in a stand-alone Python it may be fine to
2007replace the standard malloc() and free() with versions provided by the
2008GC library, an application embedding Python may want to have its *own*
2009substitute for malloc() and free(), and may not want Python's. Right
2010now, Python works with anything that implements malloc() and free()
2011properly.
2012
2013Besides, the predictability of destructor calls in Python is kind of
2014attractive. With GC, the following code (which is fine in current
2015Python) will run out of file descriptors long before it runs out of
2016memory:
2017
Guido van Rossume7d92d51995-10-11 18:06:54 +00002018 for file in <very long list of files>:
2019 f = open(file)
2020 c = file.read(1)
Guido van Rossum05151e01995-09-28 13:24:46 +00002021
2022Using the current reference counting and destructor scheme, each new
2023assignment to f closes the previous file. Using GC, this is not
2024guaranteed. Sure, you can think of ways to fix this. But it's not
2025off-the-shelf technology.
2026
Guido van Rossuma7925f11994-01-26 10:20:16 +00002027
20287. Using Python on non-UNIX platforms
2029=====================================
2030
Guido van Rossum91f60831994-02-15 15:52:27 +000020317.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002032
Guido van Rossume530c581995-04-10 12:32:16 +00002033A. Yes, see the "mac" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00002034e.g. <URL:ftp://ftp.python.org/pub/python/mac/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002035
Guido van Rossum91f60831994-02-15 15:52:27 +000020367.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002037
Guido van Rossume530c581995-04-10 12:32:16 +00002038A. Yes, see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00002039e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002040
Guido van Rossume530c581995-04-10 12:32:16 +000020417.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002042
Guido van Rossume530c581995-04-10 12:32:16 +00002043A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002044e.g. <URL:ftp://ftp.python.org/pub/python/pc/>. You may also be able
2045to run either of the Windows NT versions (see next question) if you
2046have Microsoft's "win32s".
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002047
Guido van Rossum91f60831994-02-15 15:52:27 +000020487.4. Q. Is there a Windows NT version of Python?
2049
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002050A. There are two, both sporting DLL support for dynamic loading of
2051Python modules, and extensions to access the Win32 GUI API.
2052
2053Mark Hammond <MHammond@cmutual.com.au> maintains an NT port which
2054includes an interface to the Microsoft Foundation Classes and a Python
2055programming environment using it that's written mostly in Python. See
2056<URL:ftp://ftp.python.org/pub/python/nt/>.
2057
2058Jim Ahlstrom's WPY portable GUI runs on Windows NT and is modeled
2059after the Microsoft Foundation Classes. Source and binaries are
2060available in <URL:ftp://ftp.python.org/pub/python/wpy>.
Guido van Rossum91f60831994-02-15 15:52:27 +00002061
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002062Sam Rushing <rushing@squirl.oau.org> once announced he knows how to
2063build Python for the Windows NT on the DEC Alpha AXP.
Guido van Rossum061f1821994-10-06 16:03:45 +00002064
2065Note that currently there is no unified compilation environment for
2066all NT platforms -- hopefully Microsoft will fix this with the release
2067of Visual C++ 2.0.
2068
Guido van Rossume530c581995-04-10 12:32:16 +000020697.5. Q. Is there a Windows 95 version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002070
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002071A. The Windows NT versions might work, otherwise the Windows 3.1(1)
Guido van Rossume530c581995-04-10 12:32:16 +00002072version should work (isn't Windows 95 supposed to be backwards
2073compatible?).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002074
Guido van Rossume530c581995-04-10 12:32:16 +000020757.6. Q. Is there an OS/2 version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002076
Guido van Rossume530c581995-04-10 12:32:16 +00002077A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00002078e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002079
Guido van Rossume530c581995-04-10 12:32:16 +000020807.7. Q. Is there a VMS version of Python?
2081
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002082A. Donn Cave <donn@cac.washington.edu> did a partial port. The
2083results of his efforts are on public display in
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00002084<<URL:ftp://ftp.python.org/pub/python/contrib/vms.tar.gz/>. Someone
2085else is working on a more complete port, for details watch the list.
Guido van Rossume530c581995-04-10 12:32:16 +00002086
Guido van Rossum3fc9d731995-07-25 15:10:56 +000020877.8. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002088
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00002089A. I haven't heard about these, except I remember hearing about an
2090OS/9 port and a port to Vxworks (both operating systems for embedded
2091systems). If you're interested in any of this, go directly to the
2092newsgroup and ask there, you may find exactly what you need. For
2093example, a port to MPE/iX 5.0 on HP3000 computers was just announced,
2094see <URL:http://www.allegro.com/software/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002095
Guido van Rossume530c581995-04-10 12:32:16 +000020967.9. Q. Where are the source or Makefiles for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002097
Guido van Rossume530c581995-04-10 12:32:16 +00002098A. The standard sources can (almost) be used. Additional sources can
2099be found in the platform-specific subdirectories of the distribution.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002100
Guido van Rossume530c581995-04-10 12:32:16 +000021017.10. Q. What is the status and support for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002102
2103A. I don't have access to most of these platforms, so in general I am
2104dependent on material submitted by volunteers(*). However I strive to
2105integrate all changes needed to get it to compile on a particular
2106platform back into the standard sources, so porting of the next
2107version to the various non-UNIX platforms should be easy.
2108
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002109(*) For the Macintosh, that volunteer is me, with help from Jack
2110Jansen <jack@cwi.nl>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002111
Guido van Rossume530c581995-04-10 12:32:16 +000021127.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002113Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002114
2115A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00002116"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossume530c581995-04-10 12:32:16 +00002117you can get the file lib<version>.tar.gz from most ftp sites carrying
2118Python; this is a subset of the distribution containing just those
Guido van Rossum7be34a81995-05-31 15:17:12 +00002119files, e.g. <URL:ftp://ftp.python.org/pub/python/src/lib1.1.tar.gz>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002120
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002121Once you have installed the library, you need to point sys.path to it.
2122Assuming the library is in C:\misc\python\lib, the following commands
2123will point your Python interpreter to it (note the doubled backslashes
2124-- you can also use single forward slashes instead):
2125
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002126 >>> import sys
2127 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
2128 >>>
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002129
2130For a more permanent effect, set the environment variable PYTHONPATH,
2131as follows (talking to a DOS prompt):
2132
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002133 C> SET PYTHONPATH=C:\misc\python\lib
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002134
Guido van Rossume530c581995-04-10 12:32:16 +000021357.12. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002136
Guido van Rossume530c581995-04-10 12:32:16 +00002137A. The documentation for the Unix version also applies to the Mac and
2138PC versions. Where applicable, differences are indicated in the text.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002139
Guido van Rossume530c581995-04-10 12:32:16 +000021407.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00002141creating or editing programs apart from entering it interactively, and
2142there seems to be no way to save code that was entered interactively.
2143How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002144
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002145A. Use an external editor. On the Mac, BBEdit seems to be a popular
2146no-frills text editor. I work like this: start the interpreter; edit
2147a module file using BBedit; import and test it in the interpreter;
2148edit again in BBedit; then use the built-in function reload() to
2149re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002150
2151Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
2152anyone with a pulse could certainly figure out how to do the same on
2153MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
2154Not only can you easily resave and "reload()" from Python after making
2155changes, but since WinNot auto-copies to the clipboard any text you
2156select, you can simply select the entire procedure (function) which
2157you changed in WinNot, switch to QWPython, and shift-ins to reenter
2158the changed program unit."