17db96d56Sopenharmony_ci:tocdepth: 2
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci.. highlight:: none
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_ci.. _windows-faq:
67db96d56Sopenharmony_ci
77db96d56Sopenharmony_ci=====================
87db96d56Sopenharmony_ciPython on Windows FAQ
97db96d56Sopenharmony_ci=====================
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ci.. only:: html
127db96d56Sopenharmony_ci
137db96d56Sopenharmony_ci   .. contents::
147db96d56Sopenharmony_ci
157db96d56Sopenharmony_ci.. XXX need review for Python 3.
167db96d56Sopenharmony_ci   XXX need review for Windows Vista/Seven?
177db96d56Sopenharmony_ci
187db96d56Sopenharmony_ci.. _faq-run-program-under-windows:
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_ci
217db96d56Sopenharmony_ciHow do I run a Python program under Windows?
227db96d56Sopenharmony_ci--------------------------------------------
237db96d56Sopenharmony_ci
247db96d56Sopenharmony_ciThis is not necessarily a straightforward question. If you are already familiar
257db96d56Sopenharmony_ciwith running programs from the Windows command line then everything will seem
267db96d56Sopenharmony_ciobvious; otherwise, you might need a little more guidance.
277db96d56Sopenharmony_ci
287db96d56Sopenharmony_ciUnless you use some sort of integrated development environment, you will end up
297db96d56Sopenharmony_ci*typing* Windows commands into what is referred to as a
307db96d56Sopenharmony_ci"Command prompt window".  Usually you can create such a window from your
317db96d56Sopenharmony_cisearch bar by searching for ``cmd``.  You should be able to recognize
327db96d56Sopenharmony_ciwhen you have started such a window because you will see a Windows "command
337db96d56Sopenharmony_ciprompt", which usually looks like this:
347db96d56Sopenharmony_ci
357db96d56Sopenharmony_ci.. code-block:: doscon
367db96d56Sopenharmony_ci
377db96d56Sopenharmony_ci   C:\>
387db96d56Sopenharmony_ci
397db96d56Sopenharmony_ciThe letter may be different, and there might be other things after it, so you
407db96d56Sopenharmony_cimight just as easily see something like:
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci.. code-block:: doscon
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci   D:\YourName\Projects\Python>
457db96d56Sopenharmony_ci
467db96d56Sopenharmony_cidepending on how your computer has been set up and what else you have recently
477db96d56Sopenharmony_cidone with it.  Once you have started such a window, you are well on the way to
487db96d56Sopenharmony_cirunning Python programs.
497db96d56Sopenharmony_ci
507db96d56Sopenharmony_ciYou need to realize that your Python scripts have to be processed by another
517db96d56Sopenharmony_ciprogram called the Python *interpreter*.  The interpreter reads your script,
527db96d56Sopenharmony_cicompiles it into bytecodes, and then executes the bytecodes to run your
537db96d56Sopenharmony_ciprogram. So, how do you arrange for the interpreter to handle your Python?
547db96d56Sopenharmony_ci
557db96d56Sopenharmony_ciFirst, you need to make sure that your command window recognises the word
567db96d56Sopenharmony_ci"py" as an instruction to start the interpreter.  If you have opened a
577db96d56Sopenharmony_cicommand window, you should try entering the command ``py`` and hitting
587db96d56Sopenharmony_cireturn:
597db96d56Sopenharmony_ci
607db96d56Sopenharmony_ci.. code-block:: doscon
617db96d56Sopenharmony_ci
627db96d56Sopenharmony_ci   C:\Users\YourName> py
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ciYou should then see something like:
657db96d56Sopenharmony_ci
667db96d56Sopenharmony_ci.. code-block:: pycon
677db96d56Sopenharmony_ci
687db96d56Sopenharmony_ci   Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
697db96d56Sopenharmony_ci   Type "help", "copyright", "credits" or "license" for more information.
707db96d56Sopenharmony_ci   >>>
717db96d56Sopenharmony_ci
727db96d56Sopenharmony_ciYou have started the interpreter in "interactive mode". That means you can enter
737db96d56Sopenharmony_ciPython statements or expressions interactively and have them executed or
747db96d56Sopenharmony_cievaluated while you wait.  This is one of Python's strongest features.  Check it
757db96d56Sopenharmony_ciby entering a few expressions of your choice and seeing the results:
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ci.. code-block:: pycon
787db96d56Sopenharmony_ci
797db96d56Sopenharmony_ci    >>> print("Hello")
807db96d56Sopenharmony_ci    Hello
817db96d56Sopenharmony_ci    >>> "Hello" * 3
827db96d56Sopenharmony_ci    'HelloHelloHello'
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ciMany people use the interactive mode as a convenient yet highly programmable
857db96d56Sopenharmony_cicalculator.  When you want to end your interactive Python session,
867db96d56Sopenharmony_cicall the :func:`exit` function or hold the :kbd:`Ctrl` key down
877db96d56Sopenharmony_ciwhile you enter a :kbd:`Z`, then hit the ":kbd:`Enter`" key to get
887db96d56Sopenharmony_ciback to your Windows command prompt.
897db96d56Sopenharmony_ci
907db96d56Sopenharmony_ciYou may also find that you have a Start-menu entry such as :menuselection:`Start
917db96d56Sopenharmony_ci--> Programs --> Python 3.x --> Python (command line)` that results in you
927db96d56Sopenharmony_ciseeing the ``>>>`` prompt in a new window.  If so, the window will disappear
937db96d56Sopenharmony_ciafter you call the :func:`exit` function or enter the :kbd:`Ctrl-Z`
947db96d56Sopenharmony_cicharacter; Windows is running a single "python"
957db96d56Sopenharmony_cicommand in the window, and closes it when you terminate the interpreter.
967db96d56Sopenharmony_ci
977db96d56Sopenharmony_ciNow that we know the ``py`` command is recognized, you can give your
987db96d56Sopenharmony_ciPython script to it. You'll have to give either an absolute or a
997db96d56Sopenharmony_cirelative path to the Python script. Let's say your Python script is
1007db96d56Sopenharmony_cilocated in your desktop and is named ``hello.py``, and your command
1017db96d56Sopenharmony_ciprompt is nicely opened in your home directory so you're seeing something
1027db96d56Sopenharmony_cisimilar to::
1037db96d56Sopenharmony_ci
1047db96d56Sopenharmony_ci   C:\Users\YourName>
1057db96d56Sopenharmony_ci
1067db96d56Sopenharmony_ciSo now you'll ask the ``py`` command to give your script to Python by
1077db96d56Sopenharmony_cityping ``py`` followed by your script path::
1087db96d56Sopenharmony_ci
1097db96d56Sopenharmony_ci
1107db96d56Sopenharmony_ci   C:\Users\YourName> py Desktop\hello.py
1117db96d56Sopenharmony_ci   hello
1127db96d56Sopenharmony_ci
1137db96d56Sopenharmony_ciHow do I make Python scripts executable?
1147db96d56Sopenharmony_ci----------------------------------------
1157db96d56Sopenharmony_ci
1167db96d56Sopenharmony_ciOn Windows, the standard Python installer already associates the .py
1177db96d56Sopenharmony_ciextension with a file type (Python.File) and gives that file type an open
1187db96d56Sopenharmony_cicommand that runs the interpreter (``D:\Program Files\Python\python.exe "%1"
1197db96d56Sopenharmony_ci%*``).  This is enough to make scripts executable from the command prompt as
1207db96d56Sopenharmony_ci'foo.py'.  If you'd rather be able to execute the script by simple typing 'foo'
1217db96d56Sopenharmony_ciwith no extension you need to add .py to the PATHEXT environment variable.
1227db96d56Sopenharmony_ci
1237db96d56Sopenharmony_ciWhy does Python sometimes take so long to start?
1247db96d56Sopenharmony_ci------------------------------------------------
1257db96d56Sopenharmony_ci
1267db96d56Sopenharmony_ciUsually Python starts very quickly on Windows, but occasionally there are bug
1277db96d56Sopenharmony_cireports that Python suddenly begins to take a long time to start up.  This is
1287db96d56Sopenharmony_cimade even more puzzling because Python will work fine on other Windows systems
1297db96d56Sopenharmony_ciwhich appear to be configured identically.
1307db96d56Sopenharmony_ci
1317db96d56Sopenharmony_ciThe problem may be caused by a misconfiguration of virus checking software on
1327db96d56Sopenharmony_cithe problem machine.  Some virus scanners have been known to introduce startup
1337db96d56Sopenharmony_cioverhead of two orders of magnitude when the scanner is configured to monitor
1347db96d56Sopenharmony_ciall reads from the filesystem.  Try checking the configuration of virus scanning
1357db96d56Sopenharmony_cisoftware on your systems to ensure that they are indeed configured identically.
1367db96d56Sopenharmony_ciMcAfee, when configured to scan all file system read activity, is a particular
1377db96d56Sopenharmony_cioffender.
1387db96d56Sopenharmony_ci
1397db96d56Sopenharmony_ci
1407db96d56Sopenharmony_ciHow do I make an executable from a Python script?
1417db96d56Sopenharmony_ci-------------------------------------------------
1427db96d56Sopenharmony_ci
1437db96d56Sopenharmony_ciSee :ref:`faq-create-standalone-binary` for a list of tools that can be used to
1447db96d56Sopenharmony_cimake executables.
1457db96d56Sopenharmony_ci
1467db96d56Sopenharmony_ci
1477db96d56Sopenharmony_ciIs a ``*.pyd`` file the same as a DLL?
1487db96d56Sopenharmony_ci--------------------------------------
1497db96d56Sopenharmony_ci
1507db96d56Sopenharmony_ciYes, .pyd files are dll's, but there are a few differences.  If you have a DLL
1517db96d56Sopenharmony_cinamed ``foo.pyd``, then it must have a function ``PyInit_foo()``.  You can then
1527db96d56Sopenharmony_ciwrite Python "import foo", and Python will search for foo.pyd (as well as
1537db96d56Sopenharmony_cifoo.py, foo.pyc) and if it finds it, will attempt to call ``PyInit_foo()`` to
1547db96d56Sopenharmony_ciinitialize it.  You do not link your .exe with foo.lib, as that would cause
1557db96d56Sopenharmony_ciWindows to require the DLL to be present.
1567db96d56Sopenharmony_ci
1577db96d56Sopenharmony_ciNote that the search path for foo.pyd is PYTHONPATH, not the same as the path
1587db96d56Sopenharmony_cithat Windows uses to search for foo.dll.  Also, foo.pyd need not be present to
1597db96d56Sopenharmony_cirun your program, whereas if you linked your program with a dll, the dll is
1607db96d56Sopenharmony_cirequired.  Of course, foo.pyd is required if you want to say ``import foo``.  In
1617db96d56Sopenharmony_cia DLL, linkage is declared in the source code with ``__declspec(dllexport)``.
1627db96d56Sopenharmony_ciIn a .pyd, linkage is defined in a list of available functions.
1637db96d56Sopenharmony_ci
1647db96d56Sopenharmony_ci
1657db96d56Sopenharmony_ciHow can I embed Python into a Windows application?
1667db96d56Sopenharmony_ci--------------------------------------------------
1677db96d56Sopenharmony_ci
1687db96d56Sopenharmony_ciEmbedding the Python interpreter in a Windows app can be summarized as follows:
1697db96d56Sopenharmony_ci
1707db96d56Sopenharmony_ci1. Do **not** build Python into your .exe file directly.  On Windows, Python must
1717db96d56Sopenharmony_ci   be a DLL to handle importing modules that are themselves DLL's.  (This is the
1727db96d56Sopenharmony_ci   first key undocumented fact.)  Instead, link to :file:`python{NN}.dll`; it is
1737db96d56Sopenharmony_ci   typically installed in ``C:\Windows\System``.  *NN* is the Python version, a
1747db96d56Sopenharmony_ci   number such as "33" for Python 3.3.
1757db96d56Sopenharmony_ci
1767db96d56Sopenharmony_ci   You can link to Python in two different ways.  Load-time linking means
1777db96d56Sopenharmony_ci   linking against :file:`python{NN}.lib`, while run-time linking means linking
1787db96d56Sopenharmony_ci   against :file:`python{NN}.dll`.  (General note: :file:`python{NN}.lib` is the
1797db96d56Sopenharmony_ci   so-called "import lib" corresponding to :file:`python{NN}.dll`.  It merely
1807db96d56Sopenharmony_ci   defines symbols for the linker.)
1817db96d56Sopenharmony_ci
1827db96d56Sopenharmony_ci   Run-time linking greatly simplifies link options; everything happens at run
1837db96d56Sopenharmony_ci   time.  Your code must load :file:`python{NN}.dll` using the Windows
1847db96d56Sopenharmony_ci   ``LoadLibraryEx()`` routine.  The code must also use access routines and data
1857db96d56Sopenharmony_ci   in :file:`python{NN}.dll` (that is, Python's C API's) using pointers obtained
1867db96d56Sopenharmony_ci   by the Windows ``GetProcAddress()`` routine.  Macros can make using these
1877db96d56Sopenharmony_ci   pointers transparent to any C code that calls routines in Python's C API.
1887db96d56Sopenharmony_ci
1897db96d56Sopenharmony_ci   .. XXX what about static linking?
1907db96d56Sopenharmony_ci
1917db96d56Sopenharmony_ci2. If you use SWIG, it is easy to create a Python "extension module" that will
1927db96d56Sopenharmony_ci   make the app's data and methods available to Python.  SWIG will handle just
1937db96d56Sopenharmony_ci   about all the grungy details for you.  The result is C code that you link
1947db96d56Sopenharmony_ci   *into* your .exe file (!)  You do **not** have to create a DLL file, and this
1957db96d56Sopenharmony_ci   also simplifies linking.
1967db96d56Sopenharmony_ci
1977db96d56Sopenharmony_ci3. SWIG will create an init function (a C function) whose name depends on the
1987db96d56Sopenharmony_ci   name of the extension module.  For example, if the name of the module is leo,
1997db96d56Sopenharmony_ci   the init function will be called initleo().  If you use SWIG shadow classes,
2007db96d56Sopenharmony_ci   as you should, the init function will be called initleoc().  This initializes
2017db96d56Sopenharmony_ci   a mostly hidden helper class used by the shadow class.
2027db96d56Sopenharmony_ci
2037db96d56Sopenharmony_ci   The reason you can link the C code in step 2 into your .exe file is that
2047db96d56Sopenharmony_ci   calling the initialization function is equivalent to importing the module
2057db96d56Sopenharmony_ci   into Python! (This is the second key undocumented fact.)
2067db96d56Sopenharmony_ci
2077db96d56Sopenharmony_ci4. In short, you can use the following code to initialize the Python interpreter
2087db96d56Sopenharmony_ci   with your extension module.
2097db96d56Sopenharmony_ci
2107db96d56Sopenharmony_ci   .. code-block:: c
2117db96d56Sopenharmony_ci
2127db96d56Sopenharmony_ci      #include <Python.h>
2137db96d56Sopenharmony_ci      ...
2147db96d56Sopenharmony_ci      Py_Initialize();  // Initialize Python.
2157db96d56Sopenharmony_ci      initmyAppc();  // Initialize (import) the helper class.
2167db96d56Sopenharmony_ci      PyRun_SimpleString("import myApp");  // Import the shadow class.
2177db96d56Sopenharmony_ci
2187db96d56Sopenharmony_ci5. There are two problems with Python's C API which will become apparent if you
2197db96d56Sopenharmony_ci   use a compiler other than MSVC, the compiler used to build pythonNN.dll.
2207db96d56Sopenharmony_ci
2217db96d56Sopenharmony_ci   Problem 1: The so-called "Very High Level" functions that take ``FILE *``
2227db96d56Sopenharmony_ci   arguments will not work in a multi-compiler environment because each
2237db96d56Sopenharmony_ci   compiler's notion of a ``struct FILE`` will be different.  From an implementation
2247db96d56Sopenharmony_ci   standpoint these are very low level functions.
2257db96d56Sopenharmony_ci
2267db96d56Sopenharmony_ci   Problem 2: SWIG generates the following code when generating wrappers to void
2277db96d56Sopenharmony_ci   functions:
2287db96d56Sopenharmony_ci
2297db96d56Sopenharmony_ci   .. code-block:: c
2307db96d56Sopenharmony_ci
2317db96d56Sopenharmony_ci      Py_INCREF(Py_None);
2327db96d56Sopenharmony_ci      _resultobj = Py_None;
2337db96d56Sopenharmony_ci      return _resultobj;
2347db96d56Sopenharmony_ci
2357db96d56Sopenharmony_ci   Alas, Py_None is a macro that expands to a reference to a complex data
2367db96d56Sopenharmony_ci   structure called _Py_NoneStruct inside pythonNN.dll.  Again, this code will
2377db96d56Sopenharmony_ci   fail in a mult-compiler environment.  Replace such code by:
2387db96d56Sopenharmony_ci
2397db96d56Sopenharmony_ci   .. code-block:: c
2407db96d56Sopenharmony_ci
2417db96d56Sopenharmony_ci      return Py_BuildValue("");
2427db96d56Sopenharmony_ci
2437db96d56Sopenharmony_ci   It may be possible to use SWIG's ``%typemap`` command to make the change
2447db96d56Sopenharmony_ci   automatically, though I have not been able to get this to work (I'm a
2457db96d56Sopenharmony_ci   complete SWIG newbie).
2467db96d56Sopenharmony_ci
2477db96d56Sopenharmony_ci6. Using a Python shell script to put up a Python interpreter window from inside
2487db96d56Sopenharmony_ci   your Windows app is not a good idea; the resulting window will be independent
2497db96d56Sopenharmony_ci   of your app's windowing system.  Rather, you (or the wxPythonWindow class)
2507db96d56Sopenharmony_ci   should create a "native" interpreter window.  It is easy to connect that
2517db96d56Sopenharmony_ci   window to the Python interpreter.  You can redirect Python's i/o to _any_
2527db96d56Sopenharmony_ci   object that supports read and write, so all you need is a Python object
2537db96d56Sopenharmony_ci   (defined in your extension module) that contains read() and write() methods.
2547db96d56Sopenharmony_ci
2557db96d56Sopenharmony_ciHow do I keep editors from inserting tabs into my Python source?
2567db96d56Sopenharmony_ci----------------------------------------------------------------
2577db96d56Sopenharmony_ci
2587db96d56Sopenharmony_ciThe FAQ does not recommend using tabs, and the Python style guide, :pep:`8`,
2597db96d56Sopenharmony_cirecommends 4 spaces for distributed Python code; this is also the Emacs
2607db96d56Sopenharmony_cipython-mode default.
2617db96d56Sopenharmony_ci
2627db96d56Sopenharmony_ciUnder any editor, mixing tabs and spaces is a bad idea.  MSVC is no different in
2637db96d56Sopenharmony_cithis respect, and is easily configured to use spaces: Take :menuselection:`Tools
2647db96d56Sopenharmony_ci--> Options --> Tabs`, and for file type "Default" set "Tab size" and "Indent
2657db96d56Sopenharmony_cisize" to 4, and select the "Insert spaces" radio button.
2667db96d56Sopenharmony_ci
2677db96d56Sopenharmony_ciPython raises :exc:`IndentationError` or :exc:`TabError` if mixed tabs
2687db96d56Sopenharmony_ciand spaces are causing problems in leading whitespace.
2697db96d56Sopenharmony_ciYou may also run the :mod:`tabnanny` module to check a directory tree
2707db96d56Sopenharmony_ciin batch mode.
2717db96d56Sopenharmony_ci
2727db96d56Sopenharmony_ci
2737db96d56Sopenharmony_ciHow do I check for a keypress without blocking?
2747db96d56Sopenharmony_ci-----------------------------------------------
2757db96d56Sopenharmony_ci
2767db96d56Sopenharmony_ciUse the :mod:`msvcrt` module.  This is a standard Windows-specific extension module.
2777db96d56Sopenharmony_ciIt defines a function ``kbhit()`` which checks whether a keyboard hit is
2787db96d56Sopenharmony_cipresent, and ``getch()`` which gets one character without echoing it.
2797db96d56Sopenharmony_ci
2807db96d56Sopenharmony_ciHow do I solve the missing api-ms-win-crt-runtime-l1-1-0.dll error?
2817db96d56Sopenharmony_ci-------------------------------------------------------------------
2827db96d56Sopenharmony_ci
2837db96d56Sopenharmony_ciThis can occur on Python 3.5 and later when using Windows 8.1 or earlier without all updates having been installed.
2847db96d56Sopenharmony_ciFirst ensure your operating system is supported and is up to date, and if that does not resolve the issue,
2857db96d56Sopenharmony_civisit the `Microsoft support page <https://support.microsoft.com/en-us/help/3118401/>`_
2867db96d56Sopenharmony_cifor guidance on manually installing the C Runtime update.
287