home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / python / faq / windows.html < prev   
Encoding:
Extensible Markup Language  |  2006-07-05  |  40.9 KB  |  865 lines

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.4: http://docutils.sourceforge.net/" />
  7. <title>Python Windows FAQ</title>
  8. <meta name="date" content="2005-07-26" />
  9. <style type="text/css">
  10.  
  11. /*
  12. :Author: David Goodger
  13. :Contact: goodger@users.sourceforge.net
  14. :Date: $Date$
  15. :Revision: $Revision$
  16. :Copyright: This stylesheet has been placed in the public domain.
  17.  
  18. Default cascading style sheet for the HTML output of Docutils.
  19.  
  20. See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
  21. customize this style sheet.
  22. */
  23.  
  24. /* used to remove borders from tables and images */
  25. .borderless, table.borderless td, table.borderless th {
  26.   border: 0 }
  27.  
  28. table.borderless td, table.borderless th {
  29.   /* Override padding for "table.docutils td" with "! important".
  30.      The right padding separates the table cells. */
  31.   padding: 0 0.5em 0 0 ! important }
  32.  
  33. .first {
  34.   /* Override more specific margin styles with "! important". */
  35.   margin-top: 0 ! important }
  36.  
  37. .last, .with-subtitle {
  38.   margin-bottom: 0 ! important }
  39.  
  40. .hidden {
  41.   display: none }
  42.  
  43. a.toc-backref {
  44.   text-decoration: none ;
  45.   color: black }
  46.  
  47. blockquote.epigraph {
  48.   margin: 2em 5em ; }
  49.  
  50. dl.docutils dd {
  51.   margin-bottom: 0.5em }
  52.  
  53. /* Uncomment (and remove this text!) to get bold-faced definition list terms
  54. dl.docutils dt {
  55.   font-weight: bold }
  56. */
  57.  
  58. div.abstract {
  59.   margin: 2em 5em }
  60.  
  61. div.abstract p.topic-title {
  62.   font-weight: bold ;
  63.   text-align: center }
  64.  
  65. div.admonition, div.attention, div.caution, div.danger, div.error,
  66. div.hint, div.important, div.note, div.tip, div.warning {
  67.   margin: 2em ;
  68.   border: medium outset ;
  69.   padding: 1em }
  70.  
  71. div.admonition p.admonition-title, div.hint p.admonition-title,
  72. div.important p.admonition-title, div.note p.admonition-title,
  73. div.tip p.admonition-title {
  74.   font-weight: bold ;
  75.   font-family: sans-serif }
  76.  
  77. div.attention p.admonition-title, div.caution p.admonition-title,
  78. div.danger p.admonition-title, div.error p.admonition-title,
  79. div.warning p.admonition-title {
  80.   color: red ;
  81.   font-weight: bold ;
  82.   font-family: sans-serif }
  83.  
  84. /* Uncomment (and remove this text!) to get reduced vertical space in
  85.    compound paragraphs.
  86. div.compound .compound-first, div.compound .compound-middle {
  87.   margin-bottom: 0.5em }
  88.  
  89. div.compound .compound-last, div.compound .compound-middle {
  90.   margin-top: 0.5em }
  91. */
  92.  
  93. div.dedication {
  94.   margin: 2em 5em ;
  95.   text-align: center ;
  96.   font-style: italic }
  97.  
  98. div.dedication p.topic-title {
  99.   font-weight: bold ;
  100.   font-style: normal }
  101.  
  102. div.figure {
  103.   margin-left: 2em ;
  104.   margin-right: 2em }
  105.  
  106. div.footer, div.header {
  107.   clear: both;
  108.   font-size: smaller }
  109.  
  110. div.line-block {
  111.   display: block ;
  112.   margin-top: 1em ;
  113.   margin-bottom: 1em }
  114.  
  115. div.line-block div.line-block {
  116.   margin-top: 0 ;
  117.   margin-bottom: 0 ;
  118.   margin-left: 1.5em }
  119.  
  120. div.sidebar {
  121.   margin-left: 1em ;
  122.   border: medium outset ;
  123.   padding: 1em ;
  124.   background-color: #ffffee ;
  125.   width: 40% ;
  126.   float: right ;
  127.   clear: right }
  128.  
  129. div.sidebar p.rubric {
  130.   font-family: sans-serif ;
  131.   font-size: medium }
  132.  
  133. div.system-messages {
  134.   margin: 5em }
  135.  
  136. div.system-messages h1 {
  137.   color: red }
  138.  
  139. div.system-message {
  140.   border: medium outset ;
  141.   padding: 1em }
  142.  
  143. div.system-message p.system-message-title {
  144.   color: red ;
  145.   font-weight: bold }
  146.  
  147. div.topic {
  148.   margin: 2em }
  149.  
  150. h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
  151. h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
  152.   margin-top: 0.4em }
  153.  
  154. h1.title {
  155.   text-align: center }
  156.  
  157. h2.subtitle {
  158.   text-align: center }
  159.  
  160. hr.docutils {
  161.   width: 75% }
  162.  
  163. img.align-left {
  164.   clear: left }
  165.  
  166. img.align-right {
  167.   clear: right }
  168.  
  169. ol.simple, ul.simple {
  170.   margin-bottom: 1em }
  171.  
  172. ol.arabic {
  173.   list-style: decimal }
  174.  
  175. ol.loweralpha {
  176.   list-style: lower-alpha }
  177.  
  178. ol.upperalpha {
  179.   list-style: upper-alpha }
  180.  
  181. ol.lowerroman {
  182.   list-style: lower-roman }
  183.  
  184. ol.upperroman {
  185.   list-style: upper-roman }
  186.  
  187. p.attribution {
  188.   text-align: right ;
  189.   margin-left: 50% }
  190.  
  191. p.caption {
  192.   font-style: italic }
  193.  
  194. p.credits {
  195.   font-style: italic ;
  196.   font-size: smaller }
  197.  
  198. p.label {
  199.   white-space: nowrap }
  200.  
  201. p.rubric {
  202.   font-weight: bold ;
  203.   font-size: larger ;
  204.   color: maroon ;
  205.   text-align: center }
  206.  
  207. p.sidebar-title {
  208.   font-family: sans-serif ;
  209.   font-weight: bold ;
  210.   font-size: larger }
  211.  
  212. p.sidebar-subtitle {
  213.   font-family: sans-serif ;
  214.   font-weight: bold }
  215.  
  216. p.topic-title {
  217.   font-weight: bold }
  218.  
  219. pre.address {
  220.   margin-bottom: 0 ;
  221.   margin-top: 0 ;
  222.   font-family: serif ;
  223.   font-size: 100% }
  224.  
  225. pre.literal-block, pre.doctest-block {
  226.   margin-left: 2em ;
  227.   margin-right: 2em ;
  228.   background-color: #eeeeee }
  229.  
  230. span.classifier {
  231.   font-family: sans-serif ;
  232.   font-style: oblique }
  233.  
  234. span.classifier-delimiter {
  235.   font-family: sans-serif ;
  236.   font-weight: bold }
  237.  
  238. span.interpreted {
  239.   font-family: sans-serif }
  240.  
  241. span.option {
  242.   white-space: nowrap }
  243.  
  244. span.pre {
  245.   white-space: pre }
  246.  
  247. span.problematic {
  248.   color: red }
  249.  
  250. span.section-subtitle {
  251.   /* font-size relative to parent (h1..h6 element) */
  252.   font-size: 80% }
  253.  
  254. table.citation {
  255.   border-left: solid 1px gray;
  256.   margin-left: 1px }
  257.  
  258. table.docinfo {
  259.   margin: 2em 4em }
  260.  
  261. table.docutils {
  262.   margin-top: 0.5em ;
  263.   margin-bottom: 0.5em }
  264.  
  265. table.footnote {
  266.   border-left: solid 1px black;
  267.   margin-left: 1px }
  268.  
  269. table.docutils td, table.docutils th,
  270. table.docinfo td, table.docinfo th {
  271.   padding-left: 0.5em ;
  272.   padding-right: 0.5em ;
  273.   vertical-align: top }
  274.  
  275. table.docutils th.field-name, table.docinfo th.docinfo-name {
  276.   font-weight: bold ;
  277.   text-align: left ;
  278.   white-space: nowrap ;
  279.   padding-left: 0 }
  280.  
  281. h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
  282. h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
  283.   font-size: 100% }
  284.  
  285. tt.docutils {
  286.   background-color: #eeeeee }
  287.  
  288. ul.auto-toc {
  289.   list-style-type: none }
  290.  
  291. </style>
  292. </head>
  293. <body>
  294. <div class="document" id="python-windows-faq">
  295. <h1 class="title">Python Windows FAQ</h1>
  296. <table class="docinfo" frame="void" rules="none">
  297. <col class="docinfo-name" />
  298. <col class="docinfo-content" />
  299. <tbody valign="top">
  300. <tr><th class="docinfo-name">Date:</th>
  301. <td>2005-07-26</td></tr>
  302. <tr><th class="docinfo-name">Version:</th>
  303. <td>8295</td></tr>
  304. <tr class="field"><th class="docinfo-name">Web site:</th><td class="field-body"><a class="reference" href="http://www.python.org/">http://www.python.org/</a></td>
  305. </tr>
  306. </tbody>
  307. </table>
  308. <div class="contents topic">
  309. <p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
  310. <ul class="auto-toc simple">
  311. <li><a class="reference" href="#how-do-i-run-a-python-program-under-windows" id="id1" name="id1">1   How do I run a Python program under Windows?</a></li>
  312. <li><a class="reference" href="#how-do-i-make-python-scripts-executable" id="id2" name="id2">2   How do I make python scripts executable?</a></li>
  313. <li><a class="reference" href="#why-does-python-sometimes-take-so-long-to-start" id="id3" name="id3">3   Why does Python sometimes take so long to start?</a></li>
  314. <li><a class="reference" href="#where-is-freeze-for-windows" id="id4" name="id4">4   Where is Freeze for Windows?</a></li>
  315. <li><a class="reference" href="#is-a-pyd-file-the-same-as-a-dll" id="id5" name="id5">5   Is a <tt class="docutils literal"><span class="pre">*.pyd</span></tt> file the same as a DLL?</a></li>
  316. <li><a class="reference" href="#how-can-i-embed-python-into-a-windows-application" id="id6" name="id6">6   How can I embed Python into a Windows application?</a></li>
  317. <li><a class="reference" href="#how-do-i-use-python-for-cgi" id="id7" name="id7">7   How do I use Python for CGI?</a></li>
  318. <li><a class="reference" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source" id="id8" name="id8">8   How do I keep editors from inserting tabs into my Python source?</a></li>
  319. <li><a class="reference" href="#how-do-i-check-for-a-keypress-without-blocking" id="id9" name="id9">9   How do I check for a keypress without blocking?</a></li>
  320. <li><a class="reference" href="#how-do-i-emulate-os-kill-in-windows" id="id10" name="id10">10   How do I emulate os.kill() in Windows?</a></li>
  321. <li><a class="reference" href="#why-does-os-path-isdir-fail-on-nt-shared-directories" id="id11" name="id11">11   Why does os.path.isdir() fail on NT shared directories?</a></li>
  322. <li><a class="reference" href="#cgi-py-or-other-cgi-programming-doesn-t-work-sometimes-on-nt-or-win95" id="id12" name="id12">12   cgi.py (or other CGI programming) doesn't work sometimes on NT or win95!</a></li>
  323. <li><a class="reference" href="#why-doesn-t-os-popen-work-in-pythonwin-on-nt" id="id13" name="id13">13   Why doesn't os.popen() work in PythonWin on NT?</a></li>
  324. <li><a class="reference" href="#why-doesn-t-os-popen-win32pipe-popen-work-on-win9x" id="id14" name="id14">14   Why doesn't os.popen()/win32pipe.popen() work on Win9x?</a></li>
  325. <li><a class="reference" href="#pyrun-simplefile-crashes-on-windows-but-not-on-unix-why" id="id15" name="id15">15   PyRun_SimpleFile() crashes on Windows but not on Unix; why?</a></li>
  326. <li><a class="reference" href="#importing-tkinter-fails-on-windows-95-98-why" id="id16" name="id16">16   Importing _tkinter fails on Windows 95/98: why?</a></li>
  327. <li><a class="reference" href="#how-do-i-extract-the-downloaded-documentation-on-windows" id="id17" name="id17">17   How do I extract the downloaded documentation on Windows?</a></li>
  328. <li><a class="reference" href="#missing-cw3215mt-dll-or-missing-cw3215-dll" id="id18" name="id18">18   Missing cw3215mt.dll (or missing cw3215.dll)</a></li>
  329. <li><a class="reference" href="#warning-about-ctl3d32-version-from-installer" id="id19" name="id19">19   Warning about CTL3D32 version from installer</a></li>
  330. </ul>
  331. </div>
  332. <div class="section">
  333. <h1><a class="toc-backref" href="#id1" id="how-do-i-run-a-python-program-under-windows" name="how-do-i-run-a-python-program-under-windows">1   How do I run a Python program under Windows?</a></h1>
  334. <p>This is not necessarily a straightforward question. If you are already
  335. familiar with running programs from the Windows command line then
  336. everything will seem obvious; otherwise, you might need a little more
  337. guidance. There are also differences between Windows 95, 98, NT, ME,
  338. 2000 and XP which can add to the confusion.</p>
  339. <p>Unless you use some sort of integrated development environment,
  340. you will end up <em>typing</em> Windows commands into what is variously referred
  341. to as a "DOS window" or "Command prompt window". Usually you can
  342. create such a window from your Start menu; under Windows 2000
  343. the menu selection is
  344. "Start | Programs | Accessories | Command Prompt".  You should be
  345. able to recognize when you have started such a window because you will
  346. see a Windows "command prompt", which usually looks like this:</p>
  347. <pre class="literal-block">
  348. C:\>
  349. </pre>
  350. <p>The letter may be different, and there might be other things after it,
  351. so you might just as easily see something like:</p>
  352. <pre class="literal-block">
  353. D:\Steve\Projects\Python>
  354. </pre>
  355. <p>depending on how your computer has been set up and what else you have
  356. recently done with it. Once you have started such a window, you are
  357. well on the way to running Python programs.</p>
  358. <p>You need to realize that your Python scripts have to be processed by
  359. another program called the Python interpreter. The
  360. interpreter reads your script, compiles it into bytecodes, and then executes the bytecodes to run your
  361. program. So, how do you arrange for the interpreter to handle your
  362. Python?</p>
  363. <p>First, you need to make sure that your command window recognises the
  364. word "python" as an instruction to start the interpreter. If you have
  365. opened a command window, you should try entering the command <tt class="docutils literal"><span class="pre">python</span></tt>
  366. and hitting return.  You should then  see something like:</p>
  367. <pre class="literal-block">
  368. Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
  369. Type "help", "copyright", "credits" or "license" for more information.
  370. >>>
  371. </pre>
  372. <p>You have started the interpreter in "interactive mode". That means you
  373. can enter Python statements or expressions interactively and have them
  374. executed or evaluated while you wait. This is one of Python's
  375. strongest features. Check it by entering a few expressions of your
  376. choice and seeing the results:</p>
  377. <pre class="literal-block">
  378. >>> print "Hello"
  379. Hello
  380. >>> "Hello" * 3
  381. HelloHelloHello
  382. </pre>
  383. <p>Many people use the interactive mode as a convenient yet highly
  384. programmable calculator.
  385. When you want to end your interactive Python session, hold the Ctrl
  386. key down while you enter a Z, then hit the "Enter" key to get back to
  387. your Windows command prompt.</p>
  388. <p>You may also find that you have a
  389. Start-menu entry such as "Start | Programs | Python 2.2 | Python
  390. (command line)" that results in you seeing the <tt class="docutils literal"><span class="pre">>>></span></tt> prompt in a new
  391. window. If so, the window will disappear after you enter the
  392. Ctrl-Z character; Windows is running a single "python" command in the window,
  393. and closes it when you terminate the interpreter.</p>
  394. <p>If the <tt class="docutils literal"><span class="pre">python</span></tt> command, instead of displaying the interpreter
  395. prompt <tt class="docutils literal"><span class="pre">>>></span></tt>, gives you a message like:</p>
  396. <pre class="literal-block">
  397. 'python' is not recognized as an internal or external command,
  398. operable program or batch file.
  399. </pre>
  400. <p>or:</p>
  401. <pre class="literal-block">
  402. Bad command or filename
  403. </pre>
  404. <p>then you need to make sure that your computer knows where to find the
  405. Python interpreter. To do this you will have to modify a setting
  406. called PATH, which is a list of directories where Windows
  407. will look for programs. You should arrange for Python's
  408. installation directory to be added to the PATH of every command window
  409. as it starts. If you installed Python fairly recently then the command</p>
  410. <pre class="literal-block">
  411. dir C:\py*
  412. </pre>
  413. <p>will probably tell you where it is installed; the usual location is
  414. something like <tt class="docutils literal"><span class="pre">C:\Python23</span></tt>.  Otherwise you will be reduced to a search of your
  415. whole disk ... use "Tools | Find"
  416. or hit the "Search" button and look for "python.exe". Supposing you
  417. discover that Python is installed in the <tt class="docutils literal"><span class="pre">C:\Python23</span></tt> directory (the
  418. default at the time of writing), you should make sure that
  419. entering the command</p>
  420. <pre class="literal-block">
  421. c:\Python23\python
  422. </pre>
  423. <p>starts up the interpreter as above (and don't forget you'll need a
  424. "CTRL-Z" and an "Enter" to get out of it). Once you have verified the
  425. directory, you need to add it to the start-up routines your computer
  426. goes through.  For older versions of Windows the easiest way to do
  427. this is to edit the <tt class="docutils literal"><span class="pre">C:\AUTOEXEC.BAT</span></tt> file. You would want to add a line
  428. like the following to <tt class="docutils literal"><span class="pre">AUTOEXEC.BAT</span></tt>:</p>
  429. <pre class="literal-block">
  430. PATH C:\Python23;%PATH%
  431. </pre>
  432. <p>For Windows NT, 2000 and (I assume) XP, you will need to add a string
  433. such as</p>
  434. <pre class="literal-block">
  435. ;C:\Python23
  436. </pre>
  437. <p>to the current setting for the PATH environment variable, which you
  438. will find in the properties window of "My Computer" under the
  439. "Advanced" tab. Note that if you have sufficient privilege you might
  440. get a choice of installing the settings either for the Current User or
  441. for System. The latter is preferred if you want everybody to be able
  442. to run Python on the machine.</p>
  443. <p>If you aren't confident doing any of these manipulations yourself, ask
  444. for help!  At this stage you may want to reboot your system
  445. to make absolutely sure the new setting has taken effect. You probably
  446. won't need to reboot for Windows NT, XP or 2000. You can also avoid it in
  447. earlier versions by editing the file <tt class="docutils literal"><span class="pre">C:\WINDOWS\COMMAND\CMDINIT.BAT</span></tt>
  448. instead of <tt class="docutils literal"><span class="pre">AUTOEXEC.BAT</span></tt>.</p>
  449. <p>You should now be able to start a new command window, enter <tt class="docutils literal"><span class="pre">python</span></tt>
  450. at the <tt class="docutils literal"><span class="pre">C:></span></tt> (or whatever) prompt, and see the <tt class="docutils literal"><span class="pre">>>></span></tt> prompt that
  451. indicates the Python interpreter is reading interactive commands.</p>
  452. <p>Let's suppose you have a program called <tt class="docutils literal"><span class="pre">pytest.py</span></tt> in directory
  453. <tt class="docutils literal"><span class="pre">C:\Steve\Projects\Python</span></tt>. A session to run that program might look
  454. like this:</p>
  455. <pre class="literal-block">
  456. C:\> cd \Steve\Projects\Python
  457. C:\Steve\Projects\Python> python pytest.py
  458. </pre>
  459. <p>Because you added a file name to the command to start the interpreter,
  460. when it starts up it reads the Python script in the named file,
  461. compiles it, executes it, and terminates, so you see another <tt class="docutils literal"><span class="pre">C:\></span></tt>
  462. prompt.  You might also have entered</p>
  463. <pre class="literal-block">
  464. C:\> python \Steve\Projects\Python\pytest.py
  465. </pre>
  466. <p>if you hadn't wanted to change your current directory.</p>
  467. <p>Under NT, 2000 and XP you may well find that the installation process
  468. has also arranged that the command <tt class="docutils literal"><span class="pre">pytest.py</span></tt>
  469. (or, if the file isn't in the current directory,
  470. <tt class="docutils literal"><span class="pre">C:\Steve\Projects\Python\pytest.py</span></tt>)
  471. will automatically recognize the ".py" extension and run the Python
  472. interpreter on the named file. Using this feature is fine, but <em>some</em>
  473. versions of Windows have bugs which mean that this form isn't exactly
  474. equivalent to using the interpreter explicitly, so be careful.</p>
  475. <p>The important things to remember are:</p>
  476. <ol class="arabic">
  477. <li><p class="first">Start Python from the Start Menu, or make sure the PATH is set
  478. correctly so Windows can find the Python interpreter.</p>
  479. <pre class="literal-block">
  480. python
  481. </pre>
  482. <p>should give you a '>>>" prompt from the Python interpreter. Don't
  483. forget the CTRL-Z and ENTER to terminate the interpreter (and, if you
  484. started the window from the Start Menu, make the window disappear).</p>
  485. </li>
  486. <li><p class="first">Once this works, you run programs with commands:</p>
  487. <pre class="literal-block">
  488. python {program-file}
  489. </pre>
  490. </li>
  491. <li><p class="first">When you know the commands to use you can build Windows shortcuts
  492. to run the Python interpreter on any of your scripts, naming
  493. particular working directories, and adding them to your menus.
  494. Take a look at</p>
  495. <pre class="literal-block">
  496. python --help
  497. </pre>
  498. <p>if your needs are complex.</p>
  499. </li>
  500. <li><p class="first">Interactive mode (where you see the <tt class="docutils literal"><span class="pre">>>></span></tt> prompt) is best used
  501. for checking that individual statements and expressions do
  502. what you think they will, and for developing code by experiment.</p>
  503. </li>
  504. </ol>
  505. </div>
  506. <div class="section">
  507. <h1><a class="toc-backref" href="#id2" id="how-do-i-make-python-scripts-executable" name="how-do-i-make-python-scripts-executable">2   How do I make python scripts executable?</a></h1>
  508. <p>On Windows 2000, the standard Python installer already associates the
  509. .py extension with a file type (Python.File) and gives that file type
  510. an open command that runs the interpreter (D:\Program
  511. Files\Python\python.exe "%1" %*).  This is enough to make scripts
  512. executable from the command prompt as 'foo.py'.  If you'd rather be
  513. able to execute the script by simple typing 'foo' with no extension
  514. you need to add .py to the PATHEXT environment variable.</p>
  515. <p>On Windows NT, the steps taken by the installer as described above
  516. allow you to run a script with 'foo.py', but a longtime bug in the NT
  517. command processor prevents you from redirecting the input or output of
  518. any script executed in this way.  This is often important.</p>
  519. <p>The incantation for making a Python script executable under WinNT
  520. is to give the file an extension of .cmd and add the following as the first
  521. line:</p>
  522. <pre class="literal-block">
  523. @setlocal enableextensions & python -x %~f0 %* & goto :EOF
  524. </pre>
  525. </div>
  526. <div class="section">
  527. <h1><a class="toc-backref" href="#id3" id="why-does-python-sometimes-take-so-long-to-start" name="why-does-python-sometimes-take-so-long-to-start">3   Why does Python sometimes take so long to start?</a></h1>
  528. <p>Usually Python starts very quickly on Windows, but occasionally there
  529. are bug reports that Python suddenly begins to take a long time to
  530. start up.  This is made even more puzzling because Python will work fine
  531. on other Windows systems which appear to be configured identically.</p>
  532. <p>The problem may be caused by a misconfiguration of virus checking
  533. software on the problem machine.  Some virus scanners have been known
  534. to introduce startup overhead of two orders of magnitude when the
  535. scanner is configured to monitor all reads from the filesystem.  Try
  536. checking the configuration of virus scanning software on your systems
  537. to ensure that they are indeed configured identically.  McAfee, when
  538. configured to scan all file system read activity, is a particular
  539. offender.</p>
  540. </div>
  541. <div class="section">
  542. <h1><a class="toc-backref" href="#id4" id="where-is-freeze-for-windows" name="where-is-freeze-for-windows">4   Where is Freeze for Windows?</a></h1>
  543. <p>"Freeze" is a program that allows you to ship a Python program
  544. as a single stand-alone executable file.  It is <em>not</em> a compiler;
  545. your programs don't run any faster, but they are more easily
  546. distributable, at least to platforms with the same OS and CPU.  Read the
  547. README file of the freeze program for more disclaimers.</p>
  548. <p>You can use freeze on Windows, but you must download the source
  549. tree (see <a class="reference" href="http://www.python.org/download/download_source.html">http://www.python.org/download/download_source.html</a>).
  550. The freeze program is in the <tt class="docutils literal"><span class="pre">Tools\freeze</span></tt> subdirectory of the source
  551. tree.</p>
  552. <p>You need the Microsoft VC++ compiler, and you probably need to build
  553. Python.  The required project files are in the PCbuild directory.</p>
  554. </div>
  555. <div class="section">
  556. <h1><a class="toc-backref" href="#id5" id="is-a-pyd-file-the-same-as-a-dll" name="is-a-pyd-file-the-same-as-a-dll">5   Is a <tt class="docutils literal"><span class="pre">*.pyd</span></tt> file the same as a DLL?</a></h1>
  557. <p>Yes, .pyd files are dll's, but there are a few differences.  If you
  558. have a DLL named <tt class="docutils literal"><span class="pre">foo.pyd</span></tt>, then it must have a function initfoo().  You
  559. can then write Python "import foo", and Python will search for foo.pyd
  560. (as well as foo.py, foo.pyc) and if it finds it, will attempt to call
  561. initfoo() to initialize it.  You do not link your .exe with foo.lib,
  562. as that would cause Windows to require the DLL to be present.</p>
  563. <p>Note that the search path for foo.pyd is PYTHONPATH, not the same as
  564. the path that Windows uses to search for foo.dll.  Also, foo.pyd need
  565. not be present to run your program, whereas if you linked your program
  566. with a dll, the dll is required.  Of course, foo.pyd is required if
  567. you want to say "import foo".  In a DLL, linkage is declared in the
  568. source code with __declspec(dllexport).  In a .pyd, linkage is defined
  569. in a list of available functions.</p>
  570. </div>
  571. <div class="section">
  572. <h1><a class="toc-backref" href="#id6" id="how-can-i-embed-python-into-a-windows-application" name="how-can-i-embed-python-into-a-windows-application">6   How can I embed Python into a Windows application?</a></h1>
  573. <p>Embedding the Python interpreter in a Windows app can be summarized as
  574. follows:</p>
  575. <ol class="arabic">
  576. <li><p class="first">Do _not_ build Python into your .exe file directly.  On Windows,
  577. Python must be a DLL to handle importing modules that are themselves
  578. DLL's.  (This is the first key undocumented fact.) Instead, link to
  579. pythonNN.dll; it is typically installed in <tt class="docutils literal"><span class="pre">C:\Windows\System</span></tt>.
  580. NN is the Python version, a number such as "23" for Python 2.3.</p>
  581. <p>You can link to Python statically or dynamically.  Linking
  582. statically means linking against pythonNN.lib, while dynamically
  583. linking means linking against pythonNN.dll.  The drawback to
  584. dynamic linking is that your app won't run if pythonNN.dll does not
  585. exist on your system.
  586. (General note: pythonNN.lib is the so-called "import lib" corresponding
  587. to python.dll.  It merely defines symbols for the linker.)</p>
  588. <p>Linking dynamically greatly simplifies link options; everything happens
  589. at run time.  Your code must load pythonNN.dll using the Windows
  590. LoadLibraryEx() routine.  The code must also use access routines and
  591. data in pythonNN.dll (that is, Python's C API's) using pointers
  592. obtained by the Windows GetProcAddress() routine.  Macros can make
  593. using these pointers transparent to any C code that calls routines in
  594. Python's C API.</p>
  595. <p>Borland note: convert pythonNN.lib to OMF format using Coff2Omf.exe
  596. first.</p>
  597. </li>
  598. <li><p class="first">If you use SWIG, it is easy to create a Python "extension module"
  599. that will make the app's data and methods available to Python.  SWIG
  600. will handle just about all the grungy details for you.  The result is C
  601. code that you link <em>into</em> your .exe file (!)  You do _not_ have to
  602. create a DLL file, and this also simplifies linking.</p>
  603. </li>
  604. <li><p class="first">SWIG will create an init function (a C function) whose name depends
  605. on the name of the extension module.  For example, if the name of the
  606. module is leo, the init function will be called initleo().  If you use
  607. SWIG shadow classes, as you should, the init function will be called
  608. initleoc().  This initializes a mostly hidden helper class used by the
  609. shadow class.</p>
  610. <p>The reason you can link the C code in step 2 into your .exe file is that
  611. calling the initialization function is equivalent to importing the
  612. module into Python! (This is the second key undocumented fact.)</p>
  613. </li>
  614. <li><p class="first">In short, you can use the following code to initialize the Python
  615. interpreter with your extension module.</p>
  616. <pre class="literal-block">
  617. #include "python.h"
  618. ...
  619. Py_Initialize();  // Initialize Python.
  620. initmyAppc();  // Initialize (import) the helper class.
  621. PyRun_SimpleString("import myApp") ;  // Import the shadow class.
  622. </pre>
  623. </li>
  624. <li><p class="first">There are two problems with Python's C API which will become apparent
  625. if you use a compiler other than MSVC, the compiler used to build
  626. pythonNN.dll.</p>
  627. <p>Problem 1: The so-called "Very High Level" functions that take FILE *
  628. arguments will not work in a multi-compiler environment because each compiler's
  629. notion of a struct FILE will be different.  From an implementation
  630. standpoint these are very _low_ level functions.</p>
  631. <p>Problem 2: SWIG generates the following code when generating wrappers to
  632. void functions:</p>
  633. <pre class="literal-block">
  634. Py_INCREF(Py_None);
  635. _resultobj = Py_None;
  636. return _resultobj;
  637. </pre>
  638. <p>Alas, Py_None is a macro that expands to a reference to a complex data
  639. structure called _Py_NoneStruct inside pythonNN.dll.  Again, this code
  640. will fail in a mult-compiler environment.  Replace such code by:</p>
  641. <pre class="literal-block">
  642. return Py_BuildValue("");
  643. </pre>
  644. <p>It may be possible to use SWIG's %typemap command to make the change
  645. automatically, though I have not been able to get this to work (I'm a
  646. complete SWIG newbie).</p>
  647. </li>
  648. <li><p class="first">Using a Python shell script to put up a Python interpreter window
  649. from inside your Windows app is not a good idea; the resulting window
  650. will be independent of your app's windowing system.  Rather, you (or the
  651. wxPythonWindow class) should create a "native" interpreter window.  It
  652. is easy to connect that window to the Python interpreter.  You can
  653. redirect Python's i/o to _any_ object that supports read and write, so
  654. all you need is a Python object (defined in your extension module) that
  655. contains read() and write() methods.</p>
  656. </li>
  657. </ol>
  658. </div>
  659. <div class="section">
  660. <h1><a class="toc-backref" href="#id7" id="how-do-i-use-python-for-cgi" name="how-do-i-use-python-for-cgi">7   How do I use Python for CGI?</a></h1>
  661. <p>On the Microsoft IIS server or on the Win95 MS Personal Web Server you
  662. set up Python in the same way that you would set up any other
  663. scripting engine.</p>
  664. <p>Run regedt32 and go to:</p>
  665. <pre class="literal-block">
  666. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\ScriptMap
  667. </pre>
  668. <p>and enter the following line (making any specific changes that your system may need):</p>
  669. <pre class="literal-block">
  670. .py :REG_SZ: c:\<path to python>\python.exe -u %s %s
  671. </pre>
  672. <p>This line will allow you to call your script with a simple reference like:
  673. <a class="reference" href="http://yourserver/scripts/yourscript.py">http://yourserver/scripts/yourscript.py</a>
  674. provided "scripts" is an "executable" directory for your server (which
  675. it usually is by default).
  676. The "-u" flag specifies unbuffered and binary mode for stdin - needed when working with binary data.</p>
  677. <p>In addition, it is recommended that using
  678. ".py" may not be a good idea for the file extensions when used in this
  679. context (you might want to reserve <tt class="docutils literal"><span class="pre">*.py</span></tt> for support modules and
  680. use <tt class="docutils literal"><span class="pre">*.cgi</span></tt> or <tt class="docutils literal"><span class="pre">*.cgp</span></tt> for "main program" scripts).</p>
  681. <p>In order to set up Internet Information Services 5 to use Python for CGI processing, please see the following links:</p>
  682. <blockquote>
  683. <a class="reference" href="http://www.e-coli.net/pyiis_server.html">http://www.e-coli.net/pyiis_server.html</a> (for Win2k Server)
  684. <a class="reference" href="http://www.e-coli.net/pyiis.html">http://www.e-coli.net/pyiis.html</a> (for Win2k pro)</blockquote>
  685. <p>Configuring Apache is much simpler.
  686. In the Apache configuration file <tt class="docutils literal"><span class="pre">httpd.conf</span></tt>, add the following line at
  687. the end of the file:</p>
  688. <pre class="literal-block">
  689. ScriptInterpreterSource Registry
  690. </pre>
  691. <p>Then, give your Python CGI-scripts the extension .py and put them in the cgi-bin directory.</p>
  692. </div>
  693. <div class="section">
  694. <h1><a class="toc-backref" href="#id8" id="how-do-i-keep-editors-from-inserting-tabs-into-my-python-source" name="how-do-i-keep-editors-from-inserting-tabs-into-my-python-source">8   How do I keep editors from inserting tabs into my Python source?</a></h1>
  695. <p>The FAQ does not recommend using tabs, and <a class="reference" href="http://www.python.org/peps/pep-0008.html">the Python style guide</a>
  696. recommends 4 spaces for distributed Python code; this is also the
  697. Emacs python-mode default.</p>
  698. <p>Under any editor, mixing tabs and spaces is a bad idea.  MSVC is no
  699. different in this respect, and is easily configured to use spaces:
  700. Take Tools -> Options -> Tabs, and for file type "Default" set "Tab
  701. size" and "Indent size" to 4, and select the "Insert spaces" radio
  702. button.</p>
  703. <p>If you suspect mixed tabs and spaces are causing problems in leading
  704. whitespace, run Python with the -t switch or run
  705. <tt class="docutils literal"><span class="pre">Tools/Scripts/tabnanny.py</span></tt> to check a directory tree in batch mode.</p>
  706. </div>
  707. <div class="section">
  708. <h1><a class="toc-backref" href="#id9" id="how-do-i-check-for-a-keypress-without-blocking" name="how-do-i-check-for-a-keypress-without-blocking">9   How do I check for a keypress without blocking?</a></h1>
  709. <p>Use the msvcrt module.  This is a standard Windows-specific extension
  710. module.  It defines a function kbhit() which checks whether a keyboard
  711. hit is present, and getch() which gets one character without echoing
  712. it.</p>
  713. </div>
  714. <div class="section">
  715. <h1><a class="toc-backref" href="#id10" id="how-do-i-emulate-os-kill-in-windows" name="how-do-i-emulate-os-kill-in-windows">10   How do I emulate os.kill() in Windows?</a></h1>
  716. <p>Use win32api:</p>
  717. <pre class="literal-block">
  718. def kill(pid):
  719.     """kill function for Win32"""
  720.     import win32api
  721.     handle = win32api.OpenProcess(1, 0, pid)
  722.     return (0 != win32api.TerminateProcess(handle, 0))
  723. </pre>
  724. </div>
  725. <div class="section">
  726. <h1><a class="toc-backref" href="#id11" id="why-does-os-path-isdir-fail-on-nt-shared-directories" name="why-does-os-path-isdir-fail-on-nt-shared-directories">11   Why does os.path.isdir() fail on NT shared directories?</a></h1>
  727. <p>The solution appears to be always append the "\" on
  728. the end of shared drives.</p>
  729. <pre class="literal-block">
  730. >>> import os
  731. >>> os.path.isdir( '\\\\rorschach\\public')
  732. 0
  733. >>> os.path.isdir( '\\\\rorschach\\public\\')
  734. 1
  735. </pre>
  736. <p>It helps to think of share points as being like drive letters.
  737. Example:</p>
  738. <pre class="literal-block">
  739. k: is not a directory
  740. k:\ is a directory
  741. k:\media is a directory
  742. k:\media\ is not a directory
  743. </pre>
  744. <p>The same rules apply if you substitute "k:" with "\conkyfoo":</p>
  745. <pre class="literal-block">
  746. \\conky\foo  is not a directory
  747. \\conky\foo\ is a directory
  748. \\conky\foo\media is a directory
  749. \\conky\foo\media\ is not a directory
  750. </pre>
  751. </div>
  752. <div class="section">
  753. <h1><a class="toc-backref" href="#id12" id="cgi-py-or-other-cgi-programming-doesn-t-work-sometimes-on-nt-or-win95" name="cgi-py-or-other-cgi-programming-doesn-t-work-sometimes-on-nt-or-win95">12   cgi.py (or other CGI programming) doesn't work sometimes on NT or win95!</a></h1>
  754. <p>Be sure you have the latest python.exe, that you are using
  755. python.exe rather than a GUI version of Python and that you
  756. have configured the server to execute</p>
  757. <pre class="literal-block">
  758. "...\python.exe -u ..."
  759. </pre>
  760. <p>for the CGI execution.  The -u (unbuffered) option on NT and
  761. Win95 prevents the interpreter from altering newlines in the
  762. standard input and output.  Without it post/multipart requests
  763. will seem to have the wrong length and binary (e.g. GIF)
  764. responses may get garbled (resulting in broken images, PDF files,
  765. and other binary downloads failing).</p>
  766. </div>
  767. <div class="section">
  768. <h1><a class="toc-backref" href="#id13" id="why-doesn-t-os-popen-work-in-pythonwin-on-nt" name="why-doesn-t-os-popen-work-in-pythonwin-on-nt">13   Why doesn't os.popen() work in PythonWin on NT?</a></h1>
  769. <p>The reason that os.popen() doesn't work from within PythonWin is due
  770. to a bug in Microsoft's C Runtime Library (CRT). The CRT assumes you
  771. have a Win32 console attached to the process.</p>
  772. <p>You should use the win32pipe module's popen() instead which doesn't
  773. depend on having an attached Win32 console.</p>
  774. <p>Example:</p>
  775. <pre class="literal-block">
  776. import win32pipe
  777. f = win32pipe.popen('dir /c c:\\')
  778. print f.readlines()
  779. f.close()
  780. </pre>
  781. </div>
  782. <div class="section">
  783. <h1><a class="toc-backref" href="#id14" id="why-doesn-t-os-popen-win32pipe-popen-work-on-win9x" name="why-doesn-t-os-popen-win32pipe-popen-work-on-win9x">14   Why doesn't os.popen()/win32pipe.popen() work on Win9x?</a></h1>
  784. <p>There is a bug in Win9x that prevents os.popen/win32pipe.popen* from
  785. working. The good news is there is a way to work around this problem.
  786. The Microsoft Knowledge Base article that you need to lookup is:
  787. Q150956. You will find links to the knowledge base at:
  788. <a class="reference" href="http://www.microsoft.com/kb">http://www.microsoft.com/kb</a>.</p>
  789. </div>
  790. <div class="section">
  791. <h1><a class="toc-backref" href="#id15" id="pyrun-simplefile-crashes-on-windows-but-not-on-unix-why" name="pyrun-simplefile-crashes-on-windows-but-not-on-unix-why">15   PyRun_SimpleFile() crashes on Windows but not on Unix; why?</a></h1>
  792. <p>This is very sensitive to the compiler vendor, version and (perhaps)
  793. even options.  If the FILE* structure in your embedding program isn't
  794. the same as is assumed by the Python interpreter it won't work.</p>
  795. <p>The Python 1.5.* DLLs (<tt class="docutils literal"><span class="pre">python15.dll</span></tt>) are all compiled
  796. with MS VC++ 5.0 and with multithreading-DLL options (<tt class="docutils literal"><span class="pre">/MD</span></tt>).</p>
  797. <p>If you can't change compilers or flags, try using Py_RunSimpleString().
  798. A trick to get it to run an arbitrary file is to construct a call to
  799. execfile() with the name of your file as argument.</p>
  800. <p>Also note that you can not mix-and-match Debug and Release versions.  If you
  801. wish to use the Debug Multithreaded DLL, then your module _must_ have an
  802. "_d" appended to the base name.</p>
  803. </div>
  804. <div class="section">
  805. <h1><a class="toc-backref" href="#id16" id="importing-tkinter-fails-on-windows-95-98-why" name="importing-tkinter-fails-on-windows-95-98-why">16   Importing _tkinter fails on Windows 95/98: why?</a></h1>
  806. <p>Sometimes, the import of _tkinter fails on Windows 95 or 98,
  807. complaining with a message like the following:</p>
  808. <pre class="literal-block">
  809. ImportError: DLL load failed: One of the library files needed
  810. to run this application cannot be found.
  811. </pre>
  812. <p>It could be that you haven't installed Tcl/Tk, but if you did
  813. install Tcl/Tk, and the Wish application works correctly,
  814. the problem may be that its installer didn't
  815. manage to edit the autoexec.bat file correctly.  It tries to add a
  816. statement that changes the PATH environment variable to include
  817. the Tcl/Tk 'bin' subdirectory, but sometimes this edit doesn't
  818. quite work.  Opening it with notepad usually reveals what the
  819. problem is.</p>
  820. <p>(One additional hint, noted by David Szafranski: you can't use
  821. long filenames here; e.g. use <tt class="docutils literal"><span class="pre">C:\PROGRA~1\Tcl\bin</span></tt> instead of
  822. <tt class="docutils literal"><span class="pre">C:\Program</span> <span class="pre">Files\Tcl\bin</span></tt>.)</p>
  823. </div>
  824. <div class="section">
  825. <h1><a class="toc-backref" href="#id17" id="how-do-i-extract-the-downloaded-documentation-on-windows" name="how-do-i-extract-the-downloaded-documentation-on-windows">17   How do I extract the downloaded documentation on Windows?</a></h1>
  826. <p>Sometimes, when you download the documentation package to a Windows
  827. machine using a web browser, the file extension of the saved file
  828. ends up being .EXE.  This is a mistake; the extension should be .TGZ.</p>
  829. <p>Simply rename the downloaded file to have the .TGZ extension, and
  830. WinZip will be able to handle it.  (If your copy of WinZip doesn't,
  831. get a newer one from <a class="reference" href="http://www.winzip.com">http://www.winzip.com</a>.)</p>
  832. </div>
  833. <div class="section">
  834. <h1><a class="toc-backref" href="#id18" id="missing-cw3215mt-dll-or-missing-cw3215-dll" name="missing-cw3215mt-dll-or-missing-cw3215-dll">18   Missing cw3215mt.dll (or missing cw3215.dll)</a></h1>
  835. <p>Sometimes, when using Tkinter on Windows, you get an error that
  836. cw3215mt.dll or cw3215.dll is missing.</p>
  837. <p>Cause: you have an old Tcl/Tk DLL built with cygwin in your path
  838. (probably <tt class="docutils literal"><span class="pre">C:\Windows</span></tt>).  You must use the Tcl/Tk DLLs from the
  839. standard Tcl/Tk installation (Python 1.5.2 comes with one).</p>
  840. </div>
  841. <div class="section">
  842. <h1><a class="toc-backref" href="#id19" id="warning-about-ctl3d32-version-from-installer" name="warning-about-ctl3d32-version-from-installer">19   Warning about CTL3D32 version from installer</a></h1>
  843. <p>The Python installer issues a warning like this:</p>
  844. <pre class="literal-block">
  845. This version uses ``CTL3D32.DLL`` whitch is not the correct version.
  846. This version is used for windows NT applications only.
  847. </pre>
  848. <p>[Tim Peters]
  849. This is a Microsoft DLL, and a notorious
  850. source of problems.  The message means what it says:  you have the wrong version
  851. of this DLL for your operating system.  The Python installation did not
  852. cause this -- something else you installed previous to this overwrote the
  853. DLL that came with your OS (probably older shareware of some sort, but
  854. there's no way to tell now).  If you search for "CTL3D32" using any search
  855. engine (AltaVista, for example), you'll find hundreds and hundreds of web
  856. pages complaining about the same problem with all sorts of installation
  857. programs.  They'll point you to ways to get the correct version reinstalled
  858. on your system (since Python doesn't cause this, we can't fix it).</p>
  859. <p>David A Burton has written a little program to fix this.  Go to
  860. <a class="reference" href="http://www.burtonsys.com/download.html">http://www.burtonsys.com/download.html</a> and click on "ctl3dfix.zip"</p>
  861. </div>
  862. </div>
  863. </body>
  864. </html>
  865.