| 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 |
.. seealso:: |
| 88 |
|
| 89 |
If you want to contribute to CPython, refer to the |
| 90 |
`devguide <https://devguide.python.org/getting-started/setup-building/>`_, |
| 91 |
which includes build instructions and other tips on setting up environment. |
| 92 |
|
| 93 |
If you want to compile CPython yourself, first thing you should do is get the |
| 94 |
`source <https://www.python.org/downloads/source/>`_. You can download either the |
| 95 |
latest release's source or grab a fresh `clone |
| 96 |
<https://devguide.python.org/setup/#get-the-source-code>`_. |
| 97 |
You will also need to install the :ref:`build requirements <build-requirements>`. |
| 98 |
|
| 99 |
The build process consists of the usual commands:: |
| 100 |
|
| 101 |
./configure |
| 102 |
make |
| 103 |
make install |
| 104 |
|
| 105 |
:ref:`Configuration options <configure-options>` and caveats for specific Unix |
| 106 |
platforms are extensively documented in the :source:`README.rst` file in the |
| 107 |
root of the Python source tree. |
| 108 |
|
| 109 |
.. warning:: |
| 110 |
|
| 111 |
``make install`` can overwrite or masquerade the :file:`python3` binary. |
| 112 |
``make altinstall`` is therefore recommended instead of ``make install`` |
| 113 |
since it only installs :file:`{exec_prefix}/bin/python{version}`. |
| 114 |
|
| 115 |
|
| 116 |
Python-related paths and files |
| 117 |
============================== |
| 118 |
|
| 119 |
These are subject to difference depending on local installation conventions; |
| 120 |
:option:`prefix <--prefix>` and :option:`exec_prefix <--exec-prefix>` |
| 121 |
are installation-dependent and should be interpreted as for GNU software; they |
| 122 |
may be the same. |
| 123 |
|
| 124 |
For example, on most Linux systems, the default for both is :file:`/usr`. |
| 125 |
|
| 126 |
+-----------------------------------------------+------------------------------------------+ |
| 127 |
| File/directory | Meaning | |
| 128 |
+===============================================+==========================================+ |
| 129 |
| :file:`{exec_prefix}/bin/python3` | Recommended location of the interpreter. | |
| 130 |
+-----------------------------------------------+------------------------------------------+ |
| 131 |
| :file:`{prefix}/lib/python{version}`, | Recommended locations of the directories | |
| 132 |
| :file:`{exec_prefix}/lib/python{version}` | containing the standard modules. | |
| 133 |
+-----------------------------------------------+------------------------------------------+ |
| 134 |
| :file:`{prefix}/include/python{version}`, | Recommended locations of the directories | |
| 135 |
| :file:`{exec_prefix}/include/python{version}` | containing the include files needed for | |
| 136 |
| | developing Python extensions and | |
| 137 |
| | embedding the interpreter. | |
| 138 |
+-----------------------------------------------+------------------------------------------+ |
| 139 |
|
| 140 |
|
| 141 |
Miscellaneous |
| 142 |
============= |
| 143 |
|
| 144 |
To easily use Python scripts on Unix, you need to make them executable, |
| 145 |
e.g. with |
| 146 |
|
| 147 |
.. code-block:: shell-session |
| 148 |
|
| 149 |
$ chmod +x script |
| 150 |
|
| 151 |
and put an appropriate Shebang line at the top of the script. A good choice is |
| 152 |
usually :: |
| 153 |
|
| 154 |
#!/usr/bin/env python3 |
| 155 |
|
| 156 |
which searches for the Python interpreter in the whole :envvar:`PATH`. However, |
| 157 |
some Unices may not have the :program:`env` command, so you may need to hardcode |
| 158 |
``/usr/bin/python3`` as the interpreter path. |
| 159 |
|
| 160 |
To use shell commands in your Python scripts, look at the :mod:`subprocess` module. |
| 161 |
|
| 162 |
.. _unix_custom_openssl: |
| 163 |
|
| 164 |
Custom OpenSSL |
| 165 |
============== |
| 166 |
|
| 167 |
1. To use your vendor's OpenSSL configuration and system trust store, locate |
| 168 |
the directory with ``openssl.cnf`` file or symlink in ``/etc``. On most |
| 169 |
distribution the file is either in ``/etc/ssl`` or ``/etc/pki/tls``. The |
| 170 |
directory should also contain a ``cert.pem`` file and/or a ``certs`` |
| 171 |
directory. |
| 172 |
|
| 173 |
.. code-block:: shell-session |
| 174 |
|
| 175 |
$ find /etc/ -name openssl.cnf -printf "%h\n" |
| 176 |
/etc/ssl |
| 177 |
|
| 178 |
2. Download, build, and install OpenSSL. Make sure you use ``install_sw`` and |
| 179 |
not ``install``. The ``install_sw`` target does not override |
| 180 |
``openssl.cnf``. |
| 181 |
|
| 182 |
.. code-block:: shell-session |
| 183 |
|
| 184 |
$ curl -O https://www.openssl.org/source/openssl-VERSION.tar.gz |
| 185 |
$ tar xzf openssl-VERSION |
| 186 |
$ pushd openssl-VERSION |
| 187 |
$ ./config \ |
| 188 |
--prefix=/usr/local/custom-openssl \ |
| 189 |
--libdir=lib \ |
| 190 |
--openssldir=/etc/ssl |
| 191 |
$ make -j1 depend |
| 192 |
$ make -j8 |
| 193 |
$ make install_sw |
| 194 |
$ popd |
| 195 |
|
| 196 |
3. Build Python with custom OpenSSL |
| 197 |
(see the configure ``--with-openssl`` and ``--with-openssl-rpath`` options) |
| 198 |
|
| 199 |
.. code-block:: shell-session |
| 200 |
|
| 201 |
$ pushd python-3.x.x |
| 202 |
$ ./configure -C \ |
| 203 |
--with-openssl=/usr/local/custom-openssl \ |
| 204 |
--with-openssl-rpath=auto \ |
| 205 |
--prefix=/usr/local/python-3.x.x |
| 206 |
$ make -j8 |
| 207 |
$ make altinstall |
| 208 |
|
| 209 |
.. note:: |
| 210 |
|
| 211 |
Patch releases of OpenSSL have a backwards compatible ABI. You don't need |
| 212 |
to recompile Python to update OpenSSL. It's sufficient to replace the |
| 213 |
custom OpenSSL installation with a newer version. |