Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

Python.txt 65 KB

You have to be logged in to leave a comment. Sign In
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
  1. = Python Packaging Guidelines
  2. :last-reviewed: 2024-04-03
  3. ////
  4. Additional style notes:
  5. - Normative points are at the beginnings of sections
  6. and include highlighted keywords like **MUST** or **SHOULD**.
  7. So, if a reader only wants the rules,
  8. they can skip remainders of sections
  9. (discussion, examples, clarifications, ...)
  10. without missing context.
  11. - Some sections have additional IDs
  12. (see https://docs.asciidoctor.org/asciidoc/latest/sections/custom-ids/ )
  13. to avoid breaking links to removed/renamed sections.
  14. - In text, macros appear as Literal Monospace with a leading percent sign.
  15. Use curly braces for value-like macros, but not command-like macros
  16. (for example: `%{version}` but `%autosetup` or `%check`).
  17. - The first mention of a macro generally links to the documentation.
  18. The link includes some non-monospace text, e.g. "the `+%{version}+` macro".
  19. (In the current Fedora docs style, links aren't visible in monospace text).
  20. - The documentation for a macro has the macro's name as ID.
  21. ////
  22. This version of Python Packaging Guidelines
  23. is in effect since 2021
  24. and represents a major rewrite and paradigm shift.
  25. Not all packages are updated to reflect this.
  26. Older guidelines are still being kept up to date,
  27. and existing packages *MAY* use them instead of this document:
  28. * xref:Python_201x.adoc[“201x-era” Python packaging guidelines]
  29. (Packages using these usually use the
  30. <<py3_install,`+%py3_install+`>>
  31. or <<py3_install_wheel,`+%py3_install_wheel+` macro>>
  32. or call `+setup.py install+`.)
  33. * xref:Python_Appendix.adoc#_python_2_packages[Python 2 appendix]
  34. Note that Python 2 packages require a FESCo exception.
  35. ////
  36. NOTE: A summary of changes from the older guidelines
  37. is available at https://hackmd.io/@python-maint/rJmQQc4DP
  38. ////
  39. NOTE: These guidelines only support current Fedora releases.
  40. For older releases (such as in EPEL 8),
  41. consult the xref:Python_201x.adoc[201x-era guidelines].
  42. The two <<Distro-wide guidelines>> below
  43. apply to all software in Fedora that uses Python at build- or run-time.
  44. The rest of the Guidelines apply to packages that ship code
  45. that can be imported with Python’s `+import+` statement.
  46. Specifically, that is all packages
  47. that install files under `+/usr/lib*/python*/+`.
  48. Except for the two “Distro-wide guidelines”,
  49. these Guidelines do not apply to simple one-file scripts or utilities,
  50. especially if these are included with software
  51. not written in Python.
  52. However, if an application (e.g. CLI tool, script or GUI app)
  53. needs a more complex Python library,
  54. the library *SHOULD* be packaged as an importable library
  55. under these guidelines.
  56. A major goal for Python packaging in Fedora is to
  57. _harmonize with the wider Python ecosystem_,
  58. that is,
  59. the https://pypa.io[Python Packaging Authority] (PyPA) standards
  60. and the https://pypi.org/[Python Package Index] (PyPI).
  61. Packagers *SHOULD* be prepared to get involved with upstream projects
  62. to establish best practices as outlined here.
  63. We wish to improve both Fedora and the wider Python ecosystem.
  64. NOTE: Some build tools (like CMake or autotools)
  65. may not work with the latest PyPA standards yet.
  66. (For example, they might generate `+.egg-info+` directories
  67. rather than `+.dist-info+`.)
  68. While this document's normative points (MUST/SHOULD) are tool-agnostic,
  69. many of the practical tips and helper macros will not be applicable.
  70. If this affects you, consider contacting the
  71. https://lists.fedoraproject.org/archives/list/python-devel@lists.fedoraproject.org/[Python SIG]
  72. for guidance
  73. and/or following the xref:Python_201x.adoc[older guidelines]
  74. for the time being.
  75. NOTE: Fedora’s Python SIG not only develops these guidelines,
  76. but it’s also involved in PyPA standards and Python packaging best practices.
  77. Check out https://fedoraproject.org/wiki/SIGs/Python[the wiki] or
  78. https://lists.fedoraproject.org/archives/list/python-devel@lists.fedoraproject.org/[mailing list]
  79. if you need help or wish to help out.
  80. == Distro-wide guidelines
  81. === BuildRequire python3-devel
  82. *Every* package that uses Python (at runtime and/or build time)
  83. and/or installs Python modules
  84. *MUST* explicitly BuildRequire `+python3-devel+`,
  85. even if Python is not actually invoked during build time.
  86. Such package *MUST* include `+BuildRequires: python3-devel+` in its `+.spec+` file
  87. or use <<pyproject_buildrequires,the `+%pyproject_buildrequires+` macro>>
  88. in the `+%generate_buildrequires+` section.
  89. If the package uses an alternate Python interpreter instead of `+python3+`
  90. (e.g. `+pypy+`, `+jython+`, `+python2.7+`),
  91. it *MAY* instead require the corresponding `+*-devel+` package.
  92. The `+*-devel+` package brings in relevant RPM macros.
  93. It may also enable automated or manual checks: for example,
  94. Python maintainers use this requirement to list packages
  95. that use Python in some way
  96. and might be affected by planned changes.
  97. === Mandatory macros
  98. The following macros *MUST* be used where applicable.
  99. The expansions in parentheses are provided only as reference/examples.
  100. The macros are defined for you in all supported Fedora and EPEL
  101. versions.
  102. // Keep the list synced with "Macro Reference" below
  103. * `+%{python3}+` (`+/usr/bin/python3+`):
  104. The Python interpreter.
  105. For example, this macro should be
  106. used for invoking Python from a `+spec+` file script,
  107. passed to `+configure+` scripts to select a Python executable,
  108. or used as `+%{python3} -m pip+` to run a Python-based tool.
  109. +
  110. If the packaged software invokes Python at _run time_
  111. (as opposed to running Python to build/test it),
  112. it might be necessary to pass flags to `+%{python3}+`
  113. to isolate it from user-installed packages.
  114. See <<Shebangs>> for details.
  115. +
  116. * `+%{python3_version}+` (e.g. `+3.9+`, `+3.10+`):
  117. Version of the Python interpreter.
  118. +
  119. * `+%{python3_version_nodots}+` (e.g. `+39+`, `+310+`):
  120. Version of the Python interpreter without the dot.
  121. * `+%{python3_sitelib}+` (e.g. `+/usr/lib/python3.9/site-packages+`):
  122. Where pure-Python modules are installed.
  123. +
  124. * `+%{python3_sitearch}+` (e.g. `+/usr/lib64/python3.9/site-packages+`):
  125. Where Python extension modules (native code, e.g. compiled from C) are
  126. installed.
  127. The rest of this document uses these macros,
  128. along with `+%{_bindir}+` (`+/usr/bin/+`),
  129. instead of the raw path names.
  130. === Python implementation support[[_multiple_python_runtimes]]
  131. Fedora primarily targets _CPython_,
  132. the reference implementation of the Python language.
  133. We generally use “Python” to mean CPython.
  134. Alternate implementations like `+pypy+` are available,
  135. but currently lack comprehensive tooling and guidelines for packaging.
  136. When targetting these, there are no hard rules
  137. (except the general Fedora packaging guidelines).
  138. But please try to abide by the _spirit_ of these guidelines.
  139. When in doubt, consider consulting the Python SIG.
  140. === Python version support
  141. Fedora packages *MUST NOT* depend on other versions
  142. of the CPython interpreter
  143. than the current `+python3+`.
  144. In Fedora, Python libraries are packaged for a single version of Python,
  145. called `+python3+`.
  146. For example, in Fedora 32, `+python3+` is Python 3.8.
  147. In the past, there were multiple Python stacks,
  148. e.g. `+python3.7+` and `+python2.7+`,
  149. installable together on the same machine.
  150. That is also the case in some projects that build _on top_ of Fedora,
  151. like RHEL, EPEL and CentOS.
  152. Fedora might re-introduce parallell-installable stacks in the future
  153. (for example if a switch to a new Python version needs a transition period,
  154. or if enough interested maintainers somehow appear).
  155. Fedora does include alternate interpreter versions,
  156. e.g. `+python2.7+` or `+python3.5+`,
  157. but these are meant only for developers that need to test upstream code.
  158. Bug and security fixes for these interpreters only cover this use case.
  159. Packages such as `+pip+` or `+tox+`,
  160. which enable setting up isolated environments
  161. and installing third-party packages into them,
  162. *MAY*, as an exception to the rule above, use these interpreters
  163. as long as this is coordinated with the maintainers
  164. of the relevant Python interpreter.
  165. == Naming
  166. Python packages have several different names,
  167. which should be kept in sync but will sometimes differ
  168. for historical or practical reasons.
  169. They are:
  170. * the Fedora _source package name_ (or _component name_, `+%{name}+`),
  171. * the Fedora _built RPM name_,
  172. * the _project name_ used on https://pypi.org/[PyPI]
  173. or by https://pip.pypa.io[pip], and
  174. * the _importable module name_ used in Python
  175. (a single package may have multiple importable modules).
  176. Some examples (both good and worse):
  177. [cols=",,,",options="header",]
  178. |===
  179. |Fedora component |Built RPM |Project name |Importable module
  180. |`+python-requests+` |`+python3-requests+` |`+requests+` |`+requests+`
  181. |`+python-django+` |`+python3-django+` |`+Django+` |`+django+`
  182. |`+PyYAML+` |`+python3-pyyaml+` |`+pyyaml+` |`+yaml+`
  183. |`+python-ldap+` |`+python3-ldap+` |`+python-ldap+` |`+ldap+`, `+ldif+`, etc.
  184. |`+python-pillow+` |`+python3-pillow+` |`+pillow+` |`+PIL+`
  185. |`+python-jupyter-client+` |`+python3-jupyter-client+` |`+jupyter-client+` |`+jupyter_client+`
  186. |===
  187. Elsewhere in this text,
  188. the metavariables `+SRPMNAME+`, `+RPMNAME+`, `+PROJECTNAME+`, `+MODNAME+`
  189. refer to these names, respectively.
  190. === Canonical project name
  191. Most of these names are case-sensitive machine-friendly identifiers,
  192. but the _project name_ has human-friendly semantics:
  193. it is case-insensitive
  194. and treats some sets of characters (like `+._-+`) specially.
  195. For automated use,
  196. it needs to be normalized to a canonical format
  197. used by Python tools and services such as setuptools, pip and PyPI.
  198. For example, the canonical name of the `+Django+` project
  199. is `+django+` (in lowercase).
  200. This normalization is defined in
  201. https://www.python.org/dev/peps/pep-0503/#normalized-names[PEP 503],
  202. and <<py_dist_name,the `+%{py_dist_name}+` macro>> implements it
  203. for Fedora packaging.
  204. The canonical name is obtained by switching the project name to lower case
  205. and converting all runs of non-alphanumeric characters to single “-” characters.
  206. Example: “The $$$ Tree” becomes “the-tree”.
  207. Elsewhere in this text,
  208. the metavariable `+DISTNAME+` refers to the canonical form of the project name.
  209. Note that in some places, the original,
  210. non-normalized project name must be used.
  211. For example,
  212. the <<pypi_source,`+%pypi_source+` macro>> and the `+%autosetup+` macro
  213. need `+Django+`, not `+django+`.
  214. === Name limitations
  215. The character `pass:[+]` in names of built (i.e. non-SRPM) packages
  216. that include `+.dist-info+` or `+.egg-info+` directories
  217. is reserved for <<Extras>> and *MUST NOT* be used for any other purpose.
  218. As an exception, `pass:[+]` characters *MAY* appear at the _end_ of such names.
  219. The `pass:[+]` character triggers
  220. the automatic dependency generator for extras.
  221. Replace any `pass:[+]` signs in the upstream name with `+-+`.
  222. Omit `pass:[+]` signs on the beginning of the name.
  223. Consider adding `Provides` for the original name with `pass:[+]` characters
  224. to make the package easier to find for users.
  225. === Library naming
  226. A built (i.e. non-SRPM) package for a _Python library_
  227. *MUST* be named with the prefix `+python3-+`.
  228. A source package containing primarily a _Python library_
  229. *MUST* be named with the prefix `+python-+`.
  230. The Fedora package’s name *SHOULD* contain
  231. the <<Canonical project name>>.
  232. If possible, the project name *SHOULD* be the same
  233. as the name of the main importable module,
  234. in lowercase,
  235. with underscores (`+_+`) replaced by dashes (`+-+`).
  236. If the importable module name and the project name do not match,
  237. users frequently end up confused.
  238. In this case, packagers *SHOULD* ensure that upstream is aware of the problem
  239. and (especially for new packages where renaming is feasible)
  240. strive to get the package renamed.
  241. The Python SIG is available for assistance.
  242. A _Python library_ is a package meant to be imported in Python,
  243. such as with `+import requests+`.
  244. Tools like _Ansible_ or _IDLE_, whose code is importable
  245. but not primarily meant to be imported from other software,
  246. are not considered libraries in this sense.
  247. So, this section does not apply for them.
  248. (See the
  249. xref:index#_libraries_and_applications[general Libraries and Applications guidelines]
  250. for general guidance.)
  251. The Fedora component (source package) name for a library
  252. should be formed by taking the _canonical project name_
  253. and prepending `+python-+` if it does not already start with `+python-+`.
  254. This may leads to conflicts
  255. (e.g. between https://pypi.org/project/bugzilla/[bugzilla]
  256. and https://pypi.org/project/python-bugzilla/[python-bugzilla]).
  257. In that case, ensure upstream is aware of the potentially confusing naming
  258. and apply best judgment.
  259. === Application naming
  260. Packages that primarily provide applications, services
  261. or any kind of executables *SHOULD* be named
  262. according to the general xref:Naming.adoc[Fedora naming guidelines]
  263. (e.g. `+ansible+`).
  264. Consider adding a virtual provide according to <<Library naming>> above
  265. (e.g. `+python3-PROJECTNAME+`),
  266. if it would help users find the package.
  267. == Files to include
  268. === Source files and bytecode cache[[_byte_compiling]]
  269. Packages *MUST* include the source file (`+*.py+`)
  270. *AND* the bytecode cache (`+*.pyc+`) for each pure-Python importable module.
  271. The source files *MUST* be included in the same package as the bytecode cache.
  272. Scripts that are not importable
  273. (typically ones in `+%{_bindir}+` or `+%{_libexecdir}+`)
  274. *SHOULD NOT* be byte-compiled.
  275. The cache files are found in a `+__pycache__+` directory
  276. and have an interpreter-dependent suffix like `+.cpython-39.pyc+`.
  277. The cache is not necessary to run the software,
  278. but if it is not found, Python will try to create it when a module is imported.
  279. If this succeeds, the file is not tracked by RPM
  280. and it will linger on the system after uninstallation.
  281. If it does not succeed, users can get spurious SELinux AVC denials in the logs.
  282. Normally, byte compilation (generating the cache files)
  283. is done for you by the `+brp-python-bytecompile+`
  284. xref:index.adoc#_brp_buildroot_policy_scripts[BRP script],
  285. which runs automatically
  286. after the `+%install+` section of the spec file has been processed.
  287. It byte-compiles any `+.py+` files that it finds
  288. in `+%{python3_sitelib}+` or `+%{python3_sitearch}+`.
  289. You must include these files of your package
  290. (i.e. in the `+%files+` section).
  291. If the code is in a subdirectory (importable package),
  292. include the entire directory:
  293. [source,spec]
  294. ----
  295. %files
  296. %{python3_sitelib}/foo/
  297. ----
  298. Adding the trailing slash is best practice for directories.
  299. However, this cannot be used for top-level modules (those directly in
  300. e.g. `+%{python3_sitelib}+`), because both `+%{python3_sitelib}+` and
  301. `+%{python3_sitelib}/__pycache__/+` are owned by Python itself. Here, the
  302. `+%pycached+` macro can help. It expands to the given `+*.py+` source file
  303. and its corresponding cache file(s). For example:
  304. [source,spec]
  305. ----
  306. %files
  307. %pycached %{python3_sitelib}/foo.py
  308. ----
  309. expands roughly to:
  310. [source,spec]
  311. ----
  312. %files
  313. %{python3_sitelib}/foo.py
  314. %{python3_sitelib}/__pycache__/foo.cpython-3X{,.opt-?}.pyc
  315. ----
  316. ==== Manual byte compilation[[manual-bytecompilation]]
  317. If you need to bytecompile stuff
  318. outside of `+%{python3_sitelib}+`/`+%{python3_sitearch}+`,
  319. use the <<py_byte_compile,`+%py_byte_compile+` macro>>.
  320. For example,
  321. if your software adds `+%{_datadir}/mypackage+` to Python’s import path
  322. and imports package `+foo+` from there, you will need to compile `+foo+` with:
  323. [source,spec]
  324. ----
  325. %py_byte_compile %{python3} %{buildroot}%{_datadir}/mypackage/foo/
  326. ----
  327. === Dist-info metadata
  328. Each Python package *MUST* include _Package Distribution Metadata_
  329. conforming to https://packaging.python.org/specifications/[PyPA specifications]
  330. (specifically,
  331. https://packaging.python.org/specifications/recording-installed-packages/[Recording installed projects]).
  332. The metadata *SHOULD* be included
  333. in the same subpackage as the main importable module,
  334. if there is one.
  335. This applies to libraries (e.g. `+python-requests+`)
  336. as well as tools (e.g. `+ansible+`).
  337. When software is split into several subpackages,
  338. it is OK to only ship metadata in one built RPM.
  339. In this case, consider working with upstream
  340. to also split the upstream project.
  341. The metadata takes the form of a `+.dist-info+` directory
  342. installed in `+%{python3_sitelib}+` or `+%{python3_sitearch}+`,
  343. and contains information that tools like
  344. https://docs.python.org/3/library/importlib.metadata.html[`+importlib.metadata+`]
  345. use to introspect installed libraries.
  346. For example, a project named `+MyLib+` with importable package `+mylib+`
  347. could be packaged with:
  348. [source,spec]
  349. ----
  350. %files -p python3-mylib
  351. %{python3_sitelib}/mylib/
  352. %{python3_sitelib}/MyLib-%{version}.dist-info/
  353. %doc README.md
  354. %license LICENSE.txt
  355. ----
  356. Note that some older tools instead put metadata in an `+.egg-info+` directory,
  357. or even a single file.
  358. This won’t happen if you use the `+%pyproject_wheel+` macro.
  359. If your package uses a build system
  360. that generates an `+.egg-info+` directory or file,
  361. please contact Python SIG.
  362. As an exception,
  363. the Python standard library *MAY* ship without this metadata.
  364. === Explicit lists
  365. Packages *MUST NOT* own shared directories owned by Python itself,
  366. such as the top-level `+__pycache__+` directories
  367. (`+%{python3_sitelib}/__pycache__+`, `+%{python3_sitearch}/__pycache__+`).
  368. Similarly to the xref:index#_explicit_lists[general rule],
  369. packagers *SHOULD NOT* simply glob everything under a shared directory.
  370. In addition to the xref:index#_explicit_lists[general list],
  371. the following *SHOULD NOT* be used in `+%files+`:
  372. * `+%{python3_sitelib}/*+`
  373. * `+%{python3_sitearch}/*+`
  374. * `+%{python_sitelib}/*+`
  375. * `+%{python_sitearch}/*+`
  376. * `+%pyproject_save_files '*'+`
  377. * `+%pyproject_save_files +auto+`
  378. This rule serves as a check against common mistakes
  379. which are otherwise hard to detect.
  380. It does limit some possibilities for automation.
  381. The most common mistakes this rule prevents are:
  382. * installing a test suite system-wide as an importable module named `+test+`,
  383. which would then conflict with other such packages, and
  384. * upstream adding new unexpected importable modules –
  385. you should always check such changes for
  386. https://docs.fedoraproject.org/en-US/packaging-guidelines/Conflicts/#_common_conflicting_files_cases_and_solutions[conflicts],
  387. and keep the list of such files explicit and auditable.
  388. == PyPI parity
  389. Every Python package in Fedora *SHOULD* also be available
  390. on https://pypi.org[the Python Package Index] (PyPI).
  391. The command `+pip install PROJECTNAME+` *MUST*
  392. install the same package (possibly in a different version),
  393. install nothing,
  394. or fail with a reasonable error message.
  395. If this is not the case,
  396. the packager *SHOULD* contact upstream about this.
  397. The goal is to get the project name registered or blocked on PyPI,
  398. or to otherwise ensure the rule is followed.
  399. If your package is not or cannot be published on PyPI, you can:
  400. * Ask upstream to publish it
  401. * If you wish: publish it to PyPI yourself and maintain it
  402. * Ask mailto:python-devel@lists.fedoraproject.org[Python SIG]
  403. to _block_ the name on PyPI for you
  404. * Email mailto:admin@pypi.org[PyPI admins] to block the name for you,
  405. giving the project name and explaining the situation
  406. (for example: the package cannot currently be installed via `+pip+`).
  407. You can ask questions and discuss the process at the
  408. https://discuss.python.org/t/block-names/4045[Python Discourse].
  409. NOTE: Project names that were in Fedora but not on PyPI
  410. when these guidelines were proposed are _blocked_ from being uploaded to PyPI.
  411. This prevents potential trolls from taking them,
  412. but it also blocks legitimate owners.
  413. If your package is affected, contact the Python SIG or
  414. https://github.com/pypa/pypi-support/issues/new?labels=PEP+541&template=pep541-request.md&title=PEP+541+Request%3A+PROJECT_NAME[file a PyPA issue]
  415. and mention `+@encukou+`.
  416. If your package’s project name conflicts with a different package on PyPI,
  417. change the project name.
  418. As painful as it is,
  419. we need to use a single global namespace across the Python ecosystem.
  420. Software that is not written specifically for Fedora already expects
  421. that project names use the PyPI namespace:
  422. for example, if a third-party library identifies a dependency by name,
  423. we don’t want that dependency satisfied by an unrelated Fedora package.
  424. As always,
  425. xref:index.adoc#_general_exception_policy[specific exceptions]
  426. can be granted by the Packaging Committee.
  427. == Provides and requirements[[_provides]]
  428. === Provides for importable modules[[_the_py_provides_macro]]
  429. For any module intended to be used in Python 3 with `+import MODNAME+`,
  430. the package that includes it *SHOULD* provide `+python3-MODNAME+`,
  431. with underscores (`+_+`) replaced by dashes (`+-+`).
  432. This is of course always the case if the package is named `+python3-MODNAME+`.
  433. If the subpackage has some other name,
  434. then add `+%py_provides python3-MODNAME+` explicitly.
  435. See the following section to learn about `+%py_provides+`.
  436. [#Automatic-unversioned-provides]
  437. === Automatic python- and python3.X- provides
  438. For any `+FOO+`,
  439. a package that provides `+python3-FOO+` *SHOULD* use `+%py_provides+`
  440. or an automatic generator
  441. to also provide `+python-FOO+`
  442. and `+python3.X-FOO+`, where `+X+` is the minor version of the interpreter.
  443. The provide *SHOULD NOT* be added manually:
  444. if a generator or macro is not used,
  445. do not add the `+python-FOO+` / `+python3.X-FOO+` provides at all.
  446. This is done automatically for package names by a generator.
  447. If absolutely necessary, the generator can be disabled
  448. by undefining <<__pythonname_provides,the `+%__pythonname_provides+` macro>>.
  449. For provides that aren’t package names,
  450. or (for technical reasons) for packages without files,
  451. the generator will not work.
  452. For these cases, the following invocation will provide `+python3-FOO+`,
  453. `+python-FOO+` and `+python3.X-FOO+`:
  454. [source,spec]
  455. ----
  456. %py_provides python3-FOO
  457. ----
  458. Using the generator or macro is important, because the specific form of
  459. the provide may change in the future.
  460. [#Machine-readable-provides]
  461. === Machine-readable provides[[_automatic_provides_with_a_standardized_name]]
  462. Every Python package *MUST* provide `+python3dist(DISTNAME)+`
  463. *and* `+python3.Xdist(DISTNAME)+`,
  464. where `+X+` is the minor version of the interpreter
  465. and `+DISTNAME+` is the <<Canonical project name>>
  466. corresponding to the <<Dist-info metadata>>.
  467. For example, `+python3-django+` would provide
  468. `+python3dist(django)+` and `+python3.9dist(django)+`.
  469. This is generated automatically from the dist-info metadata.
  470. The provide *SHOULD NOT* be added manually:
  471. if the generator fails to add it, the metadata *MUST* be fixed.
  472. These _Provides_ are used for automatically generated _Requires_.
  473. If absolutely necessary,
  474. the automatic generator can be disabled by undefining the
  475. <<__pythondist_provides,`+%{?__pythondist_provides}+` macro>>.
  476. Consider discussing your use case with the Python SIG if you need to do this.
  477. === Dependencies
  478. As mentioned above,
  479. each Python package *MUST* explicitly BuildRequire `+python3-devel+`.
  480. Packages *MUST NOT* have dependencies (either build-time or runtime)
  481. with the unversioned prefix `+python-+`
  482. if the corresponding `+python3-+` dependency can be used instead.
  483. Packages *SHOULD NOT* have explicit dependencies
  484. (either build-time or runtime)
  485. with a minor-version prefix such as `+python3.8-+` or `+python3.8dist(+`.
  486. Such dependencies *SHOULD* instead be automatically generated
  487. or a macro should be used to get the version.
  488. Packages *SHOULD NOT* have an explicit runtime dependency on `+python3+`.
  489. Instead of depending on `+python3+`,
  490. packages have an automatic dependency on `+python(abi) = 3.X+`
  491. when they install files to `+%{python3_sitelib}+` or `+%{python3_sitearch}+`,
  492. or they have an automatic dependency on `+/usr/bin/python3+`
  493. if they have executable Python scripts,
  494. or they have an automatic dependency on `+libpython3.X.so.1.0()+`
  495. if they embed Python.
  496. These rules help ensure a smooth upgrade path
  497. when `+python3+` is updated in new versions of Fedora.
  498. [#Automatically-generated-dependencies]
  499. === Automatically generated dependencies[[_requires_and_buildrequires_with_standardized_names]]
  500. Packages *MUST* use the automatic Python run-time dependency generator.
  501. Packages *SHOULD* use the opt-in build-dependency generator if possible.
  502. The packager *MUST* inspect the generated requires for correctness.
  503. All dependencies *MUST* be resolvable within the targeted Fedora version.
  504. Any necessary changes *MUST* be done by patches
  505. or modifying the source (e.g. with `+sed+`),
  506. rather than disabling the generator.
  507. The resulting change *SHOULD* be offered to upstream.
  508. As an exception, xref:AutoProvidesAndRequiresFiltering.adoc[filtering]
  509. *MAY* be used for temporary workarounds
  510. and xref:index.adoc#bootstrapping[bootstrapping].
  511. Dependencies covered by the generators *SHOULD NOT*
  512. be repeated in the `+.spec+` file.
  513. (For example, if the generator finds a `+requests+` dependency,
  514. then `+Requires: python3-requests+` is redundant.)
  515. The automatically generated requirements
  516. are in the form `+python3.Xdist(DISTNAME)+`,
  517. potentially augmented with version requirements or combined together
  518. with https://rpm-software-management.github.io/rpm/manual/boolean_dependencies.html[rich dependencies].
  519. Any `+.0+` suffixes are removed from version numbers
  520. to match the behavior of Python tools.
  521. (https://www.python.org/dev/peps/pep-0440/[PEP 440] specifies
  522. that `+X.Y+` and `+X.Y.0+` are treated as equal.)
  523. Note that the generators only cover Python packages.
  524. Other dependencies, often C libraries like `+openssl-devel+`,
  525. must be specified in the `+.spec+` file manually.
  526. Where the requirements are specified in the source
  527. depends on each project’s build system and preferences.
  528. Common locations are `+pyproject.toml+`, `+setup.py+`, `+setup.cfg+`,
  529. `+config.toml+`.
  530. ==== Run-time dependency generator
  531. The automatic runtime dependency generator uses package metadata
  532. (as recorded in installed `+*.dist-info+` directories)
  533. to determine what the package depends on.
  534. In an emergency, you can opt-out from running the requires generator by
  535. <<python_disable_dependency_generator, adding `+%{?python_disable_dependency_generator}+`>>
  536. to the package (usually, just before the main package’s `+%description+`).
  537. ==== Build-time dependency generator
  538. The opt-in (but strongly recommended) build-time dependency generator
  539. gathers information from
  540. https://www.python.org/dev/peps/pep-0517/#source-trees[`+pyproject.toml+` build-system information]
  541. (with fallback to `+setuptools+`) plus a standardized
  542. https://www.python.org/dev/peps/pep-0517/#get-requires-for-build-wheel[build-system hook]
  543. to gather further requirements.
  544. See <<pyproject_buildrequires,the `+%pyproject_buildrequires+` macro>>
  545. for more details.
  546. Note that without the `+-R+` flag, the generator will include run-time
  547. requirements in BuildRequires. This is useful for running tests and for
  548. checking that the dependencies are available in Fedora.
  549. === Test dependencies
  550. See the <<Tests>> section.
  551. [#Extras]
  552. === Extras[[_python_extras]]
  553. Python extras are a way for Python projects
  554. to declare that extra dependencies are required for additional functionality.
  555. For example,
  556. `+requests+` has several standard dependencies (e.g. `+urllib3+`).
  557. But it also declares an _extra_ named `+requests[security]+`,
  558. which lists additional dependencies (e.g. `+cryptography+`).
  559. Unlike RPM subpackages, extras can only specify additional dependencies,
  560. not additional files.
  561. The main package will work if the optional dependency is not installed,
  562. but it might have limited functionality.
  563. Python tools treat extras as virtual packages.
  564. For example, if a user runs `+pip install 'requests[security]'+`,
  565. or installs a project that depends on `+requests[security]+`,
  566. both `+requests+` and `+cryptography+` will be installed.
  567. In Fedora, extras are usually provided by packages with no files.
  568. Instead of square brackets,
  569. Fedora package names conventionally use the `pass:[+]` character
  570. (which is valid in RPM package names,
  571. but not in Python canonical project names nor in extras identifiers).
  572. ==== Handling extras
  573. Python packages *SHOULD* have Provides
  574. for all extras the upstream project specifies, except:
  575. * those that are not useful for other packages
  576. (for example build/development requirements,
  577. commonly named `+dev+`, `+doc+` or `+test+`), and
  578. * those that have requirements that are not packaged in Fedora.
  579. A package that provides a Python extra
  580. *MUST* provide `+python3dist(DISTNAME[EXTRA])+`
  581. *and* `+python3.Xdist(DISTNAME[EXTRA])+`,
  582. where `+X+` is the minor version of the interpreter,
  583. `+DISTNAME+` is the <<Canonical project name>>,
  584. and `+EXTRA+` is the name of a single extra.
  585. For example, `+python3.9dist(requests[security])+`.
  586. These requirements *SHOULD* be generated
  587. using the automatic dependency generator.
  588. A package that provides a Python extra
  589. *MUST* require the extra’s main package with exact NEVR.
  590. A subpackage that primarily provides one Python extra *SHOULD* be named
  591. by appending `pass:[+]` and the extra name to the main package name.
  592. For example, `+python3-requests+security+`.
  593. The most straightforward way to provide an extra
  594. is with a dedicated subpackage containing no files (a “metapackage”).
  595. This case can be automated with
  596. the <<pyproject_extras_subpkg,`+%pyproject_extras_subpkg+` macro>>
  597. or the <<python_extras_subpkg,`+%python_extras_subpkg+` macro>>.
  598. This is not the only way:
  599. when some extra is always useful in a distro,
  600. it can be provided by the main package;
  601. when several extras are related,
  602. they may be provided by a single subpackage.
  603. However, having one dedicated subpackage per extra
  604. allows you to use the automatic dependency generator
  605. to ensure that the extras’ requirements will stay in sync with upstream.
  606. If you create a dedicated subpackage
  607. and want it to be always/usually installed,
  608. you can _Require_/_Recommend_/_Suggest_ it from the main package.
  609. The dependency generator for extras activates if the following holds:
  610. * The package name must end with `++EXTRA+`
  611. (where `+EXTRA+` is the extra name).
  612. * The package must contain the `+.dist-info+` directory, usually as `+%ghost+`.
  613. ===== Example and convenience macros
  614. The extra subpackage for `+setuptools_scm[toml]+` can be specified
  615. using the `+%pyproject_extras_subpkg+` convenience macro as follows.
  616. The macro takes the main package name and name(s) of the extra(s):
  617. [source,spec]
  618. ----
  619. %pyproject_extras_subpkg -n python3-setuptools_scm toml
  620. ----
  621. If not using `+%pyproject_install+`,
  622. you will instead need to use `+%python_extras_subpkg+`
  623. and pass a path to the `+dist-info+` directory:
  624. [source,spec]
  625. ----
  626. %python_extras_subpkg -n python3-setuptools_scm -i %{python3_sitelib}/*.dist-info toml
  627. ----
  628. For this case,
  629. the extras dependency generator will read upstream metadata
  630. from the `+.dist-info+` directory.
  631. If it finds that the extra requires on `+toml+`,
  632. it will generate `+Requires: python3.Xdist(toml)+`
  633. and `+Provides: python3dist(setuptools-scm[toml])+`
  634. (and the corresponding `+python3.Xdist+` provide).
  635. If you need additional features
  636. that the `+*_extras_subpkg+` macros do not cover,
  637. you will need to write the subpackage sections manually.
  638. Such features can be, for example:
  639. * Obsoleting/providing other names (e.g. obsoleted extras packages)
  640. * Manual strong or weak dependencies on other
  641. (possibly non-Python) packages
  642. As an example of what you need to write in these cases,
  643. both of the `+*_extras_subpkg+` macro invocations above
  644. expand to the following:
  645. [source,spec]
  646. ----
  647. %package -n python3-setuptools_scm+toml
  648. Summary: Metapackage for python3-setuptools_scm: toml extra
  649. Requires: python3-setuptools_scm = %{?epoch:%{epoch}:}%{version}-%{release}
  650. %description -n python3-setuptools_scm+toml
  651. This is a metapackage bringing in toml extra requires for python3-setuptools_scm.
  652. It contains no code, just makes sure the dependencies are installed.
  653. %files -n python3-setuptools_scm+toml
  654. %ghost %{python3_sitelib}/*.dist-info
  655. ----
  656. Note that the dependency generator
  657. does not add a dependency on the main package
  658. (the `+Requires: python3-setuptools_scm = ...+` above).
  659. If you are not using the `+%python_extras_subpkg+` macro,
  660. you need to add it manually.
  661. ==== Removing extras
  662. If an existing extra is removed from an upstream project,
  663. the Fedora maintainer *SHOULD* try to convince upstream to re-introduce it
  664. (with an empty list of dependencies).
  665. If that fails, the extra *SHOULD* be Obsoleted
  666. from either the main package or another extras subpackage.
  667. Note that removing extras is discouraged in
  668. https://setuptools.readthedocs.io/en/latest/userguide/dependency_management.html#optional-dependencies[setuptools documentation]
  669. (see the _Tip_ box near the end of the _Optional dependencies_ section).
  670. ==== Automatic Requires for extras
  671. The automatic <<Run-time dependency generator>>
  672. will generate Requires on `+python3.Xdist(DISTNAME[EXTRA])+`
  673. from upstream `+Requires-Dist+` metadata.
  674. If the required package does not yet provide metadata for the extra,
  675. contact the Fedora maintainer to add it.
  676. In an emergency, you can define
  677. the <<_python_no_extras_requires,`+%_python_no_extras_requires+` macro>>
  678. to avoid automatically generating _all_ extras requirements.
  679. == Interpreter invocation
  680. === Shebangs
  681. Shebang lines to invoke Python *MUST* use `+%{python3}+` as the interpreter.
  682. Shebang lines to invoke Python *SHOULD* be `+#!%{python3} -%{py3_shebang_flags}+`
  683. and they *MAY* include extra flags.
  684. If (some of) the default flags from
  685. <<py3_shebang_flags,the `+%{py3_shebang_flags}+` macro>> are not desirable,
  686. packages *SHOULD* explicitly redefine the macro to remove them by undefining
  687. the relevant `+%{_py3_shebang_...}+` macro.
  688. Using `+#!%{python3}+` (`+#!/usr/bin/python3+`)
  689. rather than e.g. `+#!/usr/bin/env python+`
  690. ensures that the system-wide Python interpreter is used to run the code,
  691. even if the user modifies `+$PATH+` (e.g. by activating a virtual environment).
  692. By default, `+-%{py3_shebang_flags}+` expands to `+-sP+`
  693. (or just `+-s+` on Python version lower than 3.11 and Fedora Linux older than 37).
  694. The `+-s+` flag,
  695. stored in <<_py3_shebang_s,the `+%{_py3_shebang_s}+` macro>>,
  696. means _don’t add user site directory to `+sys.path+`._
  697. That ensures the user’s Python packages
  698. (e.g. installed by `+pip install --user+`,
  699. or just placed in the current directory)
  700. don’t interfere with the RPM installed software.
  701. Sometimes, such content is desirable, such as with plugins.
  702. The `+-P+` flag,
  703. stored in <<_py3_shebang_P,the `+%{_py3_shebang_P}+` macro>>,
  704. means _don’t add the script's directory to `+sys.path+`._
  705. Sometimes, adding the script's directory to `+sys.path+` is desirable,
  706. such as with executable Python scripts installed in a custom directory,
  707. importing each other.
  708. Removing the undesired flag(s) from <<py3_shebang_flags,the `+%{py3_shebang_flags}+` macro>>
  709. rather than not using the macro at all,
  710. ensures that existing or future automation won’t add the flag.
  711. [source,spec]
  712. ----
  713. # Remove -s from Python shebang - ensure that extensions installed with pip
  714. # to user locations are seen and properly loaded
  715. %undefine _py3_shebang_s
  716. ----
  717. [source,spec]
  718. ----
  719. # Don't add -P to Python shebangs
  720. # The executable Python scripts in /usr/share/opt-viewer/ import each other
  721. %undefine _py3_shebang_P
  722. ----
  723. The <<pyproject_install,`+%pyproject_install+` macro>>
  724. automatically changes all Python shebangs
  725. in `+%{buildroot}%{_bindir}/*+` to use `+%{python3}+`
  726. and add contents of the <<py3_shebang_flags,`+%{py3_shebang_flags}+` macro>>
  727. to the existing flags.
  728. If you’re not using that macro
  729. or you need to change a shebang in a different directory,
  730. you can use <<py3_shebang_fix,the `+%py3_shebang_fix+` macro>> as follows:
  731. [source,spec]
  732. ----
  733. %py3_shebang_fix SCRIPTNAME …
  734. ----
  735. === Invokable Python modules
  736. Every executable `+TOOL+`
  737. for which the current version of Python matters
  738. *SHOULD* also be invokable by `+python3 -m TOOL+`.
  739. If the software doesn’t provide this functionality,
  740. packagers *SHOULD* ask the upstream to add it.
  741. This applies to tools that
  742. modify the current Python environment (like installing or querying packages),
  743. use Python for configuration,
  744. or use Python to run plugins.
  745. It does not apply to tools like GIMP or Bash
  746. which support plugins in multiple languages
  747. and/or have other means to specify the interpreter.
  748. For example, `+pip+` can be invoked as `+python3 -m pip+`.
  749. This allows users to accurately specify
  750. the Python version used to run the software.
  751. This convention works across different environments
  752. that might not always set `+$PATH+` or install scripts consistently.
  753. == Using Cython[[_packages_using_cython]]
  754. Tightening the
  755. xref:what-can-be-packaged.adoc#pregenerated-code[general Fedora policy],
  756. packages *MUST NOT* use files pre-generated by Cython.
  757. These *MUST* be deleted in `+%prep+` and regenerated during the build.
  758. As an exception, these sources *MAY* be used temporarily to prevent
  759. build time circular dependencies by following the
  760. xref:index.adoc#bootstrapping[bootstrapping guidelines].
  761. Generated files (the ones that must be deleted)
  762. have a generic `+.c+` or `+.cpp+` extension.
  763. Cython source files (which should stay)
  764. usually have the `+.pyx+` or `+.pxd+` extension.
  765. Cython is a popular tool for writing extension modules for Python.
  766. If compiles a Python-like language to C, which is then fed to the C compiler.
  767. Historically, Cython was hard to use upstream as a build-time dependency.
  768. Many projects include pre-generated C files in source
  769. distributions to avoid users from needing to install the tool.
  770. Cython uses CPython’s fast-changing internal API for performance reasons.
  771. For a new release of Python,
  772. Cython generally needs to be updated and the C files regenerated.
  773. In Fedora, this is frequently needed
  774. before upstreams release re-generated sources
  775. (e.g. for Alpha versins of Python).
  776. Since we do not have a problem with build-time dependencies,
  777. we always want to run the Cython step.
  778. For example, `+PyYAML+` removes a generated C file with:
  779. [source,spec]
  780. ----
  781. rm -rf ext/_yaml.c
  782. ----
  783. For another example, in `+python-lxml+` all C files are generated with Cython,
  784. which allows removing them with:
  785. [source,spec]
  786. ----
  787. # Remove pregenerated Cython C sources
  788. find -type f -name '*.c' -print -delete
  789. ----
  790. Some upstreams mix generated and hand-written C files.
  791. In such cases a grep like this one from `+scipy+` helps
  792. (but might not be entirely future proof):
  793. [source,spec]
  794. ----
  795. # Remove pregenerated Cython C sources
  796. rm $(grep -rl '/\* Generated by Cython')
  797. ----
  798. == Tests
  799. === Running tests
  800. If a test suite exists upstream,
  801. it *SHOULD* be run in the `+%check+` section.
  802. If that is not possible with reasonable effort,
  803. at least a basic smoke test (such as importing the packaged module)
  804. *MUST* be run in `+%check+`.
  805. You *MAY* exclude specific failing tests.
  806. You *MUST NOT* disable the entire testsuite
  807. or ignore its result to solve a build failure.
  808. As an exception,
  809. you *MAY* disable tests with an appropriate `+%if+` conditional
  810. (e.g. https://rpm-software-management.github.io/rpm/manual/conditionalbuilds.html[bcond])
  811. when xref:index.adoc#bootstrapping[bootstrapping].
  812. Most errors in Python happen at run-time,
  813. so tests are extremely important to root out issues,
  814. especially when mass rebuilds are required.
  815. Common reasons for skipping tests in `+%check+` include requiring
  816. network access,
  817. dependencies not packaged in Fedora,
  818. and/or specialized hardware or resources.
  819. In these cases,
  820. you can use <<pyproject_check_import,the `+%pyproject_check_import+`>>
  821. or <<py3_check_import,the `+%py3_check_import+` macro>>
  822. to test that installed modules are importable.
  823. ==== Tox
  824. A popular testing tool, and one which is well integrated in Fedora,
  825. is `+tox+`.
  826. Upstream, it is commonly used to test against multiple Python versions.
  827. In a Fedora package, BuildRequire test dependencies via `+%pyproject_buildrequires -t+` or `+-e+`
  828. (see _Test dependencies_ below)
  829. and run `+tox+` with:
  830. [source,spec]
  831. ----
  832. %tox
  833. ----
  834. This sets up the environment
  835. (`+$PATH+`, `+$PYTHONPATH+`, `+$TOX_TESTENV_PASSENV+`)
  836. and instructs `+tox+` to use the current environment rather than create new ones.
  837. For more options, see <<Build macros>>.
  838. ==== pytest
  839. When upstream doesn’t use `+tox+`,
  840. the tests need to be run directly
  841. depending on upstream choice of a test runner.
  842. A popular runner is `+pytest+`, which can be invoked using `+%pytest+`.
  843. Use positional arguments to specify the test directory.
  844. See `+python3 -m pytest --help+` for how to select tests.
  845. For example, if network-related tests are marked “network”,
  846. you might use `+-m+` to deselect them:
  847. [source,spec]
  848. ----
  849. %pytest -m "not network"
  850. ----
  851. The `+%pytest+` macro sets several environment variables
  852. appropriate for `+%check+`:
  853. * Locations in the buildroot are added to `+$PATH+` and `+$PYTHONPATH+`.
  854. * `+$PYTHONDONTWRITEBYTECODE+` is set to avoid
  855. writing pytest-specific cache files to buildroot
  856. * `+$PYTEST_XDIST_AUTO_NUM_WORKERS+` is set to `+%{_smp_build_ncpus}+`
  857. * If unset, `+$CFLAGS+` and `+$LDFLAGS+` are set to match the build flags
  858. ==== Other test runners
  859. If upstream doesn’t use `+tox+` or `+pytest+`,
  860. other test runners can be invoked with <<py3_test_envvars,the `+%{py3_test_envvars}+`>> macro,
  861. available since Fedora Linux 38.
  862. This macro sets several environment variables similarly to `+%pytest+`,
  863. but requires the actual test runner to be invoked after the macro, for example:
  864. [source,spec]
  865. ----
  866. %{py3_test_envvars} %{python3} -m unittest
  867. ----
  868. Or:
  869. [source,spec]
  870. ----
  871. %{py3_test_envvars} %{python3} tests/run_tests.py
  872. ----
  873. === Test dependencies
  874. One part of the Python packaging ecosystem that is still not standardized
  875. is specifying test dependencies (and development dependencies in general).
  876. A good, common way for upstreams to specify test dependencies
  877. is using an <<Extras,extra>> like `+[test]+`, `+[testing]+` or `+[dev]+`.
  878. In this case, upstream’s instructions to install test dependencies
  879. might look like `+$ pip install -e.[test]+`.
  880. Projects using `+tox+` usually specify test dependencies
  881. in a `+tox+`-specific format:
  882. a https://tox.readthedocs.io/en/latest/config.html#conf-requires[requires]
  883. key in the configuration.
  884. These two forms are handled by
  885. the <<pyproject_buildrequires,`+%pyproject_buildrequires+` macro>>.
  886. If upstream does not use either form,
  887. list test dependencies as manual _BuildRequires_ in the `+spec+` file,
  888. for example:
  889. [source,spec]
  890. ----
  891. # Test dependencies:
  892. BuildRequires: python3dist(pytest)
  893. ----
  894. If you need to do this,
  895. consider asking upstream to add a `+[test]+` extra.
  896. === Linters
  897. In `+%check+`, packages *SHOULD NOT* run “linters”:
  898. code style checkers,
  899. test coverage checkers
  900. and other tools that check code quality rather than functionality.
  901. Tools like `+black+`, `+pylint+`, `+flake8+`, or `+mypy+`
  902. are often “opinionated” and their “opinions” change frequently enough
  903. that they are nuisance in Fedora,
  904. where the linter is not pinned to an exact version.
  905. Furthermore, some of these tools take a long time
  906. to adapt to new Python versions,
  907. preventing early testing with Alpha and Beta releases of Python.
  908. And they are just not needed: wrongly formatted code is not important enough
  909. for the Fedora packager to bug the upstream about it.
  910. Making such an issue break a package build is entirely unreasonable.
  911. Linters _do_ make sense in upstream CI. But not in Fedora.
  912. If a linter is used, disable it and remove the dependency on it.
  913. If that is not easy, talk to upstream about making it easy
  914. (for example with a configuration option or a separate `+tox+` environment).
  915. For packages that contain such linters, use them at runtime or extend them,
  916. you will usually need to run the linter in `+%check+`.
  917. Run it to test functionality, not code quality of the packaged software.
  918. == Source files from PyPI
  919. Packages *MAY* use sources from PyPI.
  920. However, packages *SHOULD NOT* use an archive that omits test suites,
  921. licenses and/or documentation present in other source archives.
  922. For example, as of this writing `+pip+` provides a
  923. https://pypi.org/project/pip/#files[source tarball (“sdist”)]
  924. which omits the relatively large `+tests+` and `+docs+` directories
  925. present in https://github.com/pypa/pip[the source on GitHub].
  926. In this case, the tarball from GitHub should be used.
  927. (See the xref:SourceURL#_git_tags[Git tags] section
  928. of Fedora SourceURL guidelines.)
  929. When using sources from PyPI,
  930. you can use the <<pypi_source,the `+%pypi_source+` macro>>
  931. to generate the proper URL.
  932. [[_version_warning]]
  933. WARNING: Some Python packages use metadata from `git`
  934. (or a similar version control system)
  935. to construct their version string,
  936. for example via https://pypi.org/project/setuptools-scm/[setuptools_scm].
  937. When publishing a package to PyPI,
  938. this version metadata is usually stored and included in a file,
  939. so the version control history is no longer needed to construct it.
  940. However, when using tarballs from a git forge directly,
  941. this version information is missing
  942. and must be manually provided by the packager.
  943. For example, the `SETUPTOOLS_SCM_PRETEND_VERSION` environment variable can be set
  944. to the desired value in the `+%generate_buildrequires+` and `+%build+` scripts in the spec file
  945. for packages that use `setuptools_scm` for this purpose.
  946. == Example spec file[[_example_python_spec_file]]
  947. The following is a viable spec file
  948. for a Python library called `+Pello+`
  949. that follows packaging best practices.
  950. Note that the project name `+Pello+` <<Canonical project name,normalizes>>
  951. to the lowercase `+pello+`.
  952. The example spec shows where each variant is typically used.
  953. The project has an <<Extras,extra>> `+color+`,
  954. which enables colorized output when installed.
  955. Since the required dependency is quite minimal
  956. and color improves the user experience,
  957. the extra is Recommended from the main package.
  958. [source,spec]
  959. ----
  960. Name: python-pello
  961. Version: 1.0.4
  962. Release: 1%{?dist}
  963. Summary: Example Python library
  964. License: MIT-0
  965. URL: https://github.com/fedora-python/Pello
  966. Source: %{url}/archive/v%{version}/Pello-%{version}.tar.gz
  967. BuildArch: noarch
  968. BuildRequires: python3-devel
  969. %global _description %{expand:
  970. A python module which provides a convenient example.
  971. This description provides some details.}
  972. %description %_description
  973. %package -n python3-pello
  974. Summary: %{summary}
  975. Recommends: python3-pello+color
  976. %description -n python3-pello %_description
  977. %pyproject_extras_subpkg -n python3-pello color
  978. %prep
  979. %autosetup -p1 -n Pello-%{version}
  980. %generate_buildrequires
  981. %pyproject_buildrequires -t
  982. %build
  983. %pyproject_wheel
  984. %install
  985. %pyproject_install
  986. # Here, "pello" is the name of the importable module.
  987. %pyproject_save_files -l pello
  988. %check
  989. %tox
  990. # Note that there is no %%files section for
  991. # the unversioned python module, python-pello.
  992. # For python3-pello, %%{pyproject_files} handles code files and %%license,
  993. # but executables and documentation must be listed in the spec file:
  994. %files -n python3-pello -f %{pyproject_files}
  995. %doc README.md
  996. %{_bindir}/pello_greeting
  997. %changelog
  998. ----
  999. == Empty spec file
  1000. The following is an unfinished spec file template to copy, paste and edit.
  1001. [source,spec]
  1002. ----
  1003. Name: python-...
  1004. Version: ...
  1005. Release: 0%{?dist}
  1006. Summary: ...
  1007. License: ...
  1008. URL: https://...
  1009. Source: %{url}/archive/v%{version}/...-%{version}.tar.gz / %{pypi_source ...}
  1010. BuildArch: noarch / BuildRequires: gcc
  1011. BuildRequires: python3-devel
  1012. %global _description %{expand:
  1013. ...}
  1014. %description %_description
  1015. %package -n python3-...
  1016. Summary: %{summary}
  1017. %description -n python3-... %_description
  1018. %prep
  1019. %autosetup -p1 -n ...-%{version}
  1020. %generate_buildrequires
  1021. %pyproject_buildrequires -x... / -t
  1022. %build
  1023. %pyproject_wheel
  1024. %install
  1025. %pyproject_install
  1026. %pyproject_save_files ...
  1027. %check
  1028. %tox / %pytest / %pyproject_check_import ...
  1029. %files -n python3-... -f %{pyproject_files}
  1030. %doc README.*
  1031. %{_bindir}/...
  1032. %changelog
  1033. ----
  1034. == Macro Reference[[_macros]]
  1035. This section documents macros that are available
  1036. to help with Python packaging.
  1037. The expansions in parentheses are provided only as reference/examples.
  1038. See the <<Mandatory macros>> section above for:
  1039. * `+%{python3}+` (`+/usr/bin/python3+`)
  1040. * `+%{python3_version}+` (e.g. `+3.9+`)
  1041. * `+%{python3_version_nodots}+` (e.g. `+39+`)
  1042. * `+%{python3_sitelib}+` (e.g. `+/usr/lib/python3.9/site-packages+`)
  1043. * `+%{python3_sitearch}+` (e.g. `+/usr/lib64/python3.9/site-packages+`)
  1044. === Shebang macros
  1045. [#py3_shebang_flags]
  1046. * `+%{py3_shebang_flags}+` (`+sP+` or `+s+` before Fedora Linux 37)
  1047. +
  1048. Flags for `+%{python3}+` to use in shebangs.
  1049. See <<Shebangs>> for details.
  1050. Includes flags from several `+%{_py3_shebang_...}+` macros listed here.
  1051. [#_py3_shebang_s]
  1052. * `+%{_py3_shebang_s}+` (`+s+`)
  1053. +
  1054. Undefine this macro to drop `+s+` from `+%{py3_shebang_flags}+`.
  1055. [#_py3_shebang_P]
  1056. * `+%{_py3_shebang_P}+` (`+P+`)
  1057. +
  1058. Undefine this macro to drop `+P+` from `+%{py3_shebang_flags}+`.
  1059. Introduced in Fedora Linux 37.
  1060. [#py3_shebang_fix]
  1061. * `+%py3_shebang_fix PATHS+` (`+pathfix.py ... PATHS+`)
  1062. +
  1063. A macro to fix shebangs in specified `+PATHS+`.
  1064. Only shebangs that already have `+python+` in them are changed.
  1065. If a directory is given, all `+.py+` files in it are fixed, recursively.
  1066. (So, if you need to fix shebangs in files not named `+*.py+`,
  1067. you need to list each file separately or use a Shell glob,
  1068. such as `+%{buildroot}%{_libexecdir}/mytool/*+`.)
  1069. Existing flags are preserved and `+%{py3_shebang_flags}+` are added.
  1070. +
  1071. For example,
  1072. `+#! /usr/bin/env python+` will be changed to `+#! /usr/bin/python3 -s+`
  1073. and `+#! /usr/bin/python -u+` will be changed to `+#! /usr/bin/python3 -su+`.
  1074. +
  1075. This macro is called automatically
  1076. by `+%pyproject_install+` on `+%{buildroot}%{_bindir}/*+`.
  1077. === Convenience macros
  1078. [#pypi_source]
  1079. * `+%{pypi_source PROJECTNAME [VERSION [EXT]]}+`
  1080. (e.g. `+https://.../Django-3.0.5.tar.gz+`)
  1081. +
  1082. Evaluates to the appropriate URL for source archive hosted on PyPI.
  1083. Accepts the project name and up to two optional arguments:
  1084. +
  1085. --
  1086. ** The version of the PyPI project.
  1087. Defaults to `+%version+` (the package version) with any `+~+` removed.
  1088. ** The file extension to use. Defaults to `+tar.gz+`.
  1089. --
  1090. +
  1091. In most cases it is not necessary to specify those two arguments.
  1092. +
  1093. For backward compatibility, the first argument is technically optional
  1094. as well, but omitting it is deprecated. (It defaults to `+%srcname+` if
  1095. defined, or to `+%pypi_name+` if defined, or to `+%name+`.)
  1096. [#python3_platform]
  1097. * `+%{python3_platform}+` (e.g. `+linux-x86_64+`)
  1098. +
  1099. The platform name.
  1100. Used in some Python build systems.
  1101. This corresponds to
  1102. https://docs.python.org/3/library/sysconfig.html#sysconfig.get_platform[`+sysconfig.get_platform()+`].
  1103. [#python3_ext_suffix]
  1104. * `+%{python3_ext_suffix}+` (e.g. `+.cpython-39-x86_64-linux-gnu.so+`)
  1105. +
  1106. Filename extension for Python extension modules.
  1107. This corresponds to the `+EXT_SUFFIX+`
  1108. https://docs.python.org/3/library/sysconfig.html[sysconfig] variable.
  1109. [#python3_platform_triplet]
  1110. * `+%{python3_platform_triplet}+` (e.g. `+x86_64-linux-gnu+`)
  1111. +
  1112. A string identifying the architecture/platform.
  1113. This corresponds to the `+MULTIARCH+`
  1114. https://docs.python.org/3/library/sysconfig.html[sysconfig] variable.
  1115. [#python3_cache_tag]
  1116. * `+%{python3_cache_tag}+` (e.g. `+cpython-311+`)
  1117. +
  1118. Part of the bytecode cache filename that identifies the interpreter.
  1119. This corresponds to the
  1120. https://docs.python.org/3/library/sys.html#sys.implementation[`+sys.implementation.cache_tag+`]
  1121. value.
  1122. === Build macros
  1123. The “pyproject macros” are most useful
  1124. for packaging Python projects that use the `+pyproject.toml+` file
  1125. defined in https://www.python.org/dev/peps/pep-0518/[PEP 518]
  1126. and https://www.python.org/dev/peps/pep-0517/[PEP 517],
  1127. which specifies the package’s build dependencies
  1128. (including the build system, such as `+setuptools+`, `+flit+` or `+poetry+`).
  1129. If `+pyproject.toml+` is not found,
  1130. the macros automatically fall backs to using `+setuptools+`
  1131. with configuration in `+setup.cfg+`/`+setup.py+`.
  1132. A full tutorial and discussion for the macros is available in the macros’
  1133. https://src.fedoraproject.org/rpms/pyproject-rpm-macros/[README].
  1134. [#pyproject_buildrequires]
  1135. * `+%pyproject_buildrequires+`
  1136. +
  1137. Generate BuildRequires for the package.
  1138. Used in the `+%generate_buildrequires+` section of the `+spec+` file.
  1139. The macro has these options:
  1140. +
  1141. ** `+-R+`: Don't include run-time requirements (e.g. if the build backend does not support this).
  1142. ** `+-r+`: Include run-time requirements
  1143. (this flag is not needed and exists for backward-compatibility reasons only,
  1144. run-time requirements are included by default).
  1145. ** `+-x EXTRA+`: Include dependencies given by the given <<Extras,extra>>.
  1146. Cannot be used with `+-R+`.
  1147. ** `+-t+`: Include dependencies for the default _tox_ environment.
  1148. Cannot be used with `+-R+`.
  1149. ** `+-e ENV+`: Include dependencies for the given _tox_ environment,
  1150. and save the `+ENV+` name as `+%{toxenv}+`.
  1151. Cannot be used with `+-R+`.
  1152. Multiple comma separated values can be given, for example:
  1153. +
  1154. [source,spec]
  1155. ----
  1156. %pyproject_buildrequires -e %{toxenv}-unit,%{toxenv}-integration
  1157. ----
  1158. [#pyproject_wheel]
  1159. * `+%pyproject_wheel+`
  1160. +
  1161. Build the package.
  1162. Commonly, this is the only macro needed in the `+%build+` section.
  1163. +
  1164. This macro needs BuildRequires generated by `+%pyproject_buildrequires+`.
  1165. [#pyproject_install]
  1166. * `+%pyproject_install+`
  1167. +
  1168. Install the package built by `+%pyproject_wheel+`.
  1169. Calls `+%py3_shebang_fix %{_buildroot}%{_bindir}/*+`.
  1170. +
  1171. This macro needs BuildRequires generated by `+%pyproject_buildrequires+`.
  1172. [#pyproject_save_files]
  1173. * `+%pyproject_save_files MODNAME …+`
  1174. +
  1175. Generate a list of files corresponding to the given importable module(s)
  1176. and save it as `+%{pyproject_files}+`.
  1177. +
  1178. Note that README file is not included.
  1179. The LICENSE file is included when it is specified in the metadata.
  1180. Also, while the macro allows including executable and other files
  1181. (using the `+auto` flag),
  1182. this feature *MUST NOT* be used in Fedora.
  1183. +
  1184. The `+MODNAME+` may be a glob pattern,
  1185. which should be specific to your package.
  1186. To prevent Shell from expanding the globs, put them in `+''+`,
  1187. e.g. `+%pyproject_save_files '*pytest'+`.
  1188. As mentioned in the <<Explicit lists>> section,
  1189. expressions like `+%pyproject_save_files '*'+` are not acceptable.
  1190. +
  1191. The macro has these options:
  1192. +
  1193. ** `+-l+`: Declare that a missing license should terminate the build.
  1194. Packagers are encouraged to use this flag
  1195. when the `%license file` is not manually listed in `%files`
  1196. to avoid accidentally losing the file in a future version.
  1197. ** `+-L+`: Explicitly disable the check for a missing license file.
  1198. When the `%license` file is manually listed in `%files`,
  1199. packagers can use this flag to ensure future compatibility
  1200. in case the `-l` behavior eventually becomes a default.
  1201. [#pyproject_files]
  1202. * `+%{pyproject_files}+`
  1203. +
  1204. Path of the file written by `+%pyproject_save_files+`, to be used as:
  1205. +
  1206. [source,spec]
  1207. ----
  1208. %files -n python3-DISTNAME -f %{pyproject_files}
  1209. ----
  1210. === Test macros
  1211. [#tox]
  1212. * `+%tox+`
  1213. +
  1214. Run tests using `+tox+`.
  1215. +
  1216. This macro needs BuildRequires generated by the `+-t+` or `+-e+` option of
  1217. <<pyproject_buildrequires,the `+%pyproject_buildrequires+` macro>>.
  1218. +
  1219. Different environments may be specified with `+-e+`, for example:
  1220. +
  1221. [source,spec]
  1222. ----
  1223. %check
  1224. %tox %{?with_integration_tests:-e %{toxenv},%{toxenv}-integration}
  1225. ----
  1226. +
  1227. Flags for the `+tox+` command can be specified after `+--+`:
  1228. +
  1229. [source,spec]
  1230. ----
  1231. %tox -- --parallel 0
  1232. ----
  1233. +
  1234. Additional arguments for the test runner may be specified after another `+--+`:
  1235. +
  1236. [source,spec]
  1237. ----
  1238. %tox -- --parallel 0 -- --verbose tests/*
  1239. ----
  1240. [#toxenv]
  1241. * `+%{toxenv}+`
  1242. +
  1243. The _tox_ environment(s) used by the `+%tox+` macro.
  1244. Multiple environments are separated by commas.
  1245. Can be overridden manually or with `+%pyproject_buildrequires -t ENV1,ENV2+`.
  1246. [#default_toxenv]
  1247. * `+%{default_toxenv}+` (e.g. `+py39+`)
  1248. +
  1249. The system-wide default value of `+%{toxenv}+`.
  1250. [#pytest]
  1251. * `+%pytest+`
  1252. +
  1253. Run `+%__pytest+` with environment variables appropriate for tests in `%check`.
  1254. See <<Running tests>> for details.
  1255. [#__pytest]
  1256. * `+%__pytest+` (`+/usr/bin/pytest+`)
  1257. +
  1258. The command that `+%pytest+` uses. May be redefined.
  1259. [#py3_test_envvars]
  1260. * `+%py3_test_envvars+` (`+PATH=... PYTHONPATH=... PYTHONDONTWRITEBYTECODE=1 ...+`)
  1261. +
  1262. The environment variables used by `+%pytest+` and `+%tox+`.
  1263. It may be used to invoke custom test runners in `+%check+`.
  1264. See <<Other test runners>> for details.
  1265. Introduced in Fedora Linux 38.
  1266. [#py3_check_import]
  1267. * `+%py3_check_import+`
  1268. +
  1269. Imports all provided modules.
  1270. If running an upstream test suite is not feasible,
  1271. use this macro in `+%check+` to test that public Python modules are importable.
  1272. +
  1273. Takes these arguments:
  1274. +
  1275. --
  1276. ** `+-f+`: path to file containing qualified module names
  1277. (separated by newlines).
  1278. Optional, can be used multiple times.
  1279. ** `+-e+`: glob to exclude the matching module names.
  1280. Optional, can be used multiple times.
  1281. ** `+-t+`: if set, import only top-level module names
  1282. ** Positional arguments (separated by spaces or commas)
  1283. specify the module name(s) to check.
  1284. --
  1285. +
  1286. The macro sets various environment variables
  1287. such as `+PATH+` and `+PYTHONPATH+`
  1288. to ensure the packaged versions of modules are imported.
  1289. [#pyproject_check_import]
  1290. * `+%pyproject_check_import+`
  1291. +
  1292. Imports all public modules found by
  1293. <<pyproject_save_files,the `+%pyproject_save_files+` macro>> whose names match
  1294. any of the provided `+MODNAME+` globs.
  1295. +
  1296. This macro needs to be used with `+%pyproject_save_files+`
  1297. (use `+%py3_check_import+` in other cases).
  1298. +
  1299. The macro takes `+-e+`/`+-t+` as well as
  1300. positional arguments for `+%py3_check_import+` above.
  1301. === Extras macros
  1302. [#pyproject_extras_subpkg]
  1303. * `+%pyproject_extras_subpkg+`
  1304. +
  1305. Generates a simple subpackage for a Python extra.
  1306. See <<Extras>> for more information.
  1307. +
  1308. This macro needs to be used with `+%pyproject_install+`
  1309. (use `+%python_extras_subpkg+` in other cases).
  1310. +
  1311. Required arguments:
  1312. +
  1313. --
  1314. ** `+-n+`: name of the “base” package (e.g. `+python3-requests+`)
  1315. ** Positional arguments (separated by spaces or commas): the extra name(s).
  1316. Multiple metapackages are generated when multiple names are provided.
  1317. --
  1318. +
  1319. The macro also takes `+-i+`/`+-f+`/`+-F+` arguments
  1320. for `+%python_extras_subpkg+` below,
  1321. but if they are not given, a filelist written by `+%pyproject_install+` is used.
  1322. +
  1323. Similarly, the `+-a+`/`+-A+` flags are passed to `+%python_extras_subpkg+`.
  1324. +
  1325. This macro generates all the subpackage definition sections
  1326. (`+%package+` including the `+Summary+` and `+Requires+` on the base package,
  1327. `+%description+` and, by default, `+%files+`).
  1328. Hence, it cannot be extended with custom _Provides_/_Obsoletes_/_Requires_/etc.
  1329. This macro is designed to fit only the most common uses.
  1330. For more complicated uses, construct the subpackage manually
  1331. as shown in the <<Extras>> section.
  1332. +
  1333. The `+%files+` section is last.
  1334. It can be continued to add files that only make sense with the extra
  1335. and the base package does not fail without them.
  1336. For example, the following macro will
  1337. package the extra `+cli+` for the project `+a-cool-tool+`
  1338. and include an `+a-cool-tool+` command:
  1339. +
  1340. [source,spec]
  1341. ----
  1342. %pyproject_extras_subpkg -n a-cool-tool cli
  1343. %{_bindir}/a-cool-tool
  1344. ----
  1345. +
  1346. Due to technical limitations,
  1347. the macro never generates requirements on the arched
  1348. `+BASE_PACKAGE%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}+`.
  1349. It only adds
  1350. `+Requires: BASE_PACKAGE = %{?epoch:%{epoch}:}%{version}-%{release})+`
  1351. because a macro cannot reliably detect if the subpackage is arched or not.
  1352. So far, this has not been a problem in practice.
  1353. [#python_extras_subpkg]
  1354. * `+%python_extras_subpkg+`
  1355. +
  1356. Generates a simple subpackage for a Python extra.
  1357. See <<Extras>> for more information.
  1358. Takes these arguments:
  1359. +
  1360. --
  1361. ** `+-n+`: name of the “base” package (e.g. `+python3-requests+`)
  1362. ** `+-i+`: the `+%files %ghost+` path (glob) to the `+.dist-info+` directory
  1363. ** Positional arguments (separated by spaces or commas)
  1364. specify the extra name(s)
  1365. — multiple metapackages are generated when multiple names are provided.
  1366. ** `+-f+`: Relative path to the filelist for this metapackage
  1367. (which should contain the `+%files %ghost+` path (glob)
  1368. to the the metadata directory).
  1369. Conflicts with `+-i+` and `+-F+`.
  1370. ** `+-F+`: Skip the %files section entirely
  1371. (if the packager wants to construct it manually).
  1372. Conflicts with `+-i+` and `+-f+`.
  1373. ** `+-a+`: Include `+BuildArch: noarch+` in the package definition,
  1374. to be used only when the package is archful,
  1375. but the “base” package passed to `+-n+` is not.
  1376. ** `+-A+`: Explicitly disables `+-a+` (does nothing at the moment).
  1377. --
  1378. +
  1379. As with `+%pyproject_extras_subpkg+`:
  1380. +
  1381. ** This macro generates all the subpackage definition sections,
  1382. with only `+%files+` being customizable.
  1383. For more complicated uses, construct the subpackage manually
  1384. as shown in the <<Extras>> section.
  1385. ** It never generates requirements on the arched
  1386. `+BASE_PACKAGE%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}+`.
  1387. === Manual generation
  1388. The following macros are available for cases where
  1389. automatic generation is turned off.
  1390. They can also be useful for handling files in non-standard locations
  1391. where the generators don’t look.
  1392. [#pycached]
  1393. * `+%pycached MODNAME.py+`
  1394. +
  1395. Given a Python file, lists the file and the files with its bytecode
  1396. cache. See _Source files and bytecode cache_ for more information.
  1397. [#py_provides]
  1398. * `+%py_provides python3-MODNAME+`
  1399. +
  1400. Generates `+Provides+` for `+python3-MODNAME+`, `+python3.X-MODNAME+`
  1401. and `+python-MODNAME+`.
  1402. See <<Automatic-unversioned-provides>> for more details.
  1403. [#py_byte_compile]
  1404. * `+%py_byte_compile INTERPRETER PATH+`
  1405. +
  1406. Byte-compile a Python file into a `+__pycache__/*.pyc+`.
  1407. +
  1408. If the `+PATH+` argument is a directory,
  1409. the macro will recursively byte compile all `+*.py+` files in the directory.
  1410. (So, if you need to compile files not named `+*.py+`,
  1411. you need to use the macro on each file separately.)
  1412. +
  1413. The `+INTERPRETER+` determines the compiled file name’s suffix
  1414. and the magic number embedded in the file.
  1415. These muct match the interpreter that will import the file.
  1416. Usually, the `+INTERPRETER+` should be set to `+%{python3}+`.
  1417. If you are compiling for a non-default interpreter,
  1418. use that interpreter instead and add a `+BuildRequires+` line for it.
  1419. [#py_dist_name]
  1420. * `+%{py_dist_name PROJECTNAME}+`
  1421. +
  1422. Given a _project name_ (e.g. `+PyYAML+`) it will convert it
  1423. to the canonical format (e.g. `+pyyaml+`).
  1424. See <<Canonical project name>> for more information.
  1425. [#py3_dist]
  1426. * `+%{py3_dist PROJECTNAME …}+`
  1427. +
  1428. Given one or more _project names_,
  1429. it will convert them to the canonical format
  1430. and evaluate to `+python3dist(DISTNAME)+`,
  1431. which is useful when listing dependencies.
  1432. See <<Machine-readable-provides>> for more information.
  1433. === System Settings
  1434. The following macros can be redefined for special use cases.
  1435. [#__python]
  1436. * `+%{__python}+` (errors by default if not redefined)
  1437. +
  1438. Defining this macro sets the meaning of all “unversioned” Python macros
  1439. such as `+%{python}+` or `+%{python_sitelib}+`.
  1440. Don’t use these macros without redefining `+%{__python}+`.
  1441. [#__python3]
  1442. * `+%{__python3}+` (`+/usr/bin/python3+`)
  1443. +
  1444. The python 3 interpreter.
  1445. Redefining this macro changes all the `+%{python3...}+` macros,
  1446. e.g. `+%{python3}+` or `+%{python3_sitelib}+`.
  1447. [#python3_pkgversion]
  1448. * `+%{python3_pkgversion}+` (`+3+`)
  1449. +
  1450. Distro-wide Python version, i.e. the `+3+` in `+python3+`.
  1451. Projects that build on top of Fedora might define it to e.g. `+3.9+`
  1452. to try allowing multiple Python stacks installable in parallel.
  1453. Packages in Fedora *MAY* use it
  1454. (e.g. in package names: `+python%{python3_pkgversion}-requests+`),
  1455. but *MUST NOT* redefine it.
  1456. === Comparing Python versions
  1457. When comparing Python versions
  1458. (e.g. to ask: is `+%{python3_version}+` greater than 3.8?),
  1459. using naïve `+%if %{python3_version} > 3.8+`
  1460. or `+%if "%{python3_version}" > "3.8"+` is not possible,
  1461. because the comparison is performed alphabetically on strings.
  1462. Hence it is true that `+"3.10" < "3.8"+` (which is not desired).
  1463. It is possible to explicitly compare version literals by using the `+v+` prefix,
  1464. similar to the Python string prefixes:
  1465. [source,spec]
  1466. ----
  1467. %if v"0%{?python3_version}" > v"3.8"
  1468. ...
  1469. %endif
  1470. ----
  1471. [NOTE]
  1472. ====
  1473. As a workaround for compatibility with RPM releases before 4.16 (Fedora 33),
  1474. `+%{python3_version_nodots}+` can be compared as an integers:
  1475. [source,spec]
  1476. ----
  1477. %if %{python3_version_nodots} > 38
  1478. ...
  1479. %endif
  1480. ----
  1481. This will work with Python 3.10 (310 > 39),
  1482. but eventually break with Python 4.0 (40 < 310).
  1483. ====
  1484. === Disabling automation
  1485. The following macros can turn off Python-specific automation.
  1486. Consider contacting the Python SIG if you need to do this.
  1487. [#python_disable_dependency_generator]
  1488. * `+%{?python_disable_dependency_generator}+`
  1489. +
  1490. Disables the automatic dependency generator.
  1491. See <<Automatically-generated-dependencies>> for details.
  1492. [#__pythonname_provides]
  1493. * `+%undefine __pythonname_provides+`
  1494. +
  1495. Disables automatic generation of unversioned/versioned provides for package names,
  1496. e.g. `+python-FOO+` and `+python3.9-FOO+` for `+python3-foo+`.
  1497. See <<Automatic-unversioned-provides>> for more details.
  1498. [#__pythondist_provides]
  1499. * `+%undefine __pythondist_provides+`
  1500. +
  1501. Disables automatic generation of machine-readable Provides,
  1502. e.g. `+python3dist(foo)+`.
  1503. See <<Machine-readable-provides>> for more details.
  1504. [#_python_no_extras_requires]
  1505. * `+%global _python_no_extras_requires 1+`
  1506. +
  1507. If defined, <<Automatic Requires for extras>> will not be generated.
  1508. [#_python_dist_allow_version_zero]
  1509. * `+%global _python_dist_allow_version_zero 1+`
  1510. +
  1511. From Fedora Linux 38 on, it is no longer possible to build a Python package with version 0
  1512. to prevent <<_version_warning,an accidental loss of the actual version information>>.
  1513. If defined, the macro will allow to build such package.
  1514. === Deprecated Macros
  1515. The following macros are deprecated.
  1516. See the xref:Python_201x.adoc[201x-era Python Packaging guidelines]
  1517. for how some of them were used.
  1518. * [[py3_build]] `+%py3_build+`
  1519. * [[py3_build_wheel]] `+%py3_build_wheel+`
  1520. * [[py3_build_egg]] `+%py3_build_egg+`
  1521. * [[py3_install]] `+%py3_install+`
  1522. * [[py3_install_wheel]] `+%py3_install_wheel+`
  1523. * [[py3_install_egg]] `+%py3_install_egg+`
  1524. * [[py3dir]] `+%py3dir+`
  1525. * [[py3_other_build]] `+%py3_other_build+`
  1526. * [[py3_other_install]] `+%py3_other_install+`
  1527. * [[python_provide]] `+%python_provide+`
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...