blob: 1d1fa8bd85d7ed7bf8b264b75ec9e217ef17497b [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: sh
Christian Heimes8b0facf2007-12-04 19:30:01 +00002
3.. _using-on-unix:
4
5********************************
6 Using Python on Unix platforms
7********************************
8
9.. sectionauthor:: Shriphani Palakodety
10
11
12Getting and installing the latest version of Python
13===================================================
14
15On Linux
16--------
17
18Python comes preinstalled on most Linux distributions, and is available as a
19package on all others. However there are certain features you might want to use
20that are not available on your distro's package. You can easily compile the
21latest version of Python from source.
Georg Brandl06788c92009-01-03 21:31:47 +000022
Christian Heimes380f7f22008-02-28 11:19:05 +000023In the event that Python doesn't come preinstalled and isn't in the repositories as
Christian Heimes8b0facf2007-12-04 19:30:01 +000024well, you can easily make packages for your own distro. Have a look at the
25following links:
26
27.. seealso::
28
Georg Brandl5d941342016-02-26 19:37:12 +010029 https://www.debian.org/doc/manuals/maint-guide/first.en.html
Christian Heimes8b0facf2007-12-04 19:30:01 +000030 for Debian users
Georg Brandl5d941342016-02-26 19:37:12 +010031 https://en.opensuse.org/Portal:Packaging
Christian Heimes8b0facf2007-12-04 19:30:01 +000032 for OpenSuse users
Sanyam Khurana338cd832018-01-20 05:55:37 +053033 https://docs-old.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch-creating-rpms.html
Christian Heimes8b0facf2007-12-04 19:30:01 +000034 for Fedora users
35 http://www.slackbook.org/html/package-management-making-packages.html
36 for Slackware users
37
38
39On FreeBSD and OpenBSD
40----------------------
41
42* FreeBSD users, to add the package use::
43
Eitan Adler3384d382018-02-13 18:44:01 -080044 pkg install python3
Christian Heimes8b0facf2007-12-04 19:30:01 +000045
Eitan Adler3384d382018-02-13 18:44:01 -080046* OpenBSD users, to add the package use::
47
48 pkg_add -r python
Christian Heimes8b0facf2007-12-04 19:30:01 +000049
Georg Brandl48310cd2009-01-03 21:18:54 +000050 pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/<insert your architecture here>/python-<version>.tgz
Georg Brandl06788c92009-01-03 21:31:47 +000051
Christian Heimes8b0facf2007-12-04 19:30:01 +000052 For example i386 users get the 2.5.1 version of Python using::
53
54 pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/i386/python-2.5.1p2.tgz
55
56
57On OpenSolaris
58--------------
59
Serhiy Storchaka6dff0202016-05-07 10:49:07 +030060You can get Python from `OpenCSW <https://www.opencsw.org/>`_. Various versions
Georg Brandl4ab57332014-10-28 22:52:49 +010061of Python are available and can be installed with e.g. ``pkgutil -i python27``.
Christian Heimes8b0facf2007-12-04 19:30:01 +000062
63
Larry Hastings3732ed22014-03-15 21:13:56 -070064.. _building-python-on-unix:
65
Christian Heimes8b0facf2007-12-04 19:30:01 +000066Building Python
67===============
68
69If you want to compile CPython yourself, first thing you should do is get the
Georg Brandl5d941342016-02-26 19:37:12 +010070`source <https://www.python.org/downloads/source/>`_. You can download either the
Éric Araujoeb933de2011-09-09 19:03:41 +020071latest release's source or just grab a fresh `clone
Lisa Hewus Fresh384899d2017-08-30 09:37:43 -070072<https://devguide.python.org/setup/#getting-the-source-code>`_. (If you want
Éric Araujoeb933de2011-09-09 19:03:41 +020073to contribute patches, you will need a clone.)
Christian Heimes8b0facf2007-12-04 19:30:01 +000074
Stéphane Wirtel9e95eb02018-10-25 17:32:30 +020075The build process consists of the usual commands::
Christian Heimes8b0facf2007-12-04 19:30:01 +000076
77 ./configure
78 make
79 make install
80
Victor Stinnera41782c2021-04-08 22:32:21 +020081:ref:`Configuration options <configure-options>` and caveats for specific Unix
82platforms are extensively documented in the :source:`README.rst` file in the
83root of the Python source tree.
Christian Heimes8b0facf2007-12-04 19:30:01 +000084
85.. warning::
86
Éric Araujoeb933de2011-09-09 19:03:41 +020087 ``make install`` can overwrite or masquerade the :file:`python3` binary.
Christian Heimes8b0facf2007-12-04 19:30:01 +000088 ``make altinstall`` is therefore recommended instead of ``make install``
89 since it only installs :file:`{exec_prefix}/bin/python{version}`.
90
91
92Python-related paths and files
93==============================
Georg Brandl48310cd2009-01-03 21:18:54 +000094
Christian Heimes8b0facf2007-12-04 19:30:01 +000095These are subject to difference depending on local installation conventions;
96:envvar:`prefix` (``${prefix}``) and :envvar:`exec_prefix` (``${exec_prefix}``)
97are installation-dependent and should be interpreted as for GNU software; they
98may be the same.
99
100For example, on most Linux systems, the default for both is :file:`/usr`.
101
102+-----------------------------------------------+------------------------------------------+
103| File/directory | Meaning |
104+===============================================+==========================================+
Éric Araujoeb933de2011-09-09 19:03:41 +0200105| :file:`{exec_prefix}/bin/python3` | Recommended location of the interpreter. |
Christian Heimes8b0facf2007-12-04 19:30:01 +0000106+-----------------------------------------------+------------------------------------------+
107| :file:`{prefix}/lib/python{version}`, | Recommended locations of the directories |
108| :file:`{exec_prefix}/lib/python{version}` | containing the standard modules. |
109+-----------------------------------------------+------------------------------------------+
110| :file:`{prefix}/include/python{version}`, | Recommended locations of the directories |
111| :file:`{exec_prefix}/include/python{version}` | containing the include files needed for |
112| | developing Python extensions and |
113| | embedding the interpreter. |
114+-----------------------------------------------+------------------------------------------+
Georg Brandl48310cd2009-01-03 21:18:54 +0000115
Christian Heimes8b0facf2007-12-04 19:30:01 +0000116
117Miscellaneous
118=============
119
120To easily use Python scripts on Unix, you need to make them executable,
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300121e.g. with
122
123.. code-block:: shell-session
Christian Heimes8b0facf2007-12-04 19:30:01 +0000124
125 $ chmod +x script
126
127and put an appropriate Shebang line at the top of the script. A good choice is
128usually ::
129
Éric Araujoeb933de2011-09-09 19:03:41 +0200130 #!/usr/bin/env python3
Christian Heimes8b0facf2007-12-04 19:30:01 +0000131
132which searches for the Python interpreter in the whole :envvar:`PATH`. However,
133some Unices may not have the :program:`env` command, so you may need to hardcode
Éric Araujoeb933de2011-09-09 19:03:41 +0200134``/usr/bin/python3`` as the interpreter path.
Christian Heimes8b0facf2007-12-04 19:30:01 +0000135
Ezio Melotti0639d5a2009-12-19 23:26:38 +0000136To use shell commands in your Python scripts, look at the :mod:`subprocess` module.
Christian Heimes32eba612021-03-19 10:29:25 +0100137
Christian Heimes39258d32021-04-17 11:36:35 +0200138.. _unix_custom_openssl:
Christian Heimes32eba612021-03-19 10:29:25 +0100139
140Custom OpenSSL
141==============
142
1431. To use your vendor's OpenSSL configuration and system trust store, locate
144 the directory with ``openssl.cnf`` file or symlink in ``/etc``. On most
145 distribution the file is either in ``/etc/ssl`` or ``/etc/pki/tls``. The
146 directory should also contain a ``cert.pem`` file and/or a ``certs``
147 directory.
148
149 .. code-block:: shell-session
150
151 $ find /etc/ -name openssl.cnf -printf "%h\n"
152 /etc/ssl
153
1542. Download, build, and install OpenSSL. Make sure you use ``install_sw`` and
155 not ``install``. The ``install_sw`` target does not override
156 ``openssl.cnf``.
157
158 .. code-block:: shell-session
159
160 $ curl -O https://www.openssl.org/source/openssl-VERSION.tar.gz
161 $ tar xzf openssl-VERSION
162 $ pushd openssl-VERSION
163 $ ./config \
164 --prefix=/usr/local/custom-openssl \
165 --openssldir=/etc/ssl
166 $ make -j1 depend
167 $ make -j8
168 $ make install_sw
169 $ popd
170
1713. Build Python with custom OpenSSL
Victor Stinner85918e42021-04-12 23:27:35 +0200172 (see the configure `--with-openssl` and `--with-openssl-rpath` options)
Christian Heimes32eba612021-03-19 10:29:25 +0100173
174 .. code-block:: shell-session
175
176 $ pushd python-3.x.x
177 $ ./configure -C \
178 --with-openssl=/usr/local/custom-openssl \
179 --with-openssl-rpath=auto \
180 --prefix=/usr/local/python-3.x.x
181 $ make -j8
182 $ make altinstall
183
184.. note::
185
186 Patch releases of OpenSSL have a backwards compatible ABI. You don't need
187 to recompile Python to update OpenSSL. It's sufficient to replace the
188 custom OpenSSL installation with a newer version.