Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 1 | :tocdepth: 2 |
| 2 | |
| 3 | .. _windows-faq: |
| 4 | |
| 5 | ===================== |
| 6 | Python on Windows FAQ |
| 7 | ===================== |
| 8 | |
| 9 | .. contents:: |
| 10 | |
Georg Brandl | 62423cb | 2009-12-19 17:59:59 +0000 | [diff] [blame] | 11 | .. XXX need review for Python 3. |
| 12 | XXX need review for Windows Vista/Seven? |
| 13 | |
| 14 | |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 15 | How do I run a Python program under Windows? |
| 16 | -------------------------------------------- |
| 17 | |
| 18 | This is not necessarily a straightforward question. If you are already familiar |
| 19 | with running programs from the Windows command line then everything will seem |
| 20 | obvious; otherwise, you might need a little more guidance. There are also |
| 21 | differences between Windows 95, 98, NT, ME, 2000 and XP which can add to the |
| 22 | confusion. |
| 23 | |
| 24 | .. sidebar:: |Python Development on XP|_ |
| 25 | :subtitle: `Python Development on XP`_ |
| 26 | |
| 27 | This series of screencasts aims to get you up and running with Python on |
| 28 | Windows XP. The knowledge is distilled into 1.5 hours and will get you up |
| 29 | and running with the right Python distribution, coding in your choice of IDE, |
| 30 | and debugging and writing solid code with unit-tests. |
| 31 | |
| 32 | .. |Python Development on XP| image:: python-video-icon.png |
| 33 | .. _`Python Development on XP`: |
| 34 | http://www.showmedo.com/videos/series?name=pythonOzsvaldPyNewbieSeries |
| 35 | |
| 36 | Unless you use some sort of integrated development environment, you will end up |
| 37 | *typing* Windows commands into what is variously referred to as a "DOS window" |
| 38 | or "Command prompt window". Usually you can create such a window from your |
| 39 | Start menu; under Windows 2000 the menu selection is :menuselection:`Start --> |
| 40 | Programs --> Accessories --> Command Prompt`. You should be able to recognize |
| 41 | when you have started such a window because you will see a Windows "command |
| 42 | prompt", which usually looks like this:: |
| 43 | |
| 44 | C:\> |
| 45 | |
| 46 | The letter may be different, and there might be other things after it, so you |
| 47 | might just as easily see something like:: |
| 48 | |
| 49 | D:\Steve\Projects\Python> |
| 50 | |
| 51 | depending on how your computer has been set up and what else you have recently |
| 52 | done with it. Once you have started such a window, you are well on the way to |
| 53 | running Python programs. |
| 54 | |
| 55 | You need to realize that your Python scripts have to be processed by another |
| 56 | program called the Python interpreter. The interpreter reads your script, |
| 57 | compiles it into bytecodes, and then executes the bytecodes to run your |
| 58 | program. So, how do you arrange for the interpreter to handle your Python? |
| 59 | |
| 60 | First, you need to make sure that your command window recognises the word |
| 61 | "python" as an instruction to start the interpreter. If you have opened a |
| 62 | command window, you should try entering the command ``python`` and hitting |
| 63 | return. You should then see something like:: |
| 64 | |
| 65 | Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32 |
| 66 | Type "help", "copyright", "credits" or "license" for more information. |
| 67 | >>> |
| 68 | |
| 69 | You have started the interpreter in "interactive mode". That means you can enter |
| 70 | Python statements or expressions interactively and have them executed or |
| 71 | evaluated while you wait. This is one of Python's strongest features. Check it |
| 72 | by entering a few expressions of your choice and seeing the results:: |
| 73 | |
Georg Brandl | 62423cb | 2009-12-19 17:59:59 +0000 | [diff] [blame] | 74 | >>> print("Hello") |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 75 | Hello |
| 76 | >>> "Hello" * 3 |
| 77 | HelloHelloHello |
| 78 | |
| 79 | Many people use the interactive mode as a convenient yet highly programmable |
| 80 | calculator. When you want to end your interactive Python session, hold the Ctrl |
| 81 | key down while you enter a Z, then hit the "Enter" key to get back to your |
| 82 | Windows command prompt. |
| 83 | |
| 84 | You may also find that you have a Start-menu entry such as :menuselection:`Start |
| 85 | --> Programs --> Python 2.2 --> Python (command line)` that results in you |
| 86 | seeing the ``>>>`` prompt in a new window. If so, the window will disappear |
| 87 | after you enter the Ctrl-Z character; Windows is running a single "python" |
| 88 | command in the window, and closes it when you terminate the interpreter. |
| 89 | |
| 90 | If the ``python`` command, instead of displaying the interpreter prompt ``>>>``, |
| 91 | gives you a message like:: |
| 92 | |
| 93 | 'python' is not recognized as an internal or external command, |
| 94 | operable program or batch file. |
| 95 | |
| 96 | .. sidebar:: |Adding Python to DOS Path|_ |
| 97 | :subtitle: `Adding Python to DOS Path`_ |
| 98 | |
| 99 | Python is not added to the DOS path by default. This screencast will walk |
| 100 | you through the steps to add the correct entry to the `System Path`, allowing |
| 101 | Python to be executed from the command-line by all users. |
| 102 | |
| 103 | .. |Adding Python to DOS Path| image:: python-video-icon.png |
| 104 | .. _`Adding Python to DOS Path`: |
| 105 | http://showmedo.com/videos/video?name=960000&fromSeriesID=96 |
| 106 | |
| 107 | |
| 108 | or:: |
| 109 | |
| 110 | Bad command or filename |
| 111 | |
| 112 | then you need to make sure that your computer knows where to find the Python |
| 113 | interpreter. To do this you will have to modify a setting called PATH, which is |
| 114 | a list of directories where Windows will look for programs. |
| 115 | |
| 116 | You should arrange for Python's installation directory to be added to the PATH |
| 117 | of every command window as it starts. If you installed Python fairly recently |
| 118 | then the command :: |
| 119 | |
| 120 | dir C:\py* |
| 121 | |
| 122 | will probably tell you where it is installed; the usual location is something |
| 123 | like ``C:\Python23``. Otherwise you will be reduced to a search of your whole |
| 124 | disk ... use :menuselection:`Tools --> Find` or hit the :guilabel:`Search` |
| 125 | button and look for "python.exe". Supposing you discover that Python is |
| 126 | installed in the ``C:\Python23`` directory (the default at the time of writing), |
| 127 | you should make sure that entering the command :: |
| 128 | |
| 129 | c:\Python23\python |
| 130 | |
| 131 | starts up the interpreter as above (and don't forget you'll need a "CTRL-Z" and |
| 132 | an "Enter" to get out of it). Once you have verified the directory, you need to |
| 133 | add it to the start-up routines your computer goes through. For older versions |
| 134 | of Windows the easiest way to do this is to edit the ``C:\AUTOEXEC.BAT`` |
| 135 | file. You would want to add a line like the following to ``AUTOEXEC.BAT``:: |
| 136 | |
| 137 | PATH C:\Python23;%PATH% |
| 138 | |
| 139 | For Windows NT, 2000 and (I assume) XP, you will need to add a string such as :: |
| 140 | |
| 141 | ;C:\Python23 |
| 142 | |
| 143 | to the current setting for the PATH environment variable, which you will find in |
| 144 | the properties window of "My Computer" under the "Advanced" tab. Note that if |
| 145 | you have sufficient privilege you might get a choice of installing the settings |
| 146 | either for the Current User or for System. The latter is preferred if you want |
| 147 | everybody to be able to run Python on the machine. |
| 148 | |
| 149 | If you aren't confident doing any of these manipulations yourself, ask for help! |
| 150 | At this stage you may want to reboot your system to make absolutely sure the new |
| 151 | setting has taken effect. You probably won't need to reboot for Windows NT, XP |
| 152 | or 2000. You can also avoid it in earlier versions by editing the file |
| 153 | ``C:\WINDOWS\COMMAND\CMDINIT.BAT`` instead of ``AUTOEXEC.BAT``. |
| 154 | |
| 155 | You should now be able to start a new command window, enter ``python`` at the |
| 156 | ``C:\>`` (or whatever) prompt, and see the ``>>>`` prompt that indicates the |
| 157 | Python interpreter is reading interactive commands. |
| 158 | |
| 159 | Let's suppose you have a program called ``pytest.py`` in directory |
| 160 | ``C:\Steve\Projects\Python``. A session to run that program might look like |
| 161 | this:: |
| 162 | |
| 163 | C:\> cd \Steve\Projects\Python |
| 164 | C:\Steve\Projects\Python> python pytest.py |
| 165 | |
| 166 | Because you added a file name to the command to start the interpreter, when it |
| 167 | starts up it reads the Python script in the named file, compiles it, executes |
| 168 | it, and terminates, so you see another ``C:\>`` prompt. You might also have |
| 169 | entered :: |
| 170 | |
| 171 | C:\> python \Steve\Projects\Python\pytest.py |
| 172 | |
| 173 | if you hadn't wanted to change your current directory. |
| 174 | |
| 175 | Under NT, 2000 and XP you may well find that the installation process has also |
| 176 | arranged that the command ``pytest.py`` (or, if the file isn't in the current |
| 177 | directory, ``C:\Steve\Projects\Python\pytest.py``) will automatically recognize |
| 178 | the ".py" extension and run the Python interpreter on the named file. Using this |
| 179 | feature is fine, but *some* versions of Windows have bugs which mean that this |
| 180 | form isn't exactly equivalent to using the interpreter explicitly, so be |
| 181 | careful. |
| 182 | |
| 183 | The important things to remember are: |
| 184 | |
| 185 | 1. Start Python from the Start Menu, or make sure the PATH is set correctly so |
| 186 | Windows can find the Python interpreter. :: |
| 187 | |
| 188 | python |
| 189 | |
| 190 | should give you a '>>>' prompt from the Python interpreter. Don't forget the |
| 191 | CTRL-Z and ENTER to terminate the interpreter (and, if you started the window |
| 192 | from the Start Menu, make the window disappear). |
| 193 | |
| 194 | 2. Once this works, you run programs with commands:: |
| 195 | |
| 196 | python {program-file} |
| 197 | |
| 198 | 3. When you know the commands to use you can build Windows shortcuts to run the |
| 199 | Python interpreter on any of your scripts, naming particular working |
| 200 | directories, and adding them to your menus. Take a look at :: |
| 201 | |
| 202 | python --help |
| 203 | |
| 204 | if your needs are complex. |
| 205 | |
| 206 | 4. Interactive mode (where you see the ``>>>`` prompt) is best used for checking |
| 207 | that individual statements and expressions do what you think they will, and |
| 208 | for developing code by experiment. |
| 209 | |
| 210 | |
Ezio Melotti | 0639d5a | 2009-12-19 23:26:38 +0000 | [diff] [blame] | 211 | How do I make Python scripts executable? |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 212 | ---------------------------------------- |
| 213 | |
| 214 | On Windows 2000, the standard Python installer already associates the .py |
| 215 | extension with a file type (Python.File) and gives that file type an open |
| 216 | command that runs the interpreter (``D:\Program Files\Python\python.exe "%1" |
| 217 | %*``). This is enough to make scripts executable from the command prompt as |
| 218 | 'foo.py'. If you'd rather be able to execute the script by simple typing 'foo' |
| 219 | with no extension you need to add .py to the PATHEXT environment variable. |
| 220 | |
| 221 | On Windows NT, the steps taken by the installer as described above allow you to |
| 222 | run a script with 'foo.py', but a longtime bug in the NT command processor |
| 223 | prevents you from redirecting the input or output of any script executed in this |
| 224 | way. This is often important. |
| 225 | |
| 226 | The incantation for making a Python script executable under WinNT is to give the |
| 227 | file an extension of .cmd and add the following as the first line:: |
| 228 | |
| 229 | @setlocal enableextensions & python -x %~f0 %* & goto :EOF |
| 230 | |
| 231 | |
| 232 | Why does Python sometimes take so long to start? |
| 233 | ------------------------------------------------ |
| 234 | |
| 235 | Usually Python starts very quickly on Windows, but occasionally there are bug |
| 236 | reports that Python suddenly begins to take a long time to start up. This is |
| 237 | made even more puzzling because Python will work fine on other Windows systems |
| 238 | which appear to be configured identically. |
| 239 | |
| 240 | The problem may be caused by a misconfiguration of virus checking software on |
| 241 | the problem machine. Some virus scanners have been known to introduce startup |
| 242 | overhead of two orders of magnitude when the scanner is configured to monitor |
| 243 | all reads from the filesystem. Try checking the configuration of virus scanning |
| 244 | software on your systems to ensure that they are indeed configured identically. |
| 245 | McAfee, when configured to scan all file system read activity, is a particular |
| 246 | offender. |
| 247 | |
| 248 | |
| 249 | Where is Freeze for Windows? |
| 250 | ---------------------------- |
| 251 | |
| 252 | "Freeze" is a program that allows you to ship a Python program as a single |
| 253 | stand-alone executable file. It is *not* a compiler; your programs don't run |
| 254 | any faster, but they are more easily distributable, at least to platforms with |
| 255 | the same OS and CPU. Read the README file of the freeze program for more |
| 256 | disclaimers. |
| 257 | |
| 258 | You can use freeze on Windows, but you must download the source tree (see |
| 259 | http://www.python.org/download/source). The freeze program is in the |
| 260 | ``Tools\freeze`` subdirectory of the source tree. |
| 261 | |
| 262 | You need the Microsoft VC++ compiler, and you probably need to build Python. |
| 263 | The required project files are in the PCbuild directory. |
| 264 | |
| 265 | |
| 266 | Is a ``*.pyd`` file the same as a DLL? |
| 267 | -------------------------------------- |
| 268 | |
| 269 | .. XXX update for py3k (PyInit_foo) |
| 270 | |
| 271 | Yes, .pyd files are dll's, but there are a few differences. If you have a DLL |
| 272 | named ``foo.pyd``, then it must have a function ``initfoo()``. You can then |
| 273 | write Python "import foo", and Python will search for foo.pyd (as well as |
| 274 | foo.py, foo.pyc) and if it finds it, will attempt to call ``initfoo()`` to |
| 275 | initialize it. You do not link your .exe with foo.lib, as that would cause |
| 276 | Windows to require the DLL to be present. |
| 277 | |
| 278 | Note that the search path for foo.pyd is PYTHONPATH, not the same as the path |
| 279 | that Windows uses to search for foo.dll. Also, foo.pyd need not be present to |
| 280 | run your program, whereas if you linked your program with a dll, the dll is |
| 281 | required. Of course, foo.pyd is required if you want to say ``import foo``. In |
| 282 | a DLL, linkage is declared in the source code with ``__declspec(dllexport)``. |
| 283 | In a .pyd, linkage is defined in a list of available functions. |
| 284 | |
| 285 | |
| 286 | How can I embed Python into a Windows application? |
| 287 | -------------------------------------------------- |
| 288 | |
| 289 | Embedding the Python interpreter in a Windows app can be summarized as follows: |
| 290 | |
| 291 | 1. Do _not_ build Python into your .exe file directly. On Windows, Python must |
| 292 | be a DLL to handle importing modules that are themselves DLL's. (This is the |
Georg Brandl | 4985ff2 | 2010-10-17 10:14:38 +0000 | [diff] [blame] | 293 | first key undocumented fact.) Instead, link to :file:`python{NN}.dll`; it is |
| 294 | typically installed in ``C:\Windows\System``. *NN* is the Python version, a |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 295 | number such as "23" for Python 2.3. |
| 296 | |
Georg Brandl | 4985ff2 | 2010-10-17 10:14:38 +0000 | [diff] [blame] | 297 | You can link to Python in two different ways. Load-time linking means |
| 298 | linking against :file:`python{NN}.lib`, while run-time linking means linking |
| 299 | against :file:`python{NN}.dll`. (General note: :file:`python{NN}.lib` is the |
Georg Brandl | fc9794a | 2010-10-17 10:15:50 +0000 | [diff] [blame] | 300 | so-called "import lib" corresponding to :file:`python{NN}.dll`. It merely |
Georg Brandl | 4985ff2 | 2010-10-17 10:14:38 +0000 | [diff] [blame] | 301 | defines symbols for the linker.) |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 302 | |
Georg Brandl | 4985ff2 | 2010-10-17 10:14:38 +0000 | [diff] [blame] | 303 | Run-time linking greatly simplifies link options; everything happens at run |
| 304 | time. Your code must load :file:`python{NN}.dll` using the Windows |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 305 | ``LoadLibraryEx()`` routine. The code must also use access routines and data |
| 306 | in :file:`python{NN}.dll` (that is, Python's C API's) using pointers obtained |
| 307 | by the Windows ``GetProcAddress()`` routine. Macros can make using these |
| 308 | pointers transparent to any C code that calls routines in Python's C API. |
| 309 | |
| 310 | Borland note: convert :file:`python{NN}.lib` to OMF format using Coff2Omf.exe |
| 311 | first. |
| 312 | |
Georg Brandl | 4985ff2 | 2010-10-17 10:14:38 +0000 | [diff] [blame] | 313 | .. XXX what about static linking? |
| 314 | |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 315 | 2. If you use SWIG, it is easy to create a Python "extension module" that will |
| 316 | make the app's data and methods available to Python. SWIG will handle just |
| 317 | about all the grungy details for you. The result is C code that you link |
| 318 | *into* your .exe file (!) You do _not_ have to create a DLL file, and this |
| 319 | also simplifies linking. |
| 320 | |
| 321 | 3. SWIG will create an init function (a C function) whose name depends on the |
| 322 | name of the extension module. For example, if the name of the module is leo, |
| 323 | the init function will be called initleo(). If you use SWIG shadow classes, |
| 324 | as you should, the init function will be called initleoc(). This initializes |
| 325 | a mostly hidden helper class used by the shadow class. |
| 326 | |
| 327 | The reason you can link the C code in step 2 into your .exe file is that |
| 328 | calling the initialization function is equivalent to importing the module |
| 329 | into Python! (This is the second key undocumented fact.) |
| 330 | |
| 331 | 4. In short, you can use the following code to initialize the Python interpreter |
| 332 | with your extension module. |
| 333 | |
| 334 | .. code-block:: c |
| 335 | |
| 336 | #include "python.h" |
| 337 | ... |
| 338 | Py_Initialize(); // Initialize Python. |
| 339 | initmyAppc(); // Initialize (import) the helper class. |
| 340 | PyRun_SimpleString("import myApp") ; // Import the shadow class. |
| 341 | |
| 342 | 5. There are two problems with Python's C API which will become apparent if you |
| 343 | use a compiler other than MSVC, the compiler used to build pythonNN.dll. |
| 344 | |
| 345 | Problem 1: The so-called "Very High Level" functions that take FILE * |
| 346 | arguments will not work in a multi-compiler environment because each |
| 347 | compiler's notion of a struct FILE will be different. From an implementation |
| 348 | standpoint these are very _low_ level functions. |
| 349 | |
| 350 | Problem 2: SWIG generates the following code when generating wrappers to void |
| 351 | functions: |
| 352 | |
| 353 | .. code-block:: c |
| 354 | |
| 355 | Py_INCREF(Py_None); |
| 356 | _resultobj = Py_None; |
| 357 | return _resultobj; |
| 358 | |
| 359 | Alas, Py_None is a macro that expands to a reference to a complex data |
| 360 | structure called _Py_NoneStruct inside pythonNN.dll. Again, this code will |
| 361 | fail in a mult-compiler environment. Replace such code by: |
| 362 | |
| 363 | .. code-block:: c |
| 364 | |
| 365 | return Py_BuildValue(""); |
| 366 | |
| 367 | It may be possible to use SWIG's ``%typemap`` command to make the change |
| 368 | automatically, though I have not been able to get this to work (I'm a |
| 369 | complete SWIG newbie). |
| 370 | |
| 371 | 6. Using a Python shell script to put up a Python interpreter window from inside |
| 372 | your Windows app is not a good idea; the resulting window will be independent |
| 373 | of your app's windowing system. Rather, you (or the wxPythonWindow class) |
| 374 | should create a "native" interpreter window. It is easy to connect that |
| 375 | window to the Python interpreter. You can redirect Python's i/o to _any_ |
| 376 | object that supports read and write, so all you need is a Python object |
| 377 | (defined in your extension module) that contains read() and write() methods. |
| 378 | |
| 379 | |
| 380 | How do I use Python for CGI? |
| 381 | ---------------------------- |
| 382 | |
| 383 | On the Microsoft IIS server or on the Win95 MS Personal Web Server you set up |
| 384 | Python in the same way that you would set up any other scripting engine. |
| 385 | |
| 386 | Run regedt32 and go to:: |
| 387 | |
| 388 | HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\ScriptMap |
| 389 | |
| 390 | and enter the following line (making any specific changes that your system may |
| 391 | need):: |
| 392 | |
| 393 | .py :REG_SZ: c:\<path to python>\python.exe -u %s %s |
| 394 | |
| 395 | This line will allow you to call your script with a simple reference like: |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 396 | ``http://yourserver/scripts/yourscript.py`` provided "scripts" is an |
| 397 | "executable" directory for your server (which it usually is by default). The |
| 398 | :option:`-u` flag specifies unbuffered and binary mode for stdin - needed when |
| 399 | working with binary data. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 400 | |
| 401 | In addition, it is recommended that using ".py" may not be a good idea for the |
| 402 | file extensions when used in this context (you might want to reserve ``*.py`` |
| 403 | for support modules and use ``*.cgi`` or ``*.cgp`` for "main program" scripts). |
| 404 | |
| 405 | In order to set up Internet Information Services 5 to use Python for CGI |
| 406 | processing, please see the following links: |
| 407 | |
| 408 | http://www.e-coli.net/pyiis_server.html (for Win2k Server) |
| 409 | http://www.e-coli.net/pyiis.html (for Win2k pro) |
| 410 | |
| 411 | Configuring Apache is much simpler. In the Apache configuration file |
| 412 | ``httpd.conf``, add the following line at the end of the file:: |
| 413 | |
| 414 | ScriptInterpreterSource Registry |
| 415 | |
| 416 | Then, give your Python CGI-scripts the extension .py and put them in the cgi-bin |
| 417 | directory. |
| 418 | |
| 419 | |
| 420 | How do I keep editors from inserting tabs into my Python source? |
| 421 | ---------------------------------------------------------------- |
| 422 | |
| 423 | The FAQ does not recommend using tabs, and the Python style guide, :pep:`8`, |
| 424 | recommends 4 spaces for distributed Python code; this is also the Emacs |
| 425 | python-mode default. |
| 426 | |
| 427 | Under any editor, mixing tabs and spaces is a bad idea. MSVC is no different in |
| 428 | this respect, and is easily configured to use spaces: Take :menuselection:`Tools |
| 429 | --> Options --> Tabs`, and for file type "Default" set "Tab size" and "Indent |
| 430 | size" to 4, and select the "Insert spaces" radio button. |
| 431 | |
| 432 | If you suspect mixed tabs and spaces are causing problems in leading whitespace, |
| 433 | run Python with the :option:`-t` switch or run ``Tools/Scripts/tabnanny.py`` to |
| 434 | check a directory tree in batch mode. |
| 435 | |
| 436 | |
| 437 | How do I check for a keypress without blocking? |
| 438 | ----------------------------------------------- |
| 439 | |
| 440 | Use the msvcrt module. This is a standard Windows-specific extension module. |
| 441 | It defines a function ``kbhit()`` which checks whether a keyboard hit is |
| 442 | present, and ``getch()`` which gets one character without echoing it. |
| 443 | |
| 444 | |
| 445 | How do I emulate os.kill() in Windows? |
| 446 | -------------------------------------- |
| 447 | |
Brian Curtin | f4ed206 | 2010-04-12 18:10:10 +0000 | [diff] [blame] | 448 | Prior to Python 2.7 and 3.2, to terminate a process, you can use :mod:`ctypes`:: |
Georg Brandl | ff24c8e | 2010-03-21 09:52:24 +0000 | [diff] [blame] | 449 | |
| 450 | import ctypes |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 451 | |
| 452 | def kill(pid): |
| 453 | """kill function for Win32""" |
Georg Brandl | ff24c8e | 2010-03-21 09:52:24 +0000 | [diff] [blame] | 454 | kernel32 = ctypes.windll.kernel32 |
| 455 | handle = kernel32.OpenProcess(1, 0, pid) |
| 456 | return (0 != kernel32.TerminateProcess(handle, 0)) |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 457 | |
Brian Curtin | f4ed206 | 2010-04-12 18:10:10 +0000 | [diff] [blame] | 458 | In 2.7 and 3.2, :func:`os.kill` is implemented similar to the above function, |
| 459 | with the additional feature of being able to send CTRL+C and CTRL+BREAK |
| 460 | to console subprocesses which are designed to handle those signals. See |
| 461 | :func:`os.kill` for further details. |
| 462 | |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 463 | |
| 464 | Why does os.path.isdir() fail on NT shared directories? |
| 465 | ------------------------------------------------------- |
| 466 | |
| 467 | The solution appears to be always append the "\\" on the end of shared |
| 468 | drives. |
| 469 | |
| 470 | >>> import os |
| 471 | >>> os.path.isdir( '\\\\rorschach\\public') |
| 472 | 0 |
| 473 | >>> os.path.isdir( '\\\\rorschach\\public\\') |
| 474 | 1 |
| 475 | |
| 476 | It helps to think of share points as being like drive letters. Example:: |
| 477 | |
| 478 | k: is not a directory |
| 479 | k:\ is a directory |
| 480 | k:\media is a directory |
| 481 | k:\media\ is not a directory |
| 482 | |
| 483 | The same rules apply if you substitute "k:" with "\\conky\foo":: |
| 484 | |
| 485 | \\conky\foo is not a directory |
| 486 | \\conky\foo\ is a directory |
| 487 | \\conky\foo\media is a directory |
| 488 | \\conky\foo\media\ is not a directory |
| 489 | |
| 490 | |
| 491 | cgi.py (or other CGI programming) doesn't work sometimes on NT or win95! |
| 492 | ------------------------------------------------------------------------ |
| 493 | |
| 494 | Be sure you have the latest python.exe, that you are using python.exe rather |
| 495 | than a GUI version of Python and that you have configured the server to execute |
| 496 | :: |
| 497 | |
| 498 | "...\python.exe -u ..." |
| 499 | |
| 500 | for the CGI execution. The :option:`-u` (unbuffered) option on NT and Win95 |
| 501 | prevents the interpreter from altering newlines in the standard input and |
| 502 | output. Without it post/multipart requests will seem to have the wrong length |
| 503 | and binary (e.g. GIF) responses may get garbled (resulting in broken images, PDF |
| 504 | files, and other binary downloads failing). |
| 505 | |
| 506 | |
| 507 | Why doesn't os.popen() work in PythonWin on NT? |
| 508 | ----------------------------------------------- |
| 509 | |
| 510 | The reason that os.popen() doesn't work from within PythonWin is due to a bug in |
| 511 | Microsoft's C Runtime Library (CRT). The CRT assumes you have a Win32 console |
| 512 | attached to the process. |
| 513 | |
| 514 | You should use the win32pipe module's popen() instead which doesn't depend on |
| 515 | having an attached Win32 console. |
| 516 | |
| 517 | Example:: |
| 518 | |
| 519 | import win32pipe |
| 520 | f = win32pipe.popen('dir /c c:\\') |
Georg Brandl | 62423cb | 2009-12-19 17:59:59 +0000 | [diff] [blame] | 521 | print(f.readlines()) |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 522 | f.close() |
| 523 | |
| 524 | |
| 525 | Why doesn't os.popen()/win32pipe.popen() work on Win9x? |
| 526 | ------------------------------------------------------- |
| 527 | |
| 528 | There is a bug in Win9x that prevents os.popen/win32pipe.popen* from |
| 529 | working. The good news is there is a way to work around this problem. The |
| 530 | Microsoft Knowledge Base article that you need to lookup is: Q150956. You will |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 531 | find links to the knowledge base at: http://support.microsoft.com/. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 532 | |
| 533 | |
| 534 | PyRun_SimpleFile() crashes on Windows but not on Unix; why? |
| 535 | ----------------------------------------------------------- |
| 536 | |
| 537 | This is very sensitive to the compiler vendor, version and (perhaps) even |
| 538 | options. If the FILE* structure in your embedding program isn't the same as is |
| 539 | assumed by the Python interpreter it won't work. |
| 540 | |
| 541 | The Python 1.5.* DLLs (``python15.dll``) are all compiled with MS VC++ 5.0 and |
| 542 | with multithreading-DLL options (``/MD``). |
| 543 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 544 | If you can't change compilers or flags, try using :c:func:`Py_RunSimpleString`. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 545 | A trick to get it to run an arbitrary file is to construct a call to |
| 546 | :func:`execfile` with the name of your file as argument. |
| 547 | |
| 548 | Also note that you can not mix-and-match Debug and Release versions. If you |
| 549 | wish to use the Debug Multithreaded DLL, then your module *must* have an "_d" |
| 550 | appended to the base name. |
| 551 | |
| 552 | |
| 553 | Importing _tkinter fails on Windows 95/98: why? |
| 554 | ------------------------------------------------ |
| 555 | |
| 556 | Sometimes, the import of _tkinter fails on Windows 95 or 98, complaining with a |
| 557 | message like the following:: |
| 558 | |
| 559 | ImportError: DLL load failed: One of the library files needed |
| 560 | to run this application cannot be found. |
| 561 | |
| 562 | It could be that you haven't installed Tcl/Tk, but if you did install Tcl/Tk, |
| 563 | and the Wish application works correctly, the problem may be that its installer |
| 564 | didn't manage to edit the autoexec.bat file correctly. It tries to add a |
| 565 | statement that changes the PATH environment variable to include the Tcl/Tk 'bin' |
| 566 | subdirectory, but sometimes this edit doesn't quite work. Opening it with |
| 567 | notepad usually reveals what the problem is. |
| 568 | |
| 569 | (One additional hint, noted by David Szafranski: you can't use long filenames |
| 570 | here; e.g. use ``C:\PROGRA~1\Tcl\bin`` instead of ``C:\Program Files\Tcl\bin``.) |
| 571 | |
| 572 | |
| 573 | How do I extract the downloaded documentation on Windows? |
| 574 | --------------------------------------------------------- |
| 575 | |
| 576 | Sometimes, when you download the documentation package to a Windows machine |
| 577 | using a web browser, the file extension of the saved file ends up being .EXE. |
| 578 | This is a mistake; the extension should be .TGZ. |
| 579 | |
| 580 | Simply rename the downloaded file to have the .TGZ extension, and WinZip will be |
| 581 | able to handle it. (If your copy of WinZip doesn't, get a newer one from |
| 582 | http://www.winzip.com.) |
| 583 | |
| 584 | |
| 585 | Missing cw3215mt.dll (or missing cw3215.dll) |
| 586 | -------------------------------------------- |
| 587 | |
| 588 | Sometimes, when using Tkinter on Windows, you get an error that cw3215mt.dll or |
| 589 | cw3215.dll is missing. |
| 590 | |
| 591 | Cause: you have an old Tcl/Tk DLL built with cygwin in your path (probably |
| 592 | ``C:\Windows``). You must use the Tcl/Tk DLLs from the standard Tcl/Tk |
| 593 | installation (Python 1.5.2 comes with one). |
| 594 | |
| 595 | |
| 596 | Warning about CTL3D32 version from installer |
| 597 | -------------------------------------------- |
| 598 | |
| 599 | The Python installer issues a warning like this:: |
| 600 | |
Georg Brandl | 682d7e0 | 2010-10-06 10:26:05 +0000 | [diff] [blame] | 601 | This version uses CTL3D32.DLL which is not the correct version. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 602 | This version is used for windows NT applications only. |
| 603 | |
| 604 | Tim Peters: |
| 605 | |
| 606 | This is a Microsoft DLL, and a notorious source of problems. The message |
| 607 | means what it says: you have the wrong version of this DLL for your operating |
| 608 | system. The Python installation did not cause this -- something else you |
| 609 | installed previous to this overwrote the DLL that came with your OS (probably |
| 610 | older shareware of some sort, but there's no way to tell now). If you search |
| 611 | for "CTL3D32" using any search engine (AltaVista, for example), you'll find |
| 612 | hundreds and hundreds of web pages complaining about the same problem with |
| 613 | all sorts of installation programs. They'll point you to ways to get the |
| 614 | correct version reinstalled on your system (since Python doesn't cause this, |
| 615 | we can't fix it). |
| 616 | |
| 617 | David A Burton has written a little program to fix this. Go to |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 618 | http://www.burtonsys.com/downloads.html and click on "ctl3dfix.zip". |