gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

585 lines
38KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="Python on Windows FAQ" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/faq/windows.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Contents: Python on Windows FAQ- How do I run a Python program under Windows?, How do I make Python scripts executable?, Why does Python sometimes take so long to start?, How do I make an executabl..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Contents: Python on Windows FAQ- How do I run a Python program under Windows?, How do I make Python scripts executable?, Why does Python sometimes take so long to start?, How do I make an executabl..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Python on Windows FAQ &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Graphic User Interface FAQ" href="gui.html" />
  33. <link rel="prev" title="Extending/Embedding FAQ" href="extending.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/faq/windows.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Python on Windows FAQ</a><ul>
  86. <li><a class="reference internal" href="#how-do-i-run-a-python-program-under-windows">How do I run a Python program under Windows?</a></li>
  87. <li><a class="reference internal" href="#how-do-i-make-python-scripts-executable">How do I make Python scripts executable?</a></li>
  88. <li><a class="reference internal" href="#why-does-python-sometimes-take-so-long-to-start">Why does Python sometimes take so long to start?</a></li>
  89. <li><a class="reference internal" href="#how-do-i-make-an-executable-from-a-python-script">How do I make an executable from a Python script?</a></li>
  90. <li><a class="reference internal" href="#is-a-pyd-file-the-same-as-a-dll">Is a <code class="docutils literal notranslate"><span class="pre">*.pyd</span></code> file the same as a DLL?</a></li>
  91. <li><a class="reference internal" href="#how-can-i-embed-python-into-a-windows-application">How can I embed Python into a Windows application?</a></li>
  92. <li><a class="reference internal" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source">How do I keep editors from inserting tabs into my Python source?</a></li>
  93. <li><a class="reference internal" href="#how-do-i-check-for-a-keypress-without-blocking">How do I check for a keypress without blocking?</a></li>
  94. <li><a class="reference internal" href="#how-do-i-solve-the-missing-api-ms-win-crt-runtime-l1-1-0-dll-error">How do I solve the missing api-ms-win-crt-runtime-l1-1-0.dll error?</a></li>
  95. </ul>
  96. </li>
  97. </ul>
  98. </div>
  99. <div>
  100. <h4>Previous topic</h4>
  101. <p class="topless"><a href="extending.html"
  102. title="previous chapter">Extending/Embedding FAQ</a></p>
  103. </div>
  104. <div>
  105. <h4>Next topic</h4>
  106. <p class="topless"><a href="gui.html"
  107. title="next chapter">Graphic User Interface FAQ</a></p>
  108. </div>
  109. <div role="note" aria-label="source link">
  110. <h3>This Page</h3>
  111. <ul class="this-page-menu">
  112. <li><a href="../bugs.html">Report a Bug</a></li>
  113. <li>
  114. <a href="https://github.com/python/cpython/blob/main/Doc/faq/windows.rst"
  115. rel="nofollow">Show Source
  116. </a>
  117. </li>
  118. </ul>
  119. </div>
  120. </nav>
  121. </div>
  122. </div>
  123. <div class="related" role="navigation" aria-label="related navigation">
  124. <h3>Navigation</h3>
  125. <ul>
  126. <li class="right" style="margin-right: 10px">
  127. <a href="../genindex.html" title="General Index"
  128. accesskey="I">index</a></li>
  129. <li class="right" >
  130. <a href="../py-modindex.html" title="Python Module Index"
  131. >modules</a> |</li>
  132. <li class="right" >
  133. <a href="gui.html" title="Graphic User Interface FAQ"
  134. accesskey="N">next</a> |</li>
  135. <li class="right" >
  136. <a href="extending.html" title="Extending/Embedding FAQ"
  137. accesskey="P">previous</a> |</li>
  138. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  139. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  140. <li class="switchers">
  141. <div class="language_switcher_placeholder"></div>
  142. <div class="version_switcher_placeholder"></div>
  143. </li>
  144. <li>
  145. </li>
  146. <li id="cpython-language-and-version">
  147. <a href="../index.html">3.12.3 Documentation</a> &#187;
  148. </li>
  149. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python Frequently Asked Questions</a> &#187;</li>
  150. <li class="nav-item nav-item-this"><a href="">Python on Windows FAQ</a></li>
  151. <li class="right">
  152. <div class="inline-search" role="search">
  153. <form class="inline-search" action="../search.html" method="get">
  154. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  155. <input type="submit" value="Go" />
  156. </form>
  157. </div>
  158. |
  159. </li>
  160. <li class="right">
  161. <label class="theme-selector-label">
  162. Theme
  163. <select class="theme-selector" oninput="activateTheme(this.value)">
  164. <option value="auto" selected>Auto</option>
  165. <option value="light">Light</option>
  166. <option value="dark">Dark</option>
  167. </select>
  168. </label> |</li>
  169. </ul>
  170. </div>
  171. <div class="document">
  172. <div class="documentwrapper">
  173. <div class="bodywrapper">
  174. <div class="body" role="main">
  175. <section id="python-on-windows-faq">
  176. <span id="windows-faq"></span><h1><a class="toc-backref" href="#id1" role="doc-backlink">Python on Windows FAQ</a><a class="headerlink" href="#python-on-windows-faq" title="Link to this heading">¶</a></h1>
  177. <nav class="contents" id="contents">
  178. <p class="topic-title">Contents</p>
  179. <ul class="simple">
  180. <li><p><a class="reference internal" href="#python-on-windows-faq" id="id1">Python on Windows FAQ</a></p>
  181. <ul>
  182. <li><p><a class="reference internal" href="#how-do-i-run-a-python-program-under-windows" id="id2">How do I run a Python program under Windows?</a></p></li>
  183. <li><p><a class="reference internal" href="#how-do-i-make-python-scripts-executable" id="id3">How do I make Python scripts executable?</a></p></li>
  184. <li><p><a class="reference internal" href="#why-does-python-sometimes-take-so-long-to-start" id="id4">Why does Python sometimes take so long to start?</a></p></li>
  185. <li><p><a class="reference internal" href="#how-do-i-make-an-executable-from-a-python-script" id="id5">How do I make an executable from a Python script?</a></p></li>
  186. <li><p><a class="reference internal" href="#is-a-pyd-file-the-same-as-a-dll" id="id6">Is a <code class="docutils literal notranslate"><span class="pre">*.pyd</span></code> file the same as a DLL?</a></p></li>
  187. <li><p><a class="reference internal" href="#how-can-i-embed-python-into-a-windows-application" id="id7">How can I embed Python into a Windows application?</a></p></li>
  188. <li><p><a class="reference internal" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source" id="id8">How do I keep editors from inserting tabs into my Python source?</a></p></li>
  189. <li><p><a class="reference internal" href="#how-do-i-check-for-a-keypress-without-blocking" id="id9">How do I check for a keypress without blocking?</a></p></li>
  190. <li><p><a class="reference internal" href="#how-do-i-solve-the-missing-api-ms-win-crt-runtime-l1-1-0-dll-error" id="id10">How do I solve the missing api-ms-win-crt-runtime-l1-1-0.dll error?</a></p></li>
  191. </ul>
  192. </li>
  193. </ul>
  194. </nav>
  195. <section id="how-do-i-run-a-python-program-under-windows">
  196. <span id="faq-run-program-under-windows"></span><h2><a class="toc-backref" href="#id2" role="doc-backlink">How do I run a Python program under Windows?</a><a class="headerlink" href="#how-do-i-run-a-python-program-under-windows" title="Link to this heading">¶</a></h2>
  197. <p>This is not necessarily a straightforward question. If you are already familiar
  198. with running programs from the Windows command line then everything will seem
  199. obvious; otherwise, you might need a little more guidance.</p>
  200. <p>Unless you use some sort of integrated development environment, you will end up
  201. <em>typing</em> Windows commands into what is referred to as a
  202. “Command prompt window”. Usually you can create such a window from your
  203. search bar by searching for <code class="docutils literal notranslate"><span class="pre">cmd</span></code>. You should be able to recognize
  204. when you have started such a window because you will see a Windows “command
  205. prompt”, which usually looks like this:</p>
  206. <div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">C:\&gt;</span>
  207. </pre></div>
  208. </div>
  209. <p>The letter may be different, and there might be other things after it, so you
  210. might just as easily see something like:</p>
  211. <div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">D:\YourName\Projects\Python&gt;</span>
  212. </pre></div>
  213. </div>
  214. <p>depending on how your computer has been set up and what else you have recently
  215. done with it. Once you have started such a window, you are well on the way to
  216. running Python programs.</p>
  217. <p>You need to realize that your Python scripts have to be processed by another
  218. program called the Python <em>interpreter</em>. The interpreter reads your script,
  219. compiles it into bytecodes, and then executes the bytecodes to run your
  220. program. So, how do you arrange for the interpreter to handle your Python?</p>
  221. <p>First, you need to make sure that your command window recognises the word
  222. “py” as an instruction to start the interpreter. If you have opened a
  223. command window, you should try entering the command <code class="docutils literal notranslate"><span class="pre">py</span></code> and hitting
  224. return:</p>
  225. <div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">C:\Users\YourName&gt;</span> py
  226. </pre></div>
  227. </div>
  228. <p>You should then see something like:</p>
  229. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="go">Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32</span>
  230. <span class="go">Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.</span>
  231. <span class="gp">&gt;&gt;&gt;</span>
  232. </pre></div>
  233. </div>
  234. <p>You have started the interpreter in “interactive mode”. That means you can enter
  235. Python statements or expressions interactively and have them executed or
  236. evaluated while you wait. This is one of Python’s strongest features. Check it
  237. by entering a few expressions of your choice and seeing the results:</p>
  238. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Hello&quot;</span><span class="p">)</span>
  239. <span class="go">Hello</span>
  240. <span class="gp">&gt;&gt;&gt; </span><span class="s2">&quot;Hello&quot;</span> <span class="o">*</span> <span class="mi">3</span>
  241. <span class="go">&#39;HelloHelloHello&#39;</span>
  242. </pre></div>
  243. </div>
  244. <p>Many people use the interactive mode as a convenient yet highly programmable
  245. calculator. When you want to end your interactive Python session,
  246. call the <a class="reference internal" href="../library/constants.html#exit" title="exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">exit()</span></code></a> function or hold the <kbd class="kbd docutils literal notranslate">Ctrl</kbd> key down
  247. while you enter a <kbd class="kbd docutils literal notranslate">Z</kbd>, then hit the “<kbd class="kbd docutils literal notranslate">Enter</kbd>” key to get
  248. back to your Windows command prompt.</p>
  249. <p>You may also find that you have a Start-menu entry such as <span class="menuselection">Start
  250. ‣ Programs ‣ Python 3.x ‣ Python (command line)</span> that results in you
  251. seeing the <code class="docutils literal notranslate"><span class="pre">&gt;&gt;&gt;</span></code> prompt in a new window. If so, the window will disappear
  252. after you call the <a class="reference internal" href="../library/constants.html#exit" title="exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">exit()</span></code></a> function or enter the <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Ctrl</kbd>-<kbd class="kbd docutils literal notranslate">Z</kbd></kbd>
  253. character; Windows is running a single “python”
  254. command in the window, and closes it when you terminate the interpreter.</p>
  255. <p>Now that we know the <code class="docutils literal notranslate"><span class="pre">py</span></code> command is recognized, you can give your
  256. Python script to it. You’ll have to give either an absolute or a
  257. relative path to the Python script. Let’s say your Python script is
  258. located in your desktop and is named <code class="docutils literal notranslate"><span class="pre">hello.py</span></code>, and your command
  259. prompt is nicely opened in your home directory so you’re seeing something
  260. similar to:</p>
  261. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>C:\Users\YourName&gt;
  262. </pre></div>
  263. </div>
  264. <p>So now you’ll ask the <code class="docutils literal notranslate"><span class="pre">py</span></code> command to give your script to Python by
  265. typing <code class="docutils literal notranslate"><span class="pre">py</span></code> followed by your script path:</p>
  266. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>C:\Users\YourName&gt; py Desktop\hello.py
  267. hello
  268. </pre></div>
  269. </div>
  270. </section>
  271. <section id="how-do-i-make-python-scripts-executable">
  272. <h2><a class="toc-backref" href="#id3" role="doc-backlink">How do I make Python scripts executable?</a><a class="headerlink" href="#how-do-i-make-python-scripts-executable" title="Link to this heading">¶</a></h2>
  273. <p>On Windows, the standard Python installer already associates the .py
  274. extension with a file type (Python.File) and gives that file type an open
  275. command that runs the interpreter (<code class="docutils literal notranslate"><span class="pre">D:\Program</span> <span class="pre">Files\Python\python.exe</span> <span class="pre">&quot;%1&quot;</span>
  276. <span class="pre">%*</span></code>). This is enough to make scripts executable from the command prompt as
  277. ‘foo.py’. If you’d rather be able to execute the script by simple typing ‘foo’
  278. with no extension you need to add .py to the PATHEXT environment variable.</p>
  279. </section>
  280. <section id="why-does-python-sometimes-take-so-long-to-start">
  281. <h2><a class="toc-backref" href="#id4" role="doc-backlink">Why does Python sometimes take so long to start?</a><a class="headerlink" href="#why-does-python-sometimes-take-so-long-to-start" title="Link to this heading">¶</a></h2>
  282. <p>Usually Python starts very quickly on Windows, but occasionally there are bug
  283. reports that Python suddenly begins to take a long time to start up. This is
  284. made even more puzzling because Python will work fine on other Windows systems
  285. which appear to be configured identically.</p>
  286. <p>The problem may be caused by a misconfiguration of virus checking software on
  287. the problem machine. Some virus scanners have been known to introduce startup
  288. overhead of two orders of magnitude when the scanner is configured to monitor
  289. all reads from the filesystem. Try checking the configuration of virus scanning
  290. software on your systems to ensure that they are indeed configured identically.
  291. McAfee, when configured to scan all file system read activity, is a particular
  292. offender.</p>
  293. </section>
  294. <section id="how-do-i-make-an-executable-from-a-python-script">
  295. <h2><a class="toc-backref" href="#id5" role="doc-backlink">How do I make an executable from a Python script?</a><a class="headerlink" href="#how-do-i-make-an-executable-from-a-python-script" title="Link to this heading">¶</a></h2>
  296. <p>See <a class="reference internal" href="programming.html#faq-create-standalone-binary"><span class="std std-ref">How can I create a stand-alone binary from a Python script?</span></a> for a list of tools that can be used to
  297. make executables.</p>
  298. </section>
  299. <section id="is-a-pyd-file-the-same-as-a-dll">
  300. <h2><a class="toc-backref" href="#id6" role="doc-backlink">Is a <code class="docutils literal notranslate"><span class="pre">*.pyd</span></code> file the same as a DLL?</a><a class="headerlink" href="#is-a-pyd-file-the-same-as-a-dll" title="Link to this heading">¶</a></h2>
  301. <p>Yes, .pyd files are dll’s, but there are a few differences. If you have a DLL
  302. named <code class="docutils literal notranslate"><span class="pre">foo.pyd</span></code>, then it must have a function <code class="docutils literal notranslate"><span class="pre">PyInit_foo()</span></code>. You can then
  303. write Python “import foo”, and Python will search for foo.pyd (as well as
  304. foo.py, foo.pyc) and if it finds it, will attempt to call <code class="docutils literal notranslate"><span class="pre">PyInit_foo()</span></code> to
  305. initialize it. You do not link your .exe with foo.lib, as that would cause
  306. Windows to require the DLL to be present.</p>
  307. <p>Note that the search path for foo.pyd is PYTHONPATH, not the same as the path
  308. that Windows uses to search for foo.dll. Also, foo.pyd need not be present to
  309. run your program, whereas if you linked your program with a dll, the dll is
  310. required. Of course, foo.pyd is required if you want to say <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">foo</span></code>. In
  311. a DLL, linkage is declared in the source code with <code class="docutils literal notranslate"><span class="pre">__declspec(dllexport)</span></code>.
  312. In a .pyd, linkage is defined in a list of available functions.</p>
  313. </section>
  314. <section id="how-can-i-embed-python-into-a-windows-application">
  315. <h2><a class="toc-backref" href="#id7" role="doc-backlink">How can I embed Python into a Windows application?</a><a class="headerlink" href="#how-can-i-embed-python-into-a-windows-application" title="Link to this heading">¶</a></h2>
  316. <p>Embedding the Python interpreter in a Windows app can be summarized as follows:</p>
  317. <ol class="arabic">
  318. <li><p>Do <strong>not</strong> build Python into your .exe file directly. On Windows, Python must
  319. be a DLL to handle importing modules that are themselves DLL’s. (This is the
  320. first key undocumented fact.) Instead, link to <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code>; it is
  321. typically installed in <code class="docutils literal notranslate"><span class="pre">C:\Windows\System</span></code>. <em>NN</em> is the Python version, a
  322. number such as “33” for Python 3.3.</p>
  323. <p>You can link to Python in two different ways. Load-time linking means
  324. linking against <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.lib</span></code>, while run-time linking means linking
  325. against <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code>. (General note: <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.lib</span></code> is the
  326. so-called “import lib” corresponding to <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code>. It merely
  327. defines symbols for the linker.)</p>
  328. <p>Run-time linking greatly simplifies link options; everything happens at run
  329. time. Your code must load <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code> using the Windows
  330. <code class="docutils literal notranslate"><span class="pre">LoadLibraryEx()</span></code> routine. The code must also use access routines and data
  331. in <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code> (that is, Python’s C API’s) using pointers obtained
  332. by the Windows <code class="docutils literal notranslate"><span class="pre">GetProcAddress()</span></code> routine. Macros can make using these
  333. pointers transparent to any C code that calls routines in Python’s C API.</p>
  334. </li>
  335. <li><p>If you use SWIG, it is easy to create a Python “extension module” that will
  336. make the app’s data and methods available to Python. SWIG will handle just
  337. about all the grungy details for you. The result is C code that you link
  338. <em>into</em> your .exe file (!) You do <strong>not</strong> have to create a DLL file, and this
  339. also simplifies linking.</p></li>
  340. <li><p>SWIG will create an init function (a C function) whose name depends on the
  341. name of the extension module. For example, if the name of the module is leo,
  342. the init function will be called initleo(). If you use SWIG shadow classes,
  343. as you should, the init function will be called initleoc(). This initializes
  344. a mostly hidden helper class used by the shadow class.</p>
  345. <p>The reason you can link the C code in step 2 into your .exe file is that
  346. calling the initialization function is equivalent to importing the module
  347. into Python! (This is the second key undocumented fact.)</p>
  348. </li>
  349. <li><p>In short, you can use the following code to initialize the Python interpreter
  350. with your extension module.</p>
  351. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;Python.h&gt;</span>
  352. <span class="p">...</span>
  353. <span class="n">Py_Initialize</span><span class="p">();</span><span class="w"> </span><span class="c1">// Initialize Python.</span>
  354. <span class="n">initmyAppc</span><span class="p">();</span><span class="w"> </span><span class="c1">// Initialize (import) the helper class.</span>
  355. <span class="n">PyRun_SimpleString</span><span class="p">(</span><span class="s">&quot;import myApp&quot;</span><span class="p">);</span><span class="w"> </span><span class="c1">// Import the shadow class.</span>
  356. </pre></div>
  357. </div>
  358. </li>
  359. <li><p>There are two problems with Python’s C API which will become apparent if you
  360. use a compiler other than MSVC, the compiler used to build pythonNN.dll.</p>
  361. <p>Problem 1: The so-called “Very High Level” functions that take <code class="docutils literal notranslate"><span class="pre">FILE</span> <span class="pre">*</span></code>
  362. arguments will not work in a multi-compiler environment because each
  363. compiler’s notion of a <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">FILE</span></code> will be different. From an implementation
  364. standpoint these are very low level functions.</p>
  365. <p>Problem 2: SWIG generates the following code when generating wrappers to void
  366. functions:</p>
  367. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_INCREF</span><span class="p">(</span><span class="n">Py_None</span><span class="p">);</span>
  368. <span class="n">_resultobj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_None</span><span class="p">;</span>
  369. <span class="k">return</span><span class="w"> </span><span class="n">_resultobj</span><span class="p">;</span>
  370. </pre></div>
  371. </div>
  372. <p>Alas, Py_None is a macro that expands to a reference to a complex data
  373. structure called _Py_NoneStruct inside pythonNN.dll. Again, this code will
  374. fail in a mult-compiler environment. Replace such code by:</p>
  375. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">return</span><span class="w"> </span><span class="n">Py_BuildValue</span><span class="p">(</span><span class="s">&quot;&quot;</span><span class="p">);</span>
  376. </pre></div>
  377. </div>
  378. <p>It may be possible to use SWIG’s <code class="docutils literal notranslate"><span class="pre">%typemap</span></code> command to make the change
  379. automatically, though I have not been able to get this to work (I’m a
  380. complete SWIG newbie).</p>
  381. </li>
  382. <li><p>Using a Python shell script to put up a Python interpreter window from inside
  383. your Windows app is not a good idea; the resulting window will be independent
  384. of your app’s windowing system. Rather, you (or the wxPythonWindow class)
  385. should create a “native” interpreter window. It is easy to connect that
  386. window to the Python interpreter. You can redirect Python’s i/o to _any_
  387. object that supports read and write, so all you need is a Python object
  388. (defined in your extension module) that contains read() and write() methods.</p></li>
  389. </ol>
  390. </section>
  391. <section id="how-do-i-keep-editors-from-inserting-tabs-into-my-python-source">
  392. <h2><a class="toc-backref" href="#id8" role="doc-backlink">How do I keep editors from inserting tabs into my Python source?</a><a class="headerlink" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source" title="Link to this heading">¶</a></h2>
  393. <p>The FAQ does not recommend using tabs, and the Python style guide, <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0008/"><strong>PEP 8</strong></a>,
  394. recommends 4 spaces for distributed Python code; this is also the Emacs
  395. python-mode default.</p>
  396. <p>Under any editor, mixing tabs and spaces is a bad idea. MSVC is no different in
  397. this respect, and is easily configured to use spaces: Take <span class="menuselection">Tools
  398. ‣ Options ‣ Tabs</span>, and for file type “Default” set “Tab size” and “Indent
  399. size” to 4, and select the “Insert spaces” radio button.</p>
  400. <p>Python raises <a class="reference internal" href="../library/exceptions.html#IndentationError" title="IndentationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndentationError</span></code></a> or <a class="reference internal" href="../library/exceptions.html#TabError" title="TabError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TabError</span></code></a> if mixed tabs
  401. and spaces are causing problems in leading whitespace.
  402. You may also run the <a class="reference internal" href="../library/tabnanny.html#module-tabnanny" title="tabnanny: Tool for detecting white space related problems in Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tabnanny</span></code></a> module to check a directory tree
  403. in batch mode.</p>
  404. </section>
  405. <section id="how-do-i-check-for-a-keypress-without-blocking">
  406. <h2><a class="toc-backref" href="#id9" role="doc-backlink">How do I check for a keypress without blocking?</a><a class="headerlink" href="#how-do-i-check-for-a-keypress-without-blocking" title="Link to this heading">¶</a></h2>
  407. <p>Use the <a class="reference internal" href="../library/msvcrt.html#module-msvcrt" title="msvcrt: Miscellaneous useful routines from the MS VC++ runtime. (Windows)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">msvcrt</span></code></a> module. This is a standard Windows-specific extension module.
  408. It defines a function <code class="docutils literal notranslate"><span class="pre">kbhit()</span></code> which checks whether a keyboard hit is
  409. present, and <code class="docutils literal notranslate"><span class="pre">getch()</span></code> which gets one character without echoing it.</p>
  410. </section>
  411. <section id="how-do-i-solve-the-missing-api-ms-win-crt-runtime-l1-1-0-dll-error">
  412. <h2><a class="toc-backref" href="#id10" role="doc-backlink">How do I solve the missing api-ms-win-crt-runtime-l1-1-0.dll error?</a><a class="headerlink" href="#how-do-i-solve-the-missing-api-ms-win-crt-runtime-l1-1-0-dll-error" title="Link to this heading">¶</a></h2>
  413. <p>This can occur on Python 3.5 and later when using Windows 8.1 or earlier without all updates having been installed.
  414. First ensure your operating system is supported and is up to date, and if that does not resolve the issue,
  415. visit the <a class="reference external" href="https://support.microsoft.com/en-us/help/3118401/">Microsoft support page</a>
  416. for guidance on manually installing the C Runtime update.</p>
  417. </section>
  418. </section>
  419. <div class="clearer"></div>
  420. </div>
  421. </div>
  422. </div>
  423. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  424. <div class="sphinxsidebarwrapper">
  425. <div>
  426. <h3><a href="../contents.html">Table of Contents</a></h3>
  427. <ul>
  428. <li><a class="reference internal" href="#">Python on Windows FAQ</a><ul>
  429. <li><a class="reference internal" href="#how-do-i-run-a-python-program-under-windows">How do I run a Python program under Windows?</a></li>
  430. <li><a class="reference internal" href="#how-do-i-make-python-scripts-executable">How do I make Python scripts executable?</a></li>
  431. <li><a class="reference internal" href="#why-does-python-sometimes-take-so-long-to-start">Why does Python sometimes take so long to start?</a></li>
  432. <li><a class="reference internal" href="#how-do-i-make-an-executable-from-a-python-script">How do I make an executable from a Python script?</a></li>
  433. <li><a class="reference internal" href="#is-a-pyd-file-the-same-as-a-dll">Is a <code class="docutils literal notranslate"><span class="pre">*.pyd</span></code> file the same as a DLL?</a></li>
  434. <li><a class="reference internal" href="#how-can-i-embed-python-into-a-windows-application">How can I embed Python into a Windows application?</a></li>
  435. <li><a class="reference internal" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source">How do I keep editors from inserting tabs into my Python source?</a></li>
  436. <li><a class="reference internal" href="#how-do-i-check-for-a-keypress-without-blocking">How do I check for a keypress without blocking?</a></li>
  437. <li><a class="reference internal" href="#how-do-i-solve-the-missing-api-ms-win-crt-runtime-l1-1-0-dll-error">How do I solve the missing api-ms-win-crt-runtime-l1-1-0.dll error?</a></li>
  438. </ul>
  439. </li>
  440. </ul>
  441. </div>
  442. <div>
  443. <h4>Previous topic</h4>
  444. <p class="topless"><a href="extending.html"
  445. title="previous chapter">Extending/Embedding FAQ</a></p>
  446. </div>
  447. <div>
  448. <h4>Next topic</h4>
  449. <p class="topless"><a href="gui.html"
  450. title="next chapter">Graphic User Interface FAQ</a></p>
  451. </div>
  452. <div role="note" aria-label="source link">
  453. <h3>This Page</h3>
  454. <ul class="this-page-menu">
  455. <li><a href="../bugs.html">Report a Bug</a></li>
  456. <li>
  457. <a href="https://github.com/python/cpython/blob/main/Doc/faq/windows.rst"
  458. rel="nofollow">Show Source
  459. </a>
  460. </li>
  461. </ul>
  462. </div>
  463. </div>
  464. <div id="sidebarbutton" title="Collapse sidebar">
  465. <span>«</span>
  466. </div>
  467. </div>
  468. <div class="clearer"></div>
  469. </div>
  470. <div class="related" role="navigation" aria-label="related navigation">
  471. <h3>Navigation</h3>
  472. <ul>
  473. <li class="right" style="margin-right: 10px">
  474. <a href="../genindex.html" title="General Index"
  475. >index</a></li>
  476. <li class="right" >
  477. <a href="../py-modindex.html" title="Python Module Index"
  478. >modules</a> |</li>
  479. <li class="right" >
  480. <a href="gui.html" title="Graphic User Interface FAQ"
  481. >next</a> |</li>
  482. <li class="right" >
  483. <a href="extending.html" title="Extending/Embedding FAQ"
  484. >previous</a> |</li>
  485. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  486. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  487. <li class="switchers">
  488. <div class="language_switcher_placeholder"></div>
  489. <div class="version_switcher_placeholder"></div>
  490. </li>
  491. <li>
  492. </li>
  493. <li id="cpython-language-and-version">
  494. <a href="../index.html">3.12.3 Documentation</a> &#187;
  495. </li>
  496. <li class="nav-item nav-item-1"><a href="index.html" >Python Frequently Asked Questions</a> &#187;</li>
  497. <li class="nav-item nav-item-this"><a href="">Python on Windows FAQ</a></li>
  498. <li class="right">
  499. <div class="inline-search" role="search">
  500. <form class="inline-search" action="../search.html" method="get">
  501. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  502. <input type="submit" value="Go" />
  503. </form>
  504. </div>
  505. |
  506. </li>
  507. <li class="right">
  508. <label class="theme-selector-label">
  509. Theme
  510. <select class="theme-selector" oninput="activateTheme(this.value)">
  511. <option value="auto" selected>Auto</option>
  512. <option value="light">Light</option>
  513. <option value="dark">Dark</option>
  514. </select>
  515. </label> |</li>
  516. </ul>
  517. </div>
  518. <div class="footer">
  519. &copy;
  520. <a href="../copyright.html">
  521. Copyright
  522. </a>
  523. 2001-2024, Python Software Foundation.
  524. <br />
  525. This page is licensed under the Python Software Foundation License Version 2.
  526. <br />
  527. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  528. <br />
  529. See <a href="/license.html">History and License</a> for more information.<br />
  530. <br />
  531. The Python Software Foundation is a non-profit corporation.
  532. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  533. <br />
  534. <br />
  535. Last updated on Apr 09, 2024 (13:47 UTC).
  536. <a href="/bugs.html">Found a bug</a>?
  537. <br />
  538. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  539. </div>
  540. </body>
  541. </html>
上海开阖软件有限公司 沪ICP备12045867号-1