Python Language FAQ - Section 8

This FAQ newsgroup posting has been automatically converted from an HTML snapshot of the original Python FAQ; please refer to the original "Python FAQ Wizard" at <http://grail.cnri.reston.va.us/cgi-bin/faqw.py> if source code snippets given in this document do not work - incidentally some formatting information may have been lost during the conversion. ---------------------------------------------------------------------------- The whole Python FAQ - Section 8 Last changed on Mon Jun 28 19:36:09 1999 EDT (Entries marked with ** were changed within the last 24 hours; entries marked with * were changed within the last 7 days.) ---------------------------------------------------------------------------- 8. Python on Windows 8.1. Using Python for CGI on Microsoft Windows 8.2. How to check for a keypress without blocking? 8.3. $PYTHONPATH 8.4. dedent syntax errors 8.5. How do I emulate os.kill() in Windows? 8.6. Why does os.path.isdir() fail on NT shared directories? 8.7. PyRun_SimpleFile() crashes on Windows but not on Unix 8.8. Import of _tkinter fails on Windows 95/98 8.9. Can't extract the downloaded documentation on Windows 8.10. Can't get Py_RunSimpleFile() to work. 8.11. Where is Freeze for Windows? 8.12. Is a *.pyd file the same as a DLL? 8.13. Missing cw3215mt.dll (or missing cw3215.dll) ---------------------------------------------------------------------------- 8. Python on Windows ---------------------------------------------------------------------------- 8.1. Using Python for CGI on Microsoft Windows Setting up the Microsoft IIS Server/Peer Server: On the Microsoft IIS server or on the Win95 MS Personal Web Server you set up python in the same way that you would set up any other scripting engine. Run regedt32 and go to: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\ScriptMap and enter the following line (making any specific changes that your system may need) .py :REG_SZ: c:\<path to python>\python.exe -u %s %s This line will allow you to call your script with a simple reference like: http://yourserver/scripts/yourscript.py provided "scripts" is an "executable" directory for your server (which it usually is by default). The "-u" flag specifies unbuffered and binary mode for stdin - needed when working with binary data In addition, it is recommended by people who would know that using ".py" may not be a good idea for the file extensions when used in this context (you might want to reserve *.py for support modules and use *.cgi or *.cgp for "main program" scripts). However, that issue is beyond this Windows FAQ entry. Netscape Servers: Information on this topic exists at: http://home.netscape.com/comprod/server_central/support/fasttrack_man/progra... ---------------------------------------------------------------------------- 8.2. How to check for a keypress without blocking? Use the msvcrt module. This is a standard Windows-specific extensions in Python 1.5 and beyond. It defines a function kbhit() which checks whether a keyboard hit is present; also getch() which gets one character without echo. Plus a few other goodies. (Search for "keypress" to find an answer for Unix as well.) ---------------------------------------------------------------------------- 8.3. $PYTHONPATH In MS-DOS derived environments, a unix variable such as $PYTHONPATH is set as PYTHONPATH, without the dollar sign. PYTHONPATH is useful for specifying the location of library files. ---------------------------------------------------------------------------- 8.4. dedent syntax errors Tim <tim_one@email.msn.com> sez: the original content of this FAQ (below) makes little sense. The FAQ does not recommend using tabs, and Guido's Python Style Guide recommends 4 spaces for distributed Python code; this is also the Emacs python-mode default; see http://www.python.org/doc/essays/styleguide.html Under any editor mixing tabs and spaces is a bad idea. MSVC is no different in this respect, and is easily configured to use spaces: Take Tools -> Options -> Tabs, and for file type "Default" set "Tab size" and "Indent size" to 4, and select the "Insert spaces" radio button. If you suspect mixed tabs and spaces are causing problems in leading whitespace, run Python with the -t switch or, run Tools/Scripts/tabnanny.py to check a directory tree in batch mode. [original follows] The FAQ really means it when it suggests using tabs, not spaces, for indentation control. This may be a bigger problem in windows than in unix. For instance, the Microsoft Visual C++ programmers editor, in its default configuration, does not indicate whether the white space is spaces or tabs. If it is spaces, you are likely to get syntax errors not obviously related to the indentation. ---------------------------------------------------------------------------- 8.5. How do I emulate os.kill() in Windows? Use win32api: def kill(pid): """kill function for Win32""" import win32api handle = win32api.OpenProcess(1, 0, pid) return (0 != win32api.TerminateProcess(handle, 0)) ---------------------------------------------------------------------------- 8.6. Why does os.path.isdir() fail on NT shared directories? The solution appears to be always append the "\\" on the end of shared drives. >>> import os >>> os.path.isdir( '\\\\rorschach\\public') 0 >>> os.path.isdir( '\\\\rorschach\\public\\') 1 [Blake Winton responds:] I've had the same problem doing "Start >> Run" and then a directory on a shared drive. If I use "\\rorschach\public", it will fail, but if I use "\\rorschach\public\", it will work. For that matter, os.stat() does the same thing (well, it gives an error for "\\\\rorschach\\public", but you get the idea)... I've got a theory about why this happens, but it's only a theory. NT knows the difference between shared directories, and regular directories. "\\rorschach\public" isn't a directory, it's _really_ an IPC abstraction. This is sort of lended credence to by the fact that when you're mapping a network drive, you can't map "\\rorschach\public\utils", but only "\\rorschach\public". [Clarification by funkster@midwinter.com] It's not actually a Python question, as Python is working just fine; it's clearing up something a bit muddled about Windows networked drives. It helps to think of share points as being like drive letters. Example: k: is not a directory k:\ is a directory k:\media is a directory k:\media\ is not a directory The same rules apply if you substitute "k:" with "\\conky\foo": \\conky\foo is not a directory \\conky\foo\ is a directory \\conky\foo\media is a directory \\conky\foo\media\ is not a directory ---------------------------------------------------------------------------- 8.7. PyRun_SimpleFile() crashes on Windows but not on Unix I've seen a number of reports of PyRun_SimpleFile() failing in a Windows port of an application embedding Python that worked fine on Unix. PyRun_SimpleString() works fine on both platforms. I think this happens because the application was compiled with a different set of compiler flags than Python15.DLL. It seems that some compiler flags affect the standard I/O library in such a way that using different flags makes calls fail. You need to set it for multi-threaded DLL. ---------------------------------------------------------------------------- 8.8. Import of _tkinter fails on Windows 95/98 Sometimes, the import of _tkinter fails on Windows 95 or 98, complaining with a message like the following: ImportError: DLL load failed: One of the library files needed to run this application cannot be found. It could be that you haven't installed Tcl/Tk, but if you did install Tcl/Tk, and the Wish application works correctly, the problem may be that its installer didn't manage to edit the autoexec.bat file correctly. It tries to add a statement that changes the PATH environment variable to include the Tcl/Tk 'bin' subdirectory, but sometimes this edit doesn't quite work. Opening it with notepad usually reveals what the problem is. (One additional hint, noted by David Szafranski: you can't use long filenames here; e.g. use C:\PROGRA~1\Tcl\bin instead of C:\Program Files\Tcl\bin.) ---------------------------------------------------------------------------- 8.9. Can't extract the downloaded documentation on Windows Sometimes, when you download the documentation package to a Windows machine using a web browser, the file extension of the saved file ends up being .EXE. This is a mistake; the extension should be .TGZ. Simply rename the downloaded file to have the .TGZ extension, and WinZip will be able to handle it. (If your copy of WinZip doesn't, get a newer one from http://www.winzip.com.) ---------------------------------------------------------------------------- 8.10. Can't get Py_RunSimpleFile() to work. This is very sensitive to the compiler vendor, version and (perhaps) even options. If the FILE* structure in your embedding program isn't the same as is assumed by the Python interpreter it won't work. The Python 1.5.* DLLs (python15.dll) are all compiled with MS VC++ 5.0 and with multithreading-DLL options (/MD, I think). If you can't change compilers or flags, try using Py_RunSimpleString(). A trick to get it to run an arbitrary file is to construct a call to execfile() with the name of your file as argument. ---------------------------------------------------------------------------- 8.11. Where is Freeze for Windows? ("Freeze" is a program that allows you to ship a Python program as a single stand-alone executable file. It is not a compiler, your programs don't run any faster, but they are more easily distributable (to platforms with the same OS and CPU). Read the README file of the freeze program for more disclaimers.) You can use freeze on Windows, but you must download the source tree (see http://www.python.org/download/download_source.html). This is recommended for Python 1.5.2 (and betas thereof) only; older versions don't quite work. You need the Microsoft VC++ 5.0 compiler (maybe it works with 6.0 too). You probably need to build Python -- the project files are all in the PCbuild directory. The freeze program is in the Tools\freeze subdirectory of the source tree. ---------------------------------------------------------------------------- 8.12. Is a *.pyd file the same as a DLL? Yes, .pyd files are dll's. But there are a few differences. If you have a DLL named foo.pyd, then it must have a function initfoo(). You can then write Python "import foo", and Python will search for foo.pyd (as well as foo.py, foo.pyc) and if it finds it, will attempt to call initfoo() to initialize it. You do not link your .exe with foo.lib, as that would cause Windows to require the DLL to be present. Note that the search path for foo.pyc is PYTHONPATH, not the same as the path that Windows uses to search for foo.dll. Also, foo.pyd need not be present to run your program, whereas if you linked your program with a dll, the dll is required. Of course, foo.pyd is required if you want to say "import foo". In a dll, linkage is declared in the source code with __declspec(dllexport). In a .pyd, linkage is defined in a list of available functions. ---------------------------------------------------------------------------- 8.13. Missing cw3215mt.dll (or missing cw3215.dll) Sometimes, when using Tkinter on Windows, you get an error that cw3215mt.dll or cw3215.dll is missing. Cause: you have an old Tcl/Tk DLL built with cygwin in your path (probably C:\Windows). You must use the Tcl/Tk DLLs from the standard Tcl/Tk installation (Python 1.5.2 comes with one). ---------------------------------------------------------------------------- -- ----------- comp.lang.python.announce (moderated) ---------- Article Submission Address: python-announce@python.org Python Language Home Page: http://www.python.org/ Python Quick Help Index: http://www.python.org/Help.html ------------------------------------------------------------
participants (1)
-
Markus Fleck