-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows.html
More file actions
1134 lines (1113 loc) · 80.8 KB
/
windows.html
File metadata and controls
1134 lines (1113 loc) · 80.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_TW">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>3. 在 Windows 上使用 Python — Python 3.7.0 說明文件</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/translations.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.7.0 說明文件 中搜尋"
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="Copyright" href="../copyright.html" />
<link rel="next" title="4. 在麥金塔系統使用Python" href="mac.html" />
<link rel="prev" title="2. Using Python on Unix platforms" href="unix.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.org/3/using/windows.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/switchers.js"></script>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="mac.html" title="4. 在麥金塔系統使用Python"
accesskey="N">下一頁</a> |</li>
<li class="right" >
<a href="unix.html" title="2. Using Python on Unix platforms"
accesskey="P">上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python Setup and Usage</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="using-python-on-windows">
<span id="using-on-windows"></span><h1>3. 在 Windows 上使用 Python<a class="headerlink" href="#using-python-on-windows" title="本標題的永久連結">¶</a></h1>
<p>This document aims to give an overview of Windows-specific behaviour you should
know about when using Python on Microsoft Windows.</p>
<div class="section" id="installing-python">
<h2>3.1. 安裝 Python<a class="headerlink" href="#installing-python" title="本標題的永久連結">¶</a></h2>
<p>Unlike most Unix systems and services, Windows does not include a system
supported installation of Python. To make Python available, the CPython team
has compiled Windows installers (MSI packages) with every <a class="reference external" href="https://www.python.org/download/releases/">release</a> for many years. These installers
are primarily intended to add a per-user installation of Python, with the
core interpreter and library being used by a single user. The installer is also
able to install for all users of a single machine, and a separate ZIP file is
available for application-local distributions.</p>
<div class="section" id="supported-versions">
<h3>3.1.1. Supported Versions<a class="headerlink" href="#supported-versions" title="本標題的永久連結">¶</a></h3>
<p>As specified in <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0011"><strong>PEP 11</strong></a>, a Python release only supports a Windows platform
while Microsoft considers the platform under extended support. This means that
Python 3.7 supports Windows Vista and newer. If you require Windows XP
support then please install Python 3.4.</p>
</div>
<div class="section" id="installation-steps">
<h3>3.1.2. 安裝步驟<a class="headerlink" href="#installation-steps" title="本標題的永久連結">¶</a></h3>
<p>Four Python 3.7 installers are available for download - two each for the
32-bit and 64-bit versions of the interpreter. The <em>web installer</em> is a small
initial download, and it will automatically download the required components as
necessary. The <em>offline installer</em> includes the components necessary for a
default installation and only requires an internet connection for optional
features. See <a class="reference internal" href="#install-layout-option"><span class="std std-ref">當安裝時不下載</span></a> for other ways to avoid downloading
during installation.</p>
<p>After starting the installer, one of two options may be selected:</p>
<img alt="../_images/win_installer.png" src="../_images/win_installer.png" />
<p>如果你選擇「馬上安裝」:</p>
<ul class="simple">
<li>You will <em>not</em> need to be an administrator (unless a system update for the
C Runtime Library is required or you install the <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a> for all
users)</li>
<li>Python will be installed into your user directory</li>
<li>The <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a> will be installed according to the option at the bottom
of the first page</li>
<li>The standard library, test suite, launcher and pip will be installed</li>
<li>如果選擇,安裝目錄將被加入到你的 <span class="target" id="index-25"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code></li>
<li>安裝捷徑將只能被目前使用者所看見</li>
</ul>
<p>選擇「客製化安裝」將允許你選擇所需的項目進行安裝,安裝位置與其他選擇或安裝後的所需進行的動作。你將需要使用此選項「除錯特徵」或「二進位方式」進行安裝。</p>
<p>To perform an all-users installation, you should select 「Customize
installation」. In this case:</p>
<ul class="simple">
<li>You may be required to provide administrative credentials or approval</li>
<li>Python will be installed into the Program Files directory</li>
<li>The <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a> will be installed into the Windows directory</li>
<li>Optional features may be selected during installation</li>
<li>The standard library can be pre-compiled to bytecode</li>
<li>If selected, the install directory will be added to the system <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code></li>
<li>捷徑將被所有使用者所見</li>
</ul>
</div>
<div class="section" id="removing-the-max-path-limitation">
<span id="max-path"></span><h3>3.1.3. Removing the MAX_PATH Limitation<a class="headerlink" href="#removing-the-max-path-limitation" title="本標題的永久連結">¶</a></h3>
<p>Windows historically has limited path lengths to 260 characters. This meant that
paths longer than this would not resolve and errors would result.</p>
<p>In the latest versions of Windows, this limitation can be expanded to
approximately 32,000 characters. Your administrator will need to activate the
「Enable Win32 long paths」 group policy, or set the registry value
<code class="docutils literal notranslate"><span class="pre">HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem@LongPathsEnabled</span></code>
to <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
<p>This allows the <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function, the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module and most other
path functionality to accept and return paths longer than 260 characters when
using strings. (Use of bytes as paths is deprecated on Windows, and this feature
is not available when using bytes.)</p>
<p>After changing the above option, no further configuration is required.</p>
<div class="versionchanged">
<p><span class="versionmodified">3.6 版更變: </span>Support for long paths was enabled in Python.</p>
</div>
</div>
<div class="section" id="installing-without-ui">
<span id="install-quiet-option"></span><h3>3.1.4. 安裝排除使用者介面<a class="headerlink" href="#installing-without-ui" title="本標題的永久連結">¶</a></h3>
<p>All of the options available in the installer UI can also be specified from the
command line, allowing scripted installers to replicate an installation on many
machines without user interaction. These options may also be set without
suppressing the UI in order to change some of the defaults.</p>
<p>To completely hide the installer UI and install Python silently, pass the
<code class="docutils literal notranslate"><span class="pre">/quiet</span></code> option. To skip past the user interaction but still display
progress and errors, pass the <code class="docutils literal notranslate"><span class="pre">/passive</span></code> option. The <code class="docutils literal notranslate"><span class="pre">/uninstall</span></code>
option may be passed to immediately begin removing Python - no prompt will be
displayed.</p>
<p>All other options are passed as <code class="docutils literal notranslate"><span class="pre">name=value</span></code>, where the value is usually
<code class="docutils literal notranslate"><span class="pre">0</span></code> to disable a feature, <code class="docutils literal notranslate"><span class="pre">1</span></code> to enable a feature, or a path. The full list
of available options is shown below.</p>
<table border="1" class="docutils">
<colgroup>
<col width="30%" />
<col width="42%" />
<col width="29%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Name</th>
<th class="head">描述</th>
<th class="head">預設</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>InstallAllUsers</td>
<td>Perform a system-wide installation.</td>
<td>0</td>
</tr>
<tr class="row-odd"><td>TargetDir</td>
<td>安裝目錄</td>
<td>Selected based on
InstallAllUsers</td>
</tr>
<tr class="row-even"><td>DefaultAllUsersTargetDir</td>
<td>The default installation directory
for all-user installs</td>
<td><code class="file docutils literal notranslate"><span class="pre">%ProgramFiles%\Python</span> <span class="pre">X.Y</span></code> or <code class="file docutils literal notranslate"><span class="pre">%ProgramFiles(x86)%\Python</span> <span class="pre">X.Y</span></code></td>
</tr>
<tr class="row-odd"><td>DefaultJustForMeTargetDir</td>
<td>預設安裝目錄給 只有給我 安裝方式</td>
<td><code class="file docutils literal notranslate"><span class="pre">%LocalAppData%\Programs\PythonXY</span></code> or
<code class="file docutils literal notranslate"><span class="pre">%LocalAppData%\Programs\PythonXY-32</span></code></td>
</tr>
<tr class="row-even"><td>DefaultCustomTargetDir</td>
<td>The default custom install directory
displayed in the UI</td>
<td>(empty)</td>
</tr>
<tr class="row-odd"><td>AssociateFiles</td>
<td>當執行程序也被安裝時創造檔案關聯</td>
<td>1</td>
</tr>
<tr class="row-even"><td>CompileAll</td>
<td>編譯所有 <code class="docutils literal notranslate"><span class="pre">.py</span></code> 檔案為 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code>。</td>
<td>0</td>
</tr>
<tr class="row-odd"><td>PrependPath</td>
<td>Add install and Scripts directories
to <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> and <code class="docutils literal notranslate"><span class="pre">.PY</span></code> to
<span class="target" id="index-4"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATHEXT</span></code></td>
<td>0</td>
</tr>
<tr class="row-even"><td>Shortcuts</td>
<td>Create shortcuts for the interpreter,
documentation and IDLE if installed.</td>
<td>1</td>
</tr>
<tr class="row-odd"><td>Include_doc</td>
<td>安裝Python文件</td>
<td>1</td>
</tr>
<tr class="row-even"><td>Include_debug</td>
<td>Install debug binaries</td>
<td>0</td>
</tr>
<tr class="row-odd"><td>Include_dev</td>
<td>Install developer headers and
libraries</td>
<td>1</td>
</tr>
<tr class="row-even"><td>Include_exe</td>
<td>Install <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code> and
related files</td>
<td>1</td>
</tr>
<tr class="row-odd"><td>Include_launcher</td>
<td>Install <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a>.</td>
<td>1</td>
</tr>
<tr class="row-even"><td>InstallLauncherAllUsers</td>
<td>Installs <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a> for all
users.</td>
<td>1</td>
</tr>
<tr class="row-odd"><td>Include_lib</td>
<td>Install standard library and
extension modules</td>
<td>1</td>
</tr>
<tr class="row-even"><td>Include_pip</td>
<td>Install bundled pip and setuptools</td>
<td>1</td>
</tr>
<tr class="row-odd"><td>Include_symbols</td>
<td>Install debugging symbols (<cite>*</cite>.pdb)</td>
<td>0</td>
</tr>
<tr class="row-even"><td>Include_tcltk</td>
<td>Install Tcl/Tk support and IDLE</td>
<td>1</td>
</tr>
<tr class="row-odd"><td>Include_test</td>
<td>Install standard library test suite</td>
<td>1</td>
</tr>
<tr class="row-even"><td>Include_tools</td>
<td>Install utility scripts</td>
<td>1</td>
</tr>
<tr class="row-odd"><td>LauncherOnly</td>
<td>Only installs the launcher. This
will override most other options.</td>
<td>0</td>
</tr>
<tr class="row-even"><td>SimpleInstall</td>
<td>Disable most install UI</td>
<td>0</td>
</tr>
<tr class="row-odd"><td>SimpleInstallDescription</td>
<td>A custom message to display when the
simplified install UI is used.</td>
<td>(empty)</td>
</tr>
</tbody>
</table>
<p>For example, to silently install a default, system-wide Python installation,
you could use the following command (from an elevated command prompt):</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>python-3.6.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
</pre></div>
</div>
<p>To allow users to easily install a personal copy of Python without the test
suite, you could provide a shortcut with the following command. This will
display a simplified initial page and disallow customization:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>python-3.6.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0
SimpleInstall=1 SimpleInstallDescription="Just for me, no test suite."
</pre></div>
</div>
<p>(Note that omitting the launcher also omits file associations, and is only
recommended for per-user installs when there is also a system-wide installation
that included the launcher.)</p>
<p>The options listed above can also be provided in a file named <code class="docutils literal notranslate"><span class="pre">unattend.xml</span></code>
alongside the executable. This file specifies a list of options and values.
When a value is provided as an attribute, it will be converted to a number if
possible. Values provided as element text are always left as strings. This
example file sets the same options as the previous example:</p>
<div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="nt"><Options></span>
<span class="nt"><Option</span> <span class="na">Name=</span><span class="s">"InstallAllUsers"</span> <span class="na">Value=</span><span class="s">"no"</span> <span class="nt">/></span>
<span class="nt"><Option</span> <span class="na">Name=</span><span class="s">"Include_launcher"</span> <span class="na">Value=</span><span class="s">"0"</span> <span class="nt">/></span>
<span class="nt"><Option</span> <span class="na">Name=</span><span class="s">"Include_test"</span> <span class="na">Value=</span><span class="s">"no"</span> <span class="nt">/></span>
<span class="nt"><Option</span> <span class="na">Name=</span><span class="s">"SimpleInstall"</span> <span class="na">Value=</span><span class="s">"yes"</span> <span class="nt">/></span>
<span class="nt"><Option</span> <span class="na">Name=</span><span class="s">"SimpleInstallDescription"</span><span class="nt">></span>Just for me, no test suite<span class="nt"></Option></span>
<span class="nt"></Options></span>
</pre></div>
</div>
</div>
<div class="section" id="installing-without-downloading">
<span id="install-layout-option"></span><h3>3.1.5. 當安裝時不下載<a class="headerlink" href="#installing-without-downloading" title="本標題的永久連結">¶</a></h3>
<p>As some features of Python are not included in the initial installer download,
selecting those features may require an internet connection. To avoid this
need, all possible components may be downloaded on-demand to create a complete
<em>layout</em> that will no longer require an internet connection regardless of the
selected features. Note that this download may be bigger than required, but
where a large number of installations are going to be performed it is very
useful to have a locally cached copy.</p>
<p>Execute the following command from Command Prompt to download all possible
required files. Remember to substitute <code class="docutils literal notranslate"><span class="pre">python-3.6.0.exe</span></code> for the actual
name of your installer, and to create layouts in their own directories to
avoid collisions between files with the same name.</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>python-3.6.0.exe /layout [optional target directory]
</pre></div>
</div>
<p>You may also specify the <code class="docutils literal notranslate"><span class="pre">/quiet</span></code> option to hide the progress display.</p>
</div>
<div class="section" id="modifying-an-install">
<h3>3.1.6. Modifying an install<a class="headerlink" href="#modifying-an-install" title="本標題的永久連結">¶</a></h3>
<p>Once Python has been installed, you can add or remove features through the
Programs and Features tool that is part of Windows. Select the Python entry and
choose 「Uninstall/Change」 to open the installer in maintenance mode.</p>
<p>「Modify」 allows you to add or remove features by modifying the checkboxes -
unchanged checkboxes will not install or remove anything. Some options cannot be
changed in this mode, such as the install directory; to modify these, you will
need to remove and then reinstall Python completely.</p>
<p>「Repair」 will verify all the files that should be installed using the current
settings and replace any that have been removed or modified.</p>
<p>「Uninstall」 will remove Python entirely, with the exception of the
<a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a>, which has its own entry in Programs and Features.</p>
</div>
<div class="section" id="other-platforms">
<h3>3.1.7. 其他平台<a class="headerlink" href="#other-platforms" title="本標題的永久連結">¶</a></h3>
<p>With ongoing development of Python, some platforms that used to be supported
earlier are no longer supported (due to the lack of users or developers).
Check <span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0011"><strong>PEP 11</strong></a> for details on all unsupported platforms.</p>
<ul class="simple">
<li><a class="reference external" href="http://pythonce.sourceforge.net/">Windows CE</a> is still supported.</li>
<li>The <a class="reference external" href="https://cygwin.com/">Cygwin</a> installer offers to install the Python
interpreter as well (cf. <a class="reference external" href="ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/mirrors/cygnus/release/python">Cygwin package source</a>, <a class="reference external" href="http://www.tishler.net/jason/software/python/">Maintainer releases</a>)</li>
</ul>
<p>See <a class="reference external" href="https://www.python.org/downloads/windows/">Python for Windows</a>
for detailed information about platforms with pre-compiled installers.</p>
<div class="admonition seealso">
<p class="first admonition-title">也參考</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://dooling.com/index.php/2006/03/14/python-on-xp-7-minutes-to-hello-world/">Python on XP</a></dt>
<dd>「7 Minutes to 「Hello World!」」
by Richard Dooling, 2006</dd>
<dt><a class="reference external" href="http://www.diveintopython.net/installing_python/windows.html">Installing on Windows</a></dt>
<dd>in 「<a class="reference external" href="http://www.diveintopython.net/">Dive into Python: Python from novice to pro</a>」
by Mark Pilgrim, 2004,
ISBN 1-59059-356-1</dd>
<dt><a class="reference external" href="https://python.swaroopch.com/installation.html#installation-on-windows">For Windows users</a></dt>
<dd>in 「Installing Python」
in 「<a class="reference external" href="https://python.swaroopch.com/">A Byte of Python</a>」
by Swaroop C H, 2003</dd>
</dl>
</div>
</div>
</div>
<div class="section" id="alternative-bundles">
<h2>3.2. Alternative bundles<a class="headerlink" href="#alternative-bundles" title="本標題的永久連結">¶</a></h2>
<p>Besides the standard CPython distribution, there are modified packages including
additional functionality. The following is a list of popular versions and their
key features:</p>
<dl class="docutils">
<dt><a class="reference external" href="https://www.activestate.com/activepython/">ActivePython</a></dt>
<dd>Installer with multi-platform compatibility, documentation, PyWin32</dd>
<dt><a class="reference external" href="https://www.anaconda.com/download/">Anaconda</a></dt>
<dd>Popular scientific modules (such as numpy, scipy and pandas) and the
<code class="docutils literal notranslate"><span class="pre">conda</span></code> package manager.</dd>
<dt><a class="reference external" href="https://www.enthought.com/product/canopy/">Canopy</a></dt>
<dd>A 「comprehensive Python analysis environment」 with editors and other
development tools.</dd>
<dt><a class="reference external" href="https://winpython.github.io/">WinPython</a></dt>
<dd>Windows-specific distribution with prebuilt scientific packages and
tools for building packages.</dd>
</dl>
<p>Note that these packages may not include the latest versions of Python or
other libraries, and are not maintained or supported by the core Python team.</p>
</div>
<div class="section" id="configuring-python">
<h2>3.3. 設定 Python<a class="headerlink" href="#configuring-python" title="本標題的永久連結">¶</a></h2>
<p>To run Python conveniently from a command prompt, you might consider changing
some default environment variables in Windows. While the installer provides an
option to configure the PATH and PATHEXT variables for you, this is only
reliable for a single, system-wide installation. If you regularly use multiple
versions of Python, consider using the <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a>.</p>
<div class="section" id="excursus-setting-environment-variables">
<span id="setting-envvars"></span><h3>3.3.1. Excursus: Setting environment variables<a class="headerlink" href="#excursus-setting-environment-variables" title="本標題的永久連結">¶</a></h3>
<p>Windows allows environment variables to be configured permanently at both the
User level and the System level, or temporarily in a command prompt.</p>
<p>To temporarily set environment variables, open Command Prompt and use the
<strong class="command">set</strong> command:</p>
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">C:\></span><span class="k">set</span> <span class="nv">PATH</span><span class="p">=</span>C:\Program Files\Python 3.6;<span class="nv">%PATH%</span>
<span class="gp">C:\></span><span class="k">set</span> <span class="nv">PYTHONPATH</span><span class="p">=</span><span class="nv">%PYTHONPATH%</span>;C:\My_python_lib
<span class="gp">C:\></span>python
</pre></div>
</div>
<p>These changes will apply to any further commands executed in that console, and
will be inherited by any applications started from the console.</p>
<p>Including the variable name within percent signs will expand to the existing
value, allowing you to add your new value at either the start or the end.
Modifying <span class="target" id="index-6"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> by adding the directory containing
<strong class="program">python.exe</strong> to the start is a common way to ensure the correct version
of Python is launched.</p>
<p>To permanently modify the default environment variables, click Start and search
for 『edit environment variables』, or open System properties, <span class="guilabel">Advanced
system settings</span> and click the <span class="guilabel">Environment Variables</span> button.
In this dialog, you can add or modify User and System variables. To change
System variables, you need non-restricted access to your machine
(i.e. Administrator rights).</p>
<div class="admonition note">
<p class="first admonition-title">備註</p>
<p>Windows will concatenate User variables <em>after</em> System variables, which may
cause unexpected results when modifying <span class="target" id="index-7"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>.</p>
<p class="last">The <span class="target" id="index-8"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> variable is used by all versions of Python 2 and
Python 3, so you should not permanently configure this variable unless it
only includes code that is compatible with all of your installed Python
versions.</p>
</div>
<div class="admonition seealso">
<p class="first admonition-title">也參考</p>
<dl class="last docutils">
<dt><a class="reference external" href="https://www.microsoft.com/en-us/wdsi/help/folder-variables">https://www.microsoft.com/en-us/wdsi/help/folder-variables</a></dt>
<dd>環境變數於 Windows NT</dd>
<dt><a class="reference external" href="https://technet.microsoft.com/en-us/library/cc754250.aspx">https://technet.microsoft.com/en-us/library/cc754250.aspx</a></dt>
<dd>The SET command, for temporarily modifying environment variables</dd>
<dt><a class="reference external" href="https://technet.microsoft.com/en-us/library/cc755104.aspx">https://technet.microsoft.com/en-us/library/cc755104.aspx</a></dt>
<dd>The SETX command, for permanently modifying environment variables</dd>
<dt><a class="reference external" href="https://support.microsoft.com/en-us/help/310519/how-to-manage-environment-variables-in-windows-xp">https://support.microsoft.com/en-us/help/310519/how-to-manage-environment-variables-in-windows-xp</a></dt>
<dd>如何管理環境變數於 Windows XP</dd>
<dt><a class="reference external" href="https://www.chem.gla.ac.uk/~louis/software/faq/q1.html">https://www.chem.gla.ac.uk/~louis/software/faq/q1.html</a></dt>
<dd>設定環境變數-Louis J. Farrugia</dd>
</dl>
</div>
</div>
<div class="section" id="finding-the-python-executable">
<span id="windows-path-mod"></span><h3>3.3.2. Finding the Python executable<a class="headerlink" href="#finding-the-python-executable" title="本標題的永久連結">¶</a></h3>
<div class="versionchanged">
<p><span class="versionmodified">3.5 版更變.</span></p>
</div>
<p>Besides using the automatically created start menu entry for the Python
interpreter, you might want to start Python in the command prompt. The
installer has an option to set that up for you.</p>
<p>On the first page of the installer, an option labelled 「Add Python to PATH」
may be selected to have the installer add the install location into the
<span class="target" id="index-9"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>. The location of the <code class="file docutils literal notranslate"><span class="pre">Scripts\</span></code> folder is also added.
This allows you to type <strong class="command">python</strong> to run the interpreter, and
<strong class="command">pip</strong> for the package installer. Thus, you can also execute your
scripts with command line options, see <a class="reference internal" href="cmdline.html#using-on-cmdline"><span class="std std-ref">Command line</span></a> documentation.</p>
<p>If you don’t enable this option at install time, you can always re-run the
installer, select Modify, and enable it. Alternatively, you can manually
modify the <span class="target" id="index-10"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> using the directions in <a class="reference internal" href="#setting-envvars"><span class="std std-ref">Excursus: Setting environment variables</span></a>. You
need to set your <span class="target" id="index-11"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> environment variable to include the directory
of your Python installation, delimited by a semicolon from other entries. An
example variable could look like this (assuming the first two entries already
existed):</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.6
</pre></div>
</div>
</div>
</div>
<div class="section" id="python-launcher-for-windows">
<span id="launcher"></span><h2>3.4. Python Launcher for Windows<a class="headerlink" href="#python-launcher-for-windows" title="本標題的永久連結">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">3.3 版新加入.</span></p>
</div>
<p>The Python launcher for Windows is a utility which aids in locating and
executing of different Python versions. It allows scripts (or the
command-line) to indicate a preference for a specific Python version, and
will locate and execute that version.</p>
<p>Unlike the <span class="target" id="index-12"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> variable, the launcher will correctly select the most
appropriate version of Python. It will prefer per-user installations over
system-wide ones, and orders by language version rather than using the most
recently installed version.</p>
<div class="section" id="getting-started">
<h3>3.4.1. 開始<a class="headerlink" href="#getting-started" title="本標題的永久連結">¶</a></h3>
<div class="section" id="from-the-command-line">
<h4>3.4.1.1. From the command-line<a class="headerlink" href="#from-the-command-line" title="本標題的永久連結">¶</a></h4>
<div class="versionchanged">
<p><span class="versionmodified">3.6 版更變.</span></p>
</div>
<p>System-wide installations of Python 3.3 and later will put the launcher on your
<span class="target" id="index-13"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>. The launcher is compatible with all available versions of
Python, so it does not matter which version is installed. To check that the
launcher is available, execute the following command in Command Prompt:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py
</pre></div>
</div>
<p>You should find that the latest version of Python you have installed is
started - it can be exited as normal, and any additional command-line
arguments specified will be sent directly to Python.</p>
<p>If you have multiple versions of Python installed (e.g., 2.7 and 3.7) you
will have noticed that Python 3.7 was started - to launch Python 2.7, try
the command:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py -2.7
</pre></div>
</div>
<p>If you want the latest version of Python 2.x you have installed, try the
command:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py -2
</pre></div>
</div>
<p>You should find the latest version of Python 2.x starts.</p>
<p>If you see the following error, you do not have the launcher installed:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>'py' is not recognized as an internal or external command,
operable program or batch file.
</pre></div>
</div>
<p>Per-user installations of Python do not add the launcher to <span class="target" id="index-14"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>
unless the option was selected on installation.</p>
</div>
<div class="section" id="virtual-environments">
<h4>3.4.1.2. 虛擬環境(Virtual environment)<a class="headerlink" href="#virtual-environments" title="本標題的永久連結">¶</a></h4>
<div class="versionadded">
<p><span class="versionmodified">3.5 版新加入.</span></p>
</div>
<p>If the launcher is run with no explicit Python version specification, and a
virtual environment (created with the standard library <a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> module or
the external <code class="docutils literal notranslate"><span class="pre">virtualenv</span></code> tool) active, the launcher will run the virtual
environment’s interpreter rather than the global one. To run the global
interpreter, either deactivate the virtual environment, or explicitly specify
the global Python version.</p>
</div>
<div class="section" id="from-a-script">
<h4>3.4.1.3. From a script<a class="headerlink" href="#from-a-script" title="本標題的永久連結">¶</a></h4>
<p>Let’s create a test Python script - create a file called <code class="docutils literal notranslate"><span class="pre">hello.py</span></code> with the
following contents</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="ch">#! python</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"hello from Python </span><span class="si">%s</span><span class="se">\n</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">version</span><span class="p">,))</span>
</pre></div>
</div>
<p>From the directory in which hello.py lives, execute the command:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py hello.py
</pre></div>
</div>
<p>You should notice the version number of your latest Python 2.x installation
is printed. Now try changing the first line to be:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="ch">#! python3</span>
</pre></div>
</div>
<p>Re-executing the command should now print the latest Python 3.x information.
As with the above command-line examples, you can specify a more explicit
version qualifier. Assuming you have Python 2.6 installed, try changing the
first line to <code class="docutils literal notranslate"><span class="pre">#!</span> <span class="pre">python2.6</span></code> and you should find the 2.6 version
information printed.</p>
<p>Note that unlike interactive use, a bare 「python」 will use the latest
version of Python 2.x that you have installed. This is for backward
compatibility and for compatibility with Unix, where the command <code class="docutils literal notranslate"><span class="pre">python</span></code>
typically refers to Python 2.</p>
</div>
<div class="section" id="from-file-associations">
<h4>3.4.1.4. 從檔案關聯<a class="headerlink" href="#from-file-associations" title="本標題的永久連結">¶</a></h4>
<p>The launcher should have been associated with Python files (i.e. <code class="docutils literal notranslate"><span class="pre">.py</span></code>,
<code class="docutils literal notranslate"><span class="pre">.pyw</span></code>, <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files) when it was installed. This means that
when you double-click on one of these files from Windows explorer the launcher
will be used, and therefore you can use the same facilities described above to
have the script specify the version which should be used.</p>
<p>The key benefit of this is that a single launcher can support multiple Python
versions at the same time depending on the contents of the first line.</p>
</div>
</div>
<div class="section" id="shebang-lines">
<h3>3.4.2. Shebang Lines<a class="headerlink" href="#shebang-lines" title="本標題的永久連結">¶</a></h3>
<p>If the first line of a script file starts with <code class="docutils literal notranslate"><span class="pre">#!</span></code>, it is known as a
「shebang」 line. Linux and other Unix like operating systems have native
support for such lines and they are commonly used on such systems to indicate
how a script should be executed. This launcher allows the same facilities to
be used with Python scripts on Windows and the examples above demonstrate their
use.</p>
<p>To allow shebang lines in Python scripts to be portable between Unix and
Windows, this launcher supports a number of 『virtual』 commands to specify
which interpreter to use. The supported virtual commands are:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">/usr/bin/env</span> <span class="pre">python</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">/usr/bin/python</span></code></li>
<li><cite>/usr/local/bin/python`</cite></li>
<li><code class="docutils literal notranslate"><span class="pre">python</span></code></li>
</ul>
<p>For example, if the first line of your script starts with</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="ch">#! /usr/bin/python</span>
</pre></div>
</div>
<p>The default Python will be located and used. As many Python scripts written
to work on Unix will already have this line, you should find these scripts can
be used by the launcher without modification. If you are writing a new script
on Windows which you hope will be useful on Unix, you should use one of the
shebang lines starting with <code class="docutils literal notranslate"><span class="pre">/usr</span></code>.</p>
<p>Any of the above virtual commands can be suffixed with an explicit version
(either just the major version, or the major and minor version) - for example
<code class="docutils literal notranslate"><span class="pre">/usr/bin/python2.7</span></code> - which will cause that specific version to be located
and used.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">/usr/bin/env</span></code> form of shebang line has one further special property.
Before looking for installed Python interpreters, this form will search the
executable <span class="target" id="index-15"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> for a Python executable. This corresponds to the
behaviour of the Unix <code class="docutils literal notranslate"><span class="pre">env</span></code> program, which performs a <span class="target" id="index-16"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> search.</p>
</div>
<div class="section" id="arguments-in-shebang-lines">
<h3>3.4.3. Arguments in shebang lines<a class="headerlink" href="#arguments-in-shebang-lines" title="本標題的永久連結">¶</a></h3>
<p>The shebang lines can also specify additional options to be passed to the
Python interpreter. For example, if you have a shebang line:</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="ch">#! /usr/bin/python -v</span>
</pre></div>
</div>
<p>Then Python will be started with the <code class="docutils literal notranslate"><span class="pre">-v</span></code> option</p>
</div>
<div class="section" id="customization">
<h3>3.4.4. Customization<a class="headerlink" href="#customization" title="本標題的永久連結">¶</a></h3>
<div class="section" id="customization-via-ini-files">
<h4>3.4.4.1. Customization via INI files<a class="headerlink" href="#customization-via-ini-files" title="本標題的永久連結">¶</a></h4>
<p>Two .ini files will be searched by the launcher - <code class="docutils literal notranslate"><span class="pre">py.ini</span></code> in the current
user’s 「application data」 directory (i.e. the directory returned by calling the
Windows function <code class="docutils literal notranslate"><span class="pre">SHGetFolderPath</span></code> with <code class="docutils literal notranslate"><span class="pre">CSIDL_LOCAL_APPDATA</span></code>) and <code class="docutils literal notranslate"><span class="pre">py.ini</span></code> in the
same directory as the launcher. The same .ini files are used for both the
『console』 version of the launcher (i.e. py.exe) and for the 『windows』 version
(i.e. pyw.exe)</p>
<p>Customization specified in the 「application directory」 will have precedence over
the one next to the executable, so a user, who may not have write access to the
.ini file next to the launcher, can override commands in that global .ini file)</p>
</div>
<div class="section" id="customizing-default-python-versions">
<h4>3.4.4.2. Customizing default Python versions<a class="headerlink" href="#customizing-default-python-versions" title="本標題的永久連結">¶</a></h4>
<p>In some cases, a version qualifier can be included in a command to dictate
which version of Python will be used by the command. A version qualifier
starts with a major version number and can optionally be followed by a period
(『.』) and a minor version specifier. If the minor qualifier is specified, it
may optionally be followed by 「-32」 to indicate the 32-bit implementation of
that version be used.</p>
<p>For example, a shebang line of <code class="docutils literal notranslate"><span class="pre">#!python</span></code> has no version qualifier, while
<code class="docutils literal notranslate"><span class="pre">#!python3</span></code> has a version qualifier which specifies only a major version.</p>
<p>If no version qualifiers are found in a command, the environment variable
<code class="docutils literal notranslate"><span class="pre">PY_PYTHON</span></code> can be set to specify the default version qualifier - the default
value is 「2」. Note this value could specify just a major version (e.g. 「2」) or
a major.minor qualifier (e.g. 「2.6」), or even major.minor-32.</p>
<p>If no minor version qualifiers are found, the environment variable
<code class="docutils literal notranslate"><span class="pre">PY_PYTHON{major}</span></code> (where <code class="docutils literal notranslate"><span class="pre">{major}</span></code> is the current major version qualifier
as determined above) can be set to specify the full version. If no such option
is found, the launcher will enumerate the installed Python versions and use
the latest minor release found for the major version, which is likely,
although not guaranteed, to be the most recently installed version in that
family.</p>
<p>On 64-bit Windows with both 32-bit and 64-bit implementations of the same
(major.minor) Python version installed, the 64-bit version will always be
preferred. This will be true for both 32-bit and 64-bit implementations of the
launcher - a 32-bit launcher will prefer to execute a 64-bit Python installation
of the specified version if available. This is so the behavior of the launcher
can be predicted knowing only what versions are installed on the PC and
without regard to the order in which they were installed (i.e., without knowing
whether a 32 or 64-bit version of Python and corresponding launcher was
installed last). As noted above, an optional 「-32」 suffix can be used on a
version specifier to change this behaviour.</p>
<p>Examples:</p>
<ul class="simple">
<li>If no relevant options are set, the commands <code class="docutils literal notranslate"><span class="pre">python</span></code> and
<code class="docutils literal notranslate"><span class="pre">python2</span></code> will use the latest Python 2.x version installed and
the command <code class="docutils literal notranslate"><span class="pre">python3</span></code> will use the latest Python 3.x installed.</li>
<li>The commands <code class="docutils literal notranslate"><span class="pre">python3.1</span></code> and <code class="docutils literal notranslate"><span class="pre">python2.7</span></code> will not consult any
options at all as the versions are fully specified.</li>
<li>If <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3</span></code>, the commands <code class="docutils literal notranslate"><span class="pre">python</span></code> and <code class="docutils literal notranslate"><span class="pre">python3</span></code> will both use
the latest installed Python 3 version.</li>
<li>If <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3.1-32</span></code>, the command <code class="docutils literal notranslate"><span class="pre">python</span></code> will use the 32-bit
implementation of 3.1 whereas the command <code class="docutils literal notranslate"><span class="pre">python3</span></code> will use the latest
installed Python (PY_PYTHON was not considered at all as a major
version was specified.)</li>
<li>If <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3</span></code> and <code class="docutils literal notranslate"><span class="pre">PY_PYTHON3=3.1</span></code>, the commands
<code class="docutils literal notranslate"><span class="pre">python</span></code> and <code class="docutils literal notranslate"><span class="pre">python3</span></code> will both use specifically 3.1</li>
</ul>
<p>In addition to environment variables, the same settings can be configured
in the .INI file used by the launcher. The section in the INI file is
called <code class="docutils literal notranslate"><span class="pre">[defaults]</span></code> and the key name will be the same as the
environment variables without the leading <code class="docutils literal notranslate"><span class="pre">PY_</span></code> prefix (and note that
the key names in the INI file are case insensitive.) The contents of
an environment variable will override things specified in the INI file.</p>
<p>For example:</p>
<ul class="simple">
<li>Setting <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3.1</span></code> is equivalent to the INI file containing:</li>
</ul>
<div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[defaults]</span>
<span class="na">python</span><span class="o">=</span><span class="s">3.1</span>
</pre></div>
</div>
<ul class="simple">
<li>Setting <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3</span></code> and <code class="docutils literal notranslate"><span class="pre">PY_PYTHON3=3.1</span></code> is equivalent to the INI file
containing:</li>
</ul>
<div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[defaults]</span>
<span class="na">python</span><span class="o">=</span><span class="s">3</span>
<span class="na">python3</span><span class="o">=</span><span class="s">3.1</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="diagnostics">
<h3>3.4.5. Diagnostics<a class="headerlink" href="#diagnostics" title="本標題的永久連結">¶</a></h3>
<p>If an environment variable <code class="docutils literal notranslate"><span class="pre">PYLAUNCH_DEBUG</span></code> is set (to any value), the
launcher will print diagnostic information to stderr (i.e. to the console).
While this information manages to be simultaneously verbose <em>and</em> terse, it
should allow you to see what versions of Python were located, why a
particular version was chosen and the exact command-line used to execute the
target Python.</p>
</div>
</div>
<div class="section" id="finding-modules">
<span id="id1"></span><h2>3.5. Finding modules<a class="headerlink" href="#finding-modules" title="本標題的永久連結">¶</a></h2>
<p>Python usually stores its library (and thereby your site-packages folder) in the
installation directory. So, if you had installed Python to
<code class="file docutils literal notranslate"><span class="pre">C:\Python\</span></code>, the default library would reside in
<code class="file docutils literal notranslate"><span class="pre">C:\Python\Lib\</span></code> and third-party modules should be stored in
<code class="file docutils literal notranslate"><span class="pre">C:\Python\Lib\site-packages\</span></code>.</p>
<p>To completely override <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>, create a <code class="docutils literal notranslate"><span class="pre">._pth</span></code> file with the same
name as the DLL (<code class="docutils literal notranslate"><span class="pre">python37._pth</span></code>) or the executable (<code class="docutils literal notranslate"><span class="pre">python._pth</span></code>) and
specify one line for each path to add to <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>. The file based on the
DLL name overrides the one based on the executable, which allows paths to be
restricted for any program loading the runtime if desired.</p>
<p>When the file exists, all registry and environment variables are ignored,
isolated mode is enabled, and <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> is not imported unless one line in the
file specifies <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">site</span></code>. Blank paths and lines starting with <code class="docutils literal notranslate"><span class="pre">#</span></code> are
ignored. Each path may be absolute or relative to the location of the file.
Import statements other than to <code class="docutils literal notranslate"><span class="pre">site</span></code> are not permitted, and arbitrary code
cannot be specified.</p>
<p>Note that <code class="docutils literal notranslate"><span class="pre">.pth</span></code> files (without leading underscore) will be processed normally
by the <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> module when <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">site</span></code> has been specified.</p>
<p>When no <code class="docutils literal notranslate"><span class="pre">._pth</span></code> file is found, this is how <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> is populated on
Windows:</p>
<ul class="simple">
<li>An empty entry is added at the start, which corresponds to the current
directory.</li>
<li>If the environment variable <span class="target" id="index-17"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> exists, as described in
<a class="reference internal" href="cmdline.html#using-on-envvars"><span class="std std-ref">Environment variables</span></a>, its entries are added next. Note that on Windows,
paths in this variable must be separated by semicolons, to distinguish them
from the colon used in drive identifiers (<code class="docutils literal notranslate"><span class="pre">C:\</span></code> etc.).</li>
<li>Additional 「application paths」 can be added in the registry as subkeys of
<code class="samp docutils literal notranslate"><span class="pre">\SOFTWARE\Python\PythonCore\</span><em><span class="pre">version</span></em><span class="pre">\PythonPath</span></code> under both the
<code class="docutils literal notranslate"><span class="pre">HKEY_CURRENT_USER</span></code> and <code class="docutils literal notranslate"><span class="pre">HKEY_LOCAL_MACHINE</span></code> hives. Subkeys which have
semicolon-delimited path strings as their default value will cause each path
to be added to <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>. (Note that all known installers only use
HKLM, so HKCU is typically empty.)</li>
<li>If the environment variable <span class="target" id="index-18"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> is set, it is assumed as
「Python Home」. Otherwise, the path of the main Python executable is used to
locate a 「landmark file」 (either <code class="docutils literal notranslate"><span class="pre">Lib\os.py</span></code> or <code class="docutils literal notranslate"><span class="pre">pythonXY.zip</span></code>) to deduce
the 「Python Home」. If a Python home is found, the relevant sub-directories
added to <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> (<code class="docutils literal notranslate"><span class="pre">Lib</span></code>, <code class="docutils literal notranslate"><span class="pre">plat-win</span></code>, etc) are based on that
folder. Otherwise, the core Python path is constructed from the PythonPath
stored in the registry.</li>
<li>If the Python Home cannot be located, no <span class="target" id="index-19"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> is specified in
the environment, and no registry entries can be found, a default path with
relative entries is used (e.g. <code class="docutils literal notranslate"><span class="pre">.\Lib;.\plat-win</span></code>, etc).</li>
</ul>
<p>If a <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> file is found alongside the main executable or in the
directory one level above the executable, the following variations apply:</p>
<ul class="simple">
<li>If <code class="docutils literal notranslate"><span class="pre">home</span></code> is an absolute path and <span class="target" id="index-20"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> is not set, this
path is used instead of the path to the main executable when deducing the
home location.</li>
</ul>
<p>最終這所有的結果為:</p>
<ul class="simple">
<li>When running <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code>, or any other .exe in the main Python
directory (either an installed version, or directly from the PCbuild
directory), the core path is deduced, and the core paths in the registry are
ignored. Other 「application paths」 in the registry are always read.</li>
<li>When Python is hosted in another .exe (different directory, embedded via COM,
etc), the 「Python Home」 will not be deduced, so the core path from the
registry is used. Other 「application paths」 in the registry are always read.</li>
<li>If Python can’t find its home and there are no registry value (frozen .exe,
some very strange installation setup) you get a path with some default, but
relative, paths.</li>
</ul>
<p>For those who want to bundle Python into their application or distribution, the
following advice will prevent conflicts with other installations:</p>
<ul class="simple">
<li>Include a <code class="docutils literal notranslate"><span class="pre">._pth</span></code> file alongside your executable containing the
directories to include. This will ignore paths listed in the registry and
environment variables, and also ignore <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> unless <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">site</span></code> is
listed.</li>
<li>If you are loading <code class="file docutils literal notranslate"><span class="pre">python3.dll</span></code> or <code class="file docutils literal notranslate"><span class="pre">python37.dll</span></code> in your own
executable, explicitly call <a class="reference internal" href="../c-api/init.html#c.Py_SetPath" title="Py_SetPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetPath()</span></code></a> or (at least)
<a class="reference internal" href="../c-api/init.html#c.Py_SetProgramName" title="Py_SetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetProgramName()</span></code></a> before <a class="reference internal" href="../c-api/init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</li>
<li>Clear and/or overwrite <span class="target" id="index-21"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> and set <span class="target" id="index-22"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a>
before launching <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code> from your application.</li>
<li>If you cannot use the previous suggestions (for example, you are a
distribution that allows people to run <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code> directly), ensure
that the landmark file (<code class="file docutils literal notranslate"><span class="pre">Lib\os.py</span></code>) exists in your install directory.
(Note that it will not be detected inside a ZIP file, but a correctly named
ZIP file will be detected instead.)</li>
</ul>
<p>These will ensure that the files in a system-wide installation will not take
precedence over the copy of the standard library bundled with your application.
Otherwise, your users may experience problems using your application. Note that
the first suggestion is the best, as the others may still be susceptible to
non-standard paths in the registry and user site-packages.</p>
<div class="versionchanged">
<blockquote>
<div><span class="versionmodified">3.6 版更變: </span><ul class="simple">
<li>Adds <code class="docutils literal notranslate"><span class="pre">._pth</span></code> file support and removes <code class="docutils literal notranslate"><span class="pre">applocal</span></code> option from
<code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code>.</li>
<li>Adds <code class="docutils literal notranslate"><span class="pre">pythonXX.zip</span></code> as a potential landmark when directly adjacent
to the executable.</li>
</ul>
</div></blockquote>
</div>
<div class="deprecated">
<blockquote>
<div><span class="versionmodified">3.6 版後已棄用: </span><p>Modules specified in the registry under <code class="docutils literal notranslate"><span class="pre">Modules</span></code> (not <code class="docutils literal notranslate"><span class="pre">PythonPath</span></code>)
may be imported by <a class="reference internal" href="../library/importlib.html#importlib.machinery.WindowsRegistryFinder" title="importlib.machinery.WindowsRegistryFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.WindowsRegistryFinder</span></code></a>.
This finder is enabled on Windows in 3.6.0 and earlier, but may need to
be explicitly added to <a class="reference internal" href="../library/sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> in the future.</p>
</div></blockquote>
</div>
</div>
<div class="section" id="additional-modules">
<h2>3.6. Additional modules<a class="headerlink" href="#additional-modules" title="本標題的永久連結">¶</a></h2>
<p>Even though Python aims to be portable among all platforms, there are features
that are unique to Windows. A couple of modules, both in the standard library
and external, and snippets exist to use these features.</p>
<p>The Windows-specific standard modules are documented in
<a class="reference internal" href="../library/windows.html#mswin-specific-services"><span class="std std-ref">MS Windows Specific Services</span></a>.</p>
<div class="section" id="pywin32">
<h3>3.6.1. PyWin32<a class="headerlink" href="#pywin32" title="本標題的永久連結">¶</a></h3>
<p>The <a class="reference external" href="https://pypi.org/project/pywin32">PyWin32</a> module by Mark Hammond
is a collection of modules for advanced Windows-specific support. This includes
utilities for:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.microsoft.com/com/">Component Object Model</a> (COM)</li>
<li>Win32 API calls</li>
<li>登錄檔(Registry)</li>
<li>事件日誌(Event log)</li>
<li><a class="reference external" href="https://msdn.microsoft.com/en-us/library/fe1cf721%28VS.80%29.aspx">Microsoft Foundation Classes</a> (MFC)
user interfaces</li>
</ul>
<p><a class="reference external" href="https://web.archive.org/web/20060524042422/https://www.python.org/windows/pythonwin/">PythonWin</a> is a sample MFC application
shipped with PyWin32. It is an embeddable IDE with a built-in debugger.</p>
<div class="admonition seealso">
<p class="first admonition-title">也參考</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://timgolden.me.uk/python/win32_how_do_i.html">Win32 How Do I…?</a></dt>
<dd>by Tim Golden</dd>
<dt><a class="reference external" href="http://www.boddie.org.uk/python/COM.html">Python and COM</a></dt>
<dd>by David and Paul Boddie</dd>
</dl>
</div>
</div>
<div class="section" id="cx-freeze">
<h3>3.6.2. cx_Freeze<a class="headerlink" href="#cx-freeze" title="本標題的永久連結">¶</a></h3>
<p><a class="reference external" href="https://anthony-tuininga.github.io/cx_Freeze/">cx_Freeze</a> is a <a class="reference internal" href="../library/distutils.html#module-distutils" title="distutils: Support for building and installing Python modules into an existing Python installation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">distutils</span></code></a>
extension (see <a class="reference internal" href="../distutils/extending.html#extending-distutils"><span class="std std-ref">Extending Distutils</span></a>) which wraps Python scripts into
executable Windows programs (<code class="file docutils literal notranslate"><em><span class="pre">*</span></em><span class="pre">.exe</span></code> files). When you have done this,
you can distribute your application without requiring your users to install
Python.</p>
</div>
<div class="section" id="wconio">
<h3>3.6.3. WConio<a class="headerlink" href="#wconio" title="本標題的永久連結">¶</a></h3>
<p>Since Python’s advanced terminal handling layer, <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a>, is restricted to
Unix-like systems, there is a library exclusive to Windows as well: Windows
Console I/O for Python.</p>
<p><a class="reference external" href="http://newcenturycomputers.net/projects/wconio.html">WConio</a> is a wrapper for
Turbo-C’s <code class="file docutils literal notranslate"><span class="pre">CONIO.H</span></code>, used to create text user interfaces.</p>
</div>
</div>
<div class="section" id="compiling-python-on-windows">
<h2>3.7. 編譯 Python 在 Windows<a class="headerlink" href="#compiling-python-on-windows" title="本標題的永久連結">¶</a></h2>
<p>If you want to compile CPython yourself, first thing you should do is get the
<a class="reference external" href="https://www.python.org/downloads/source/">source</a>. You can download either the
latest release’s source or just grab a fresh <a class="reference external" href="https://devguide.python.org/setup/#getting-the-source-code">checkout</a>.</p>
<p>The source tree contains a build solution and project files for Microsoft
Visual Studio 2015, which is the compiler used to build the official Python
releases. These files are in the <code class="file docutils literal notranslate"><span class="pre">PCbuild</span></code> directory.</p>
<p>Check <code class="file docutils literal notranslate"><span class="pre">PCbuild/readme.txt</span></code> for general information on the build process.</p>
<p>For extension modules, consult <a class="reference internal" href="../extending/windows.html#building-on-windows"><span class="std std-ref">Building C and C++ Extensions on Windows</span></a>.</p>
<div class="admonition seealso">
<p class="first admonition-title">也參考</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://sebsauvage.net/python/mingw.html">Python + Windows + distutils + SWIG + gcc MinGW</a></dt>
<dd>or 「Creating Python extensions in C/C++ with SWIG and compiling them with
MinGW gcc under Windows」 or 「Installing Python extension with distutils
and without Microsoft Visual C++」 by Sébastien Sauvage, 2003</dd>
<dt><a class="reference external" href="http://oldwiki.mingw.org/index.php/Python%20extensions">MingW – Python extensions</a></dt>
<dd>by Trent Apted et al, 2007</dd>
</dl>
</div>
</div>
<div class="section" id="embedded-distribution">
<h2>3.8. 嵌入式發行版本<a class="headerlink" href="#embedded-distribution" title="本標題的永久連結">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">3.5 版新加入.</span></p>
</div>
<p>The embedded distribution is a ZIP file containing a minimal Python environment.
It is intended for acting as part of another application, rather than being
directly accessed by end-users.</p>
<p>When extracted, the embedded distribution is (almost) fully isolated from the
user’s system, including environment variables, system registry settings, and
installed packages. The standard library is included as pre-compiled and
optimized <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files in a ZIP, and <code class="docutils literal notranslate"><span class="pre">python3.dll</span></code>, <code class="docutils literal notranslate"><span class="pre">python37.dll</span></code>,
<code class="docutils literal notranslate"><span class="pre">python.exe</span></code> and <code class="docutils literal notranslate"><span class="pre">pythonw.exe</span></code> are all provided. Tcl/tk (including all
dependants, such as Idle), pip and the Python documentation are not included.</p>
<div class="admonition note">
<p class="first admonition-title">備註</p>
<p class="last">The embedded distribution does not include the <a class="reference external" href="https://www.microsoft.com/en-us/download/details.aspx?id=48145">Microsoft C Runtime</a> and it is
the responsibility of the application installer to provide this. The
runtime may have already been installed on a user’s system previously or
automatically via Windows Update, and can be detected by finding
<code class="docutils literal notranslate"><span class="pre">ucrtbase.dll</span></code> in the system directory.</p>
</div>
<p>Third-party packages should be installed by the application installer alongside
the embedded distribution. Using pip to manage dependencies as for a regular
Python installation is not supported with this distribution, though with some
care it may be possible to include and use pip for automatic updates. In
general, third-party packages should be treated as part of the application
(「vendoring」) so that the developer can ensure compatibility with newer
versions before providing updates to users.</p>
<p>The two recommended use cases for this distribution are described below.</p>
<div class="section" id="python-application">
<h3>3.8.1. Python Application<a class="headerlink" href="#python-application" title="本標題的永久連結">¶</a></h3>
<p>An application written in Python does not necessarily require users to be aware
of that fact. The embedded distribution may be used in this case to include a
private version of Python in an install package. Depending on how transparent it
should be (or conversely, how professional it should appear), there are two
options.</p>
<p>Using a specialized executable as a launcher requires some coding, but provides
the most transparent experience for users. With a customized launcher, there are
no obvious indications that the program is running on Python: icons can be
customized, company and version information can be specified, and file
associations behave properly. In most cases, a custom launcher should simply be
able to call <code class="docutils literal notranslate"><span class="pre">Py_Main</span></code> with a hard-coded command line.</p>
<p>The simpler approach is to provide a batch file or generated shortcut that
directly calls the <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> or <code class="docutils literal notranslate"><span class="pre">pythonw.exe</span></code> with the required
command-line arguments. In this case, the application will appear to be Python
and not its actual name, and users may have trouble distinguishing it from other
running Python processes or file associations.</p>
<p>With the latter approach, packages should be installed as directories alongside
the Python executable to ensure they are available on the path. With the
specialized launcher, packages can be located in other locations as there is an
opportunity to specify the search path before launching the application.</p>
</div>
<div class="section" id="embedding-python">
<h3>3.8.2. 嵌入 Python<a class="headerlink" href="#embedding-python" title="本標題的永久連結">¶</a></h3>
<p>Applications written in native code often require some form of scripting
language, and the embedded Python distribution can be used for this purpose. In
general, the majority of the application is in native code, and some part will
either invoke <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> or directly use <code class="docutils literal notranslate"><span class="pre">python3.dll</span></code>. For either case,
extracting the embedded distribution to a subdirectory of the application
installation is sufficient to provide a loadable Python interpreter.</p>
<p>As with the application use, packages can be installed to any location as there
is an opportunity to specify search paths before initializing the interpreter.
Otherwise, there is no fundamental differences between using the embedded
distribution and a regular installation.</p>
</div>
</div>
<div class="section" id="other-resources">
<h2>3.9. 其他資源<a class="headerlink" href="#other-resources" title="本標題的永久連結">¶</a></h2>
<div class="admonition seealso">
<p class="first admonition-title">也參考</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://shop.oreilly.com/product/9781565926219.do">Python Programming On Win32</a></dt>
<dd>「Help for Windows Programmers」
by Mark Hammond and Andy Robinson, O’Reilly Media, 2000,
ISBN 1-56592-621-8</dd>
<dt><a class="reference external" href="http://www.imladris.com/Scripts/PythonForWindows.html">A Python for Windows Tutorial</a></dt>
<dd>by Amanda Birmingham, 2004</dd>
<dt><span class="target" id="index-23"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0397"><strong>PEP 397</strong></a> - Python launcher for Windows</dt>
<dd>The proposal for the launcher to be included in the Python distribution.</dd>
</dl>
</div>
</div>
</div>
</div>
</div>
</div>