Debian Codegraph @

Doc/using/unix.rst

raw
1 .. highlight:: sh
2
3 .. _using-on-unix:
4
5 ********************************
6 Using Python on Unix platforms
7 ********************************
8
9 .. sectionauthor:: Shriphani Palakodety
10
11
12 Getting and installing the latest version of Python
13 ===================================================
14
15 On Linux
16 --------
17
18 Python comes preinstalled on most Linux distributions, and is available as a
19 package on all others. However there are certain features you might want to use
20 that are not available on your distro's package. You can compile the
21 latest version of Python from source.
22
23 In the event that the latest version of Python doesn't come preinstalled and isn't
24 in the repositories as well, you can make packages for your own distro. Have a
25 look at the following links:
26
27 .. seealso::
28
29 https://www.debian.org/doc/manuals/maint-guide/first.en.html
30 for Debian users
31 https://en.opensuse.org/Portal:Packaging
32 for OpenSuse users
33 https://docs.fedoraproject.org/en-US/package-maintainers/Packaging_Tutorial_GNU_Hello/
34 for Fedora users
35 https://slackbook.org/html/package-management-making-packages.html
36 for Slackware users
37
38 .. _installing_idle_on_linux:
39
40 Installing IDLE
41 ~~~~~~~~~~~~~~~
42
43 In some cases, IDLE might not be included in your Python installation.
44
45 * For Debian and Ubuntu users::
46
47 sudo apt update
48 sudo apt install idle
49
50 * For Fedora, RHEL, and CentOS users::
51
52 sudo dnf install python3-idle
53
54 * For SUSE and OpenSUSE users::
55
56 sudo zypper install python3-idle
57
58 * For Alpine Linux users::
59
60 sudo apk add python3-idle
61
62
63
64 On FreeBSD and OpenBSD
65 ----------------------
66
67 * FreeBSD users, to add the package use::
68
69 pkg install python3
70
71 * OpenBSD users, to add the package use::
72
73 pkg_add -r python
74
75 pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/<insert your architecture here>/python-<version>.tgz
76
77 For example i386 users get the 2.5.1 version of Python using::
78
79 pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/i386/python-2.5.1p2.tgz
80
81
82 .. _building-python-on-unix:
83
84 Building Python
85 ===============
86
87 If you want to compile CPython yourself, first thing you should do is get the
88 `source <https://www.python.org/downloads/source/>`_. You can download either the
89 latest release's source or just grab a fresh `clone
90 <https://devguide.python.org/setup/#get-the-source-code>`_. (If you want
91 to contribute patches, you will need a clone.)
92
93 The build process consists of the usual commands::
94
95 ./configure
96 make
97 make install
98
99 :ref:`Configuration options <configure-options>` and caveats for specific Unix
100 platforms are extensively documented in the :source:`README.rst` file in the
101 root of the Python source tree.
102
103 .. warning::
104
105 ``make install`` can overwrite or masquerade the :file:`python3` binary.
106 ``make altinstall`` is therefore recommended instead of ``make install``
107 since it only installs :file:`{exec_prefix}/bin/python{version}`.
108
109
110 Python-related paths and files
111 ==============================
112
113 These are subject to difference depending on local installation conventions;
114 :option:`prefix <--prefix>` and :option:`exec_prefix <--exec-prefix>`
115 are installation-dependent and should be interpreted as for GNU software; they
116 may be the same.
117
118 For example, on most Linux systems, the default for both is :file:`/usr`.
119
120 +-----------------------------------------------+------------------------------------------+
121 | File/directory | Meaning |
122 +===============================================+==========================================+
123 | :file:`{exec_prefix}/bin/python3` | Recommended location of the interpreter. |
124 +-----------------------------------------------+------------------------------------------+
125 | :file:`{prefix}/lib/python{version}`, | Recommended locations of the directories |
126 | :file:`{exec_prefix}/lib/python{version}` | containing the standard modules. |
127 +-----------------------------------------------+------------------------------------------+
128 | :file:`{prefix}/include/python{version}`, | Recommended locations of the directories |
129 | :file:`{exec_prefix}/include/python{version}` | containing the include files needed for |
130 | | developing Python extensions and |
131 | | embedding the interpreter. |
132 +-----------------------------------------------+------------------------------------------+
133
134
135 Miscellaneous
136 =============
137
138 To easily use Python scripts on Unix, you need to make them executable,
139 e.g. with
140
141 .. code-block:: shell-session
142
143 $ chmod +x script
144
145 and put an appropriate Shebang line at the top of the script. A good choice is
146 usually ::
147
148 #!/usr/bin/env python3
149
150 which searches for the Python interpreter in the whole :envvar:`PATH`. However,
151 some Unices may not have the :program:`env` command, so you may need to hardcode
152 ``/usr/bin/python3`` as the interpreter path.
153
154 To use shell commands in your Python scripts, look at the :mod:`subprocess` module.
155
156 .. _unix_custom_openssl:
157
158 Custom OpenSSL
159 ==============
160
161 1. To use your vendor's OpenSSL configuration and system trust store, locate
162 the directory with ``openssl.cnf`` file or symlink in ``/etc``. On most
163 distribution the file is either in ``/etc/ssl`` or ``/etc/pki/tls``. The
164 directory should also contain a ``cert.pem`` file and/or a ``certs``
165 directory.
166
167 .. code-block:: shell-session
168
169 $ find /etc/ -name openssl.cnf -printf "%h\n"
170 /etc/ssl
171
172 2. Download, build, and install OpenSSL. Make sure you use ``install_sw`` and
173 not ``install``. The ``install_sw`` target does not override
174 ``openssl.cnf``.
175
176 .. code-block:: shell-session
177
178 $ curl -O https://www.openssl.org/source/openssl-VERSION.tar.gz
179 $ tar xzf openssl-VERSION
180 $ pushd openssl-VERSION
181 $ ./config \
182 --prefix=/usr/local/custom-openssl \
183 --libdir=lib \
184 --openssldir=/etc/ssl
185 $ make -j1 depend
186 $ make -j8
187 $ make install_sw
188 $ popd
189
190 3. Build Python with custom OpenSSL
191 (see the configure ``--with-openssl`` and ``--with-openssl-rpath`` options)
192
193 .. code-block:: shell-session
194
195 $ pushd python-3.x.x
196 $ ./configure -C \
197 --with-openssl=/usr/local/custom-openssl \
198 --with-openssl-rpath=auto \
199 --prefix=/usr/local/python-3.x.x
200 $ make -j8
201 $ make altinstall
202
203 .. note::
204
205 Patch releases of OpenSSL have a backwards compatible ABI. You don't need
206 to recompile Python to update OpenSSL. It's sufficient to replace the
207 custom OpenSSL installation with a newer version.