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