Files
hustoj/web/python-docs/faq/windows.html
2025-03-09 17:10:56 +08:00

408 lines
33 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>Python在Windows上的常见问题 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="图形用户界面GUI常见问题" href="gui.html" />
<link rel="prev" title="扩展/嵌入常见问题" href="extending.html" />
<link rel="canonical" href="https://docs.python.org/3/faq/windows.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
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" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">Python在Windows上的常见问题</a><ul>
<li><a class="reference internal" href="#how-do-i-run-a-python-program-under-windows">我怎样在Windows下运行一个Python程序</a></li>
<li><a class="reference internal" href="#how-do-i-make-python-scripts-executable">我怎么让 Python 脚本可执行?</a></li>
<li><a class="reference internal" href="#why-does-python-sometimes-take-so-long-to-start">为什么有时候 Python 程序会启动缓慢?</a></li>
<li><a class="reference internal" href="#how-do-i-make-an-executable-from-a-python-script">我怎样使用 Python 脚本制作可执行文件?</a></li>
<li><a class="reference internal" href="#is-a-pyd-file-the-same-as-a-dll"><code class="docutils literal notranslate"><span class="pre">*.pyd</span></code> 文件和 DLL 文件相同吗?</a></li>
<li><a class="reference internal" href="#how-can-i-embed-python-into-a-windows-application">我怎样将 Python 嵌入一个 Windows 程序?</a></li>
<li><a class="reference internal" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source">如何让编辑器不要在我的 Python 源代码中插入 tab </a></li>
<li><a class="reference internal" href="#how-do-i-check-for-a-keypress-without-blocking">如何在不阻塞的情况下检查按键?</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="extending.html"
title="上一章">扩展/嵌入常见问题</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="gui.html"
title="下一章">图形用户界面GUI常见问题</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/faq/windows.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/faq/windows.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="gui.html" title="图形用户界面GUI常见问题"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="extending.html" title="扩展/嵌入常见问题"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python 常见问题</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="python-on-windows-faq">
<span id="windows-faq"></span><h1><a class="toc-backref" href="#id2">Python在Windows上的常见问题</a><a class="headerlink" href="#python-on-windows-faq" title="永久链接至标题"></a></h1>
<div class="contents topic" id="id1">
<p class="topic-title">目录</p>
<ul class="simple">
<li><p><a class="reference internal" href="#python-on-windows-faq" id="id2">Python在Windows上的常见问题</a></p>
<ul>
<li><p><a class="reference internal" href="#how-do-i-run-a-python-program-under-windows" id="id3">我怎样在Windows下运行一个Python程序</a></p></li>
<li><p><a class="reference internal" href="#how-do-i-make-python-scripts-executable" id="id4">我怎么让 Python 脚本可执行?</a></p></li>
<li><p><a class="reference internal" href="#why-does-python-sometimes-take-so-long-to-start" id="id5">为什么有时候 Python 程序会启动缓慢?</a></p></li>
<li><p><a class="reference internal" href="#how-do-i-make-an-executable-from-a-python-script" id="id6">我怎样使用 Python 脚本制作可执行文件?</a></p></li>
<li><p><a class="reference internal" href="#is-a-pyd-file-the-same-as-a-dll" id="id7"><code class="docutils literal notranslate"><span class="pre">*.pyd</span></code> 文件和 DLL 文件相同吗?</a></p></li>
<li><p><a class="reference internal" href="#how-can-i-embed-python-into-a-windows-application" id="id8">我怎样将 Python 嵌入一个 Windows 程序?</a></p></li>
<li><p><a class="reference internal" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source" id="id9">如何让编辑器不要在我的 Python 源代码中插入 tab </a></p></li>
<li><p><a class="reference internal" href="#how-do-i-check-for-a-keypress-without-blocking" id="id10">如何在不阻塞的情况下检查按键?</a></p></li>
</ul>
</li>
</ul>
</div>
<section id="how-do-i-run-a-python-program-under-windows">
<span id="faq-run-program-under-windows"></span><h2><a class="toc-backref" href="#id3">我怎样在Windows下运行一个Python程序</a><a class="headerlink" href="#how-do-i-run-a-python-program-under-windows" title="永久链接至标题"></a></h2>
<p>这不一定是一个简单的问题。如果你已经熟悉在Windows的命令行中运行程序的方法一切都显而易见不然的话你也许需要额外获得些许指导。</p>
<p>除非你在使用某种集成开发环境否则你将会在被称为“DOS 窗口”或“命令提示符窗口”的地方 <em>输入</em> Windows 命令。 通常你可以在搜索栏搜索 <code class="docutils literal notranslate"><span class="pre">cmd</span></code> 来创建这种窗口。 你应该能够识别你何时打开了这样的窗口,因为你将看到一个 Windows“命令提示符”通常看起来是这样</p>
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">C:\&gt;</span>
</pre></div>
</div>
<p>前面的字母可能会不同,而且后面有可能会有其他东西,所以你也许会看到类似这样的东西:</p>
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">D:\YourName\Projects\Python&gt;</span>
</pre></div>
</div>
<p>出现的内容具体取决与你的电脑如何设置和最近用它做的事。 当你启动了这样一个窗口后就可以开始运行Python程序了。</p>
<p>Python 脚本需要被另外一个叫做 Python <em>解释器</em> 的程序来处理。 解释器读取脚本,把它编译成字节码,然后执行字节码来运行你的程序。 所以怎样安排解释器来处理你的 Python 脚本呢?</p>
<p>首先确保命令窗口能够将“py”识别为指令来开启解释器。 如果你打开过一个命令窗口, 尝试输入命令 <code class="docutils literal notranslate"><span class="pre">py</span></code> 然后按回车:</p>
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">C:\Users\YourName&gt;</span> py
</pre></div>
</div>
<p>然后你应当看见类似类似这样的东西:</p>
<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>
<span class="go">Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.</span>
<span class="gp">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>解释器已经以“交互模式”打开。这意味着你可以交互输入Python语句或表达式并在等待时执行或评估它们。这是Python最强大的功能之一。输入几个表达式并看看结果</p>
<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>
<span class="go">Hello</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s2">&quot;Hello&quot;</span> <span class="o">*</span> <span class="mi">3</span>
<span class="go">&#39;HelloHelloHello&#39;</span>
</pre></div>
</div>
<p>许多人把交互模式当作方便和高度可编程的计算器。 想结束交互式Python会话时调用 <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> 函数,或者按住 <kbd class="kbd docutils literal notranslate">Ctrl</kbd> 键时输入 <kbd class="kbd docutils literal notranslate">Z</kbd> ,之后按 <kbd class="kbd docutils literal notranslate">Enter</kbd> 键返回Windows命令提示符。</p>
<p>你可能发现在开始菜单有这样一个条目 <span class="menuselection">开始 ‣ 所有程序 ‣ Python 3.x ‣ Python (命令行)</span> 运行它后会出现一个有着 <code class="docutils literal notranslate"><span class="pre">&gt;&gt;&gt;</span></code> 提示的新窗口。 在此之后,如果调用 <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> 函数或按 <kbd class="kbd docutils literal notranslate">Ctrl-Z</kbd> 组合键后窗口将会消失。 Windows 会在这个窗口中运行一个“python”命令并且在你终止解释器的时候关闭它。</p>
<p>现在我们知道 <code class="docutils literal notranslate"><span class="pre">py</span></code> 命令已经被识别,可以输入 Python 脚本了。 你需要提供 Python 脚本的绝对路径或相对路径。 假设 Python 脚本位于桌面上并命名为 <code class="docutils literal notranslate"><span class="pre">hello.py</span></code>,并且命令提示符在用户主目录打开,那么可以看到类似于这样的东西:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>C:\Users\YourName&gt;
</pre></div>
</div>
<p>那么现在可以让 <code class="docutils literal notranslate"><span class="pre">py</span></code> 命令执行你的脚本,只需要输入 <code class="docutils literal notranslate"><span class="pre">py</span></code> 和脚本路径:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>C:\Users\YourName&gt; py Desktop\hello.py
hello
</pre></div>
</div>
</section>
<section id="how-do-i-make-python-scripts-executable">
<h2><a class="toc-backref" href="#id4">我怎么让 Python 脚本可执行?</a><a class="headerlink" href="#how-do-i-make-python-scripts-executable" title="永久链接至标题"></a></h2>
<p>在 Windows 上,标准 Python 安装程序已将 .py 扩展名与文件类型 (Python.File) 相关联,并为该文件类型提供运行解释器的打开命令 (<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> <span class="pre">%*</span></code>) 。 这足以使脚本在命令提示符下作为“foo.py”命令被执行。 如果希望通过简单地键入“foo”而无需输入文件扩展名来执行脚本则需要将 .py 添加到 PATHEXT 环境变量中。</p>
</section>
<section id="why-does-python-sometimes-take-so-long-to-start">
<h2><a class="toc-backref" href="#id5">为什么有时候 Python 程序会启动缓慢?</a><a class="headerlink" href="#why-does-python-sometimes-take-so-long-to-start" title="永久链接至标题"></a></h2>
<p>通常Python 在 Windows 上启动得很快,但偶尔会有错误报告说 Python 突然需要很长时间才能启动。更令人费解的是,在其他配置相同的 Windows 系统上Python 却可以工作得很好。</p>
<p>该问题可能是由于计算机上的杀毒软件配置错误造成的。当将病毒扫描配置为监视文件系统中所有读取行为时,一些杀毒扫描程序会导致两个数量级的启动开销。请检查你系统安装的杀毒扫描程序的配置,确保两台机它们是同样的配置。已知的, McAfee 杀毒软件在将它设置为扫描所有文件系统访问时,会产生这个问题。</p>
</section>
<section id="how-do-i-make-an-executable-from-a-python-script">
<h2><a class="toc-backref" href="#id6">我怎样使用 Python 脚本制作可执行文件?</a><a class="headerlink" href="#how-do-i-make-an-executable-from-a-python-script" title="永久链接至标题"></a></h2>
<p>请参看 <a class="reference external" href="https://cx-freeze.readthedocs.io/en/latest/">cx_Freeze</a><a class="reference external" href="http://www.py2exe.org/">py2exe</a>,两者都是允许你基于 Python 代码创建控制台和 GUI 可执行程序的 distutils 扩展。</p>
</section>
<section id="is-a-pyd-file-the-same-as-a-dll">
<h2><a class="toc-backref" href="#id7"><code class="docutils literal notranslate"><span class="pre">*.pyd</span></code> 文件和 DLL 文件相同吗?</a><a class="headerlink" href="#is-a-pyd-file-the-same-as-a-dll" title="永久链接至标题"></a></h2>
<p>是的, .pyd 文件也是 dll ,但有一些差异。如果你有一个名为 <code class="docutils literal notranslate"><span class="pre">foo.pyd</span></code> 的DLL那么它必须有一个函数 <code class="docutils literal notranslate"><span class="pre">PyInit_foo()</span></code> 。 然后你可以编写 Python 代码 “import foo” Python 将搜索 foo.pyd (以及 foo.py 、 foo.pyc )。如果找到它,将尝试调用 <code class="docutils literal notranslate"><span class="pre">PyInit_foo()</span></code> 来初始化它。你不应将 .exe 与 foo.lib 链接,因为这会导致 Windows 要求存在 DLL 。</p>
<p>请注意, foo.pyd 的搜索路径是 PYTHONPATH ,与 Windows 用于搜索 foo.dll 的路径不同。此外, foo.pyd 不需要存在来运行你的程序,而如果你将程序与 dll 链接,则需要 dll 。 当然,如果你想 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">foo</span></code> ,则需要 foo.pyd 。在 DLL 中,链接在源代码中用 <code class="docutils literal notranslate"><span class="pre">__declspec(dllexport)</span></code> 声明。 在 .pyd 中,链接在可用函数列表中定义。</p>
</section>
<section id="how-can-i-embed-python-into-a-windows-application">
<h2><a class="toc-backref" href="#id8">我怎样将 Python 嵌入一个 Windows 程序?</a><a class="headerlink" href="#how-can-i-embed-python-into-a-windows-application" title="永久链接至标题"></a></h2>
<p>在 Windows 应用程序中嵌入 Python 解释器可以总结如下:</p>
<ol class="arabic">
<li><p>请 _不要_ 直接在你的 .exe 文件中内置 Python 。在 Windows 上, Python 必须是一个 DLL ,这样才可以处理导入的本身就是 DLL 的模块。(这是第一个未记录的关键事实。)相反,链接到 <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code> ;它通常安装在 <code class="docutils literal notranslate"><span class="pre">C:\Windows\System</span></code> 中。 <em>NN</em> 是 Python 版本如数字“33”代表 Python 3.3 。</p>
<p>你可以通过两种不同的方式链接到 Python 。加载时链接意味着链接到 <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.lib</span></code> ,而运行时链接意味着链接 <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code> 。(一般说明: <code class="file docutils literal notranslate"><span class="pre">python</span> <em><span class="pre">NN</span></em><span class="pre">.lib</span></code> 是所谓的“import lib”对应于 <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code> 。它只定义了链接器的符号。)</p>
<p>运行时链接极大地简化了链接选项,一切都在运行时发生。你的代码必须使用 Windows 的 <code class="docutils literal notranslate"><span class="pre">LoadLibraryEx()</span></code> 程序加载 <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code> 。代码还必须使用使用 Windows 的 <code class="docutils literal notranslate"><span class="pre">GetProcAddress()</span></code> 例程获得的指针访问 <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code> 中程序和数据(即 Python 的 C API )。宏可以使这些指针对任何调用 Python C API 中的例程的 C 代码都是透明的。</p>
<p>Borland 提示:首先使用 Coff2Omf.exe 将 <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.lib</span></code> 转换为 OMF 格式。</p>
</li>
<li><p>如果你使用 SWIG ,很容易创建一个 Python “扩展模块”,它将使应用程序的数据和方法可供 Python 使用。SWIG将为你处理所有蹩脚的细节。结果是你将链接到 .exe 文件 <em></em> 的C代码 (!) 你不必创建 DLL 文件,这也简化了链接。</p></li>
<li><p>SWIG 将创建一个 init 函数(一个 C 函数),其名称取决于扩展模块的名称。例如,如果模块的名称是 leo ,则 init 函数将被称为 initleo() 。 如果您使用 SWIG 阴影类,则 init 函数将被称为 initleoc() 。这初始化了一个由阴影类使用的隐藏辅助类。</p>
<p>你可以将步骤 2 中的 C 代码链接到 .exe 文件的原因是调用初始化函数等同于将模块导入 Python (这是第二个关键的未记载事实。)</p>
</li>
<li><p>简而言之,你可以用以下代码使用扩展模块初始化 Python 解释器。</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;python.h&quot;</span>
<span class="p">...</span>
<span class="n">Py_Initialize</span><span class="p">();</span><span class="w"> </span><span class="c1">// Initialize Python.</span>
<span class="n">initmyAppc</span><span class="p">();</span><span class="w"> </span><span class="c1">// Initialize (import) the helper class.</span>
<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>
</pre></div>
</div>
</li>
<li><p>Python C API 存在两个问题,如果你使用除 MSVC 之外的编译器用于构建 python.dll ,这将会变得明显。</p>
<p>问题1采用 FILE* 参数的所谓“极高级”函数在多编译器环境中不起作用因为每个编译器的FILE结构体概念都不同。从实现的角度来看这些是非常 _低_ 级的功能。</p>
<p>问题2在为 void 函数生成包装器时SWIG 会生成以下代码:</p>
<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>
<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>
<span class="k">return</span><span class="w"> </span><span class="n">_resultobj</span><span class="p">;</span>
</pre></div>
</div>
<p>Py_None 是一个宏,它扩展为对 pythonNN.dll 中名为 _Py_NoneStruct 的复杂数据结构的引用。同样,此代码将在多编译器环境中失败。将此类代码替换为:</p>
<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>
</pre></div>
</div>
<p>有可能使用 SWIG 的 <code class="docutils literal notranslate"><span class="pre">%typemap</span></code> 命令自动进行更改但我无法使其工作我是一个完全的SWIG新手</p>
</li>
<li><p>使用 Python shell 脚本从 Windows 应用程序内部建立 Python 解释器窗口并不是一个好主意;生成的窗口将独立于应用程序的窗口系统。相反,你(或 wxPythonWindow 类应该创建一个“本机”解释器窗口。将该窗口连接到Python解释器很容易。你可以将 Python的 i/o 重定向到支持读写的 _任意_ 对象,因此你只需要一个包含 read() 和 write() 方法的 Python 对象(在扩展模块中定义)。</p></li>
</ol>
</section>
<section id="how-do-i-keep-editors-from-inserting-tabs-into-my-python-source">
<h2><a class="toc-backref" href="#id9">如何让编辑器不要在我的 Python 源代码中插入 tab </a><a class="headerlink" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source" title="永久链接至标题"></a></h2>
<p>本 FAQ 不建议使用制表符, Python 样式指南 <span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0008"><strong>PEP 8</strong></a> ,为发行的 Python 代码推荐 4 个空格;这也是 Emacs python-mode 默认值。</p>
<p>在任何编辑器下,混合制表符和空格都是一个坏主意。 MSVC 在这方面没有什么不同,并且很容易配置为使用空格: 点击 <span class="menuselection">Tools ‣ Options ‣ Tabs</span>对于文件类型“Default”设置“Tab size”和“Indent size”为 4 ,并选择“插入空格”单选按钮。</p>
<p>如果混合制表符和空格导致前导空格出现问题, Python 会引发 <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><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> 。你还可以运行 <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> 模块以批处理模式检查目录树。</p>
</section>
<section id="how-do-i-check-for-a-keypress-without-blocking">
<h2><a class="toc-backref" href="#id10">如何在不阻塞的情况下检查按键?</a><a class="headerlink" href="#how-do-i-check-for-a-keypress-without-blocking" title="永久链接至标题"></a></h2>
<p>使用 <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> 模块。 这是一个标准的 Windows 专属扩展模块。 它定义了一个函数 <code class="docutils literal notranslate"><span class="pre">kbhit()</span></code> 用于检查是否有键盘中的某个键被按下,以及 <code class="docutils literal notranslate"><span class="pre">getch()</span></code> 用于获取一个字符而不将其回显。</p>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">Python在Windows上的常见问题</a><ul>
<li><a class="reference internal" href="#how-do-i-run-a-python-program-under-windows">我怎样在Windows下运行一个Python程序</a></li>
<li><a class="reference internal" href="#how-do-i-make-python-scripts-executable">我怎么让 Python 脚本可执行?</a></li>
<li><a class="reference internal" href="#why-does-python-sometimes-take-so-long-to-start">为什么有时候 Python 程序会启动缓慢?</a></li>
<li><a class="reference internal" href="#how-do-i-make-an-executable-from-a-python-script">我怎样使用 Python 脚本制作可执行文件?</a></li>
<li><a class="reference internal" href="#is-a-pyd-file-the-same-as-a-dll"><code class="docutils literal notranslate"><span class="pre">*.pyd</span></code> 文件和 DLL 文件相同吗?</a></li>
<li><a class="reference internal" href="#how-can-i-embed-python-into-a-windows-application">我怎样将 Python 嵌入一个 Windows 程序?</a></li>
<li><a class="reference internal" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source">如何让编辑器不要在我的 Python 源代码中插入 tab </a></li>
<li><a class="reference internal" href="#how-do-i-check-for-a-keypress-without-blocking">如何在不阻塞的情况下检查按键?</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="extending.html"
title="上一章">扩展/嵌入常见问题</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="gui.html"
title="下一章">图形用户界面GUI常见问题</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/faq/windows.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="gui.html" title="图形用户界面GUI常见问题"
>下一页</a> |</li>
<li class="right" >
<a href="extending.html" title="扩展/嵌入常见问题"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 常见问题</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>