| 1 |
.. highlightlang:: 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 easily compile the |
| 21 |
latest version of Python from source. |
| 22 |
|
| 23 |
In the event that Python doesn't come preinstalled and isn't in the repositories as |
| 24 |
well, you can easily make packages for your own distro. Have a look at the |
| 25 |
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/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch-creating-rpms.html |
| 34 |
for Fedora users |
| 35 |
http://www.slackbook.org/html/package-management-making-packages.html |
| 36 |
for Slackware users |
| 37 |
|
| 38 |
|
| 39 |
On FreeBSD and OpenBSD |
| 40 |
---------------------- |
| 41 |
|
| 42 |
* FreeBSD users, to add the package use:: |
| 43 |
|
| 44 |
pkg install python3 |
| 45 |
|
| 46 |
* OpenBSD users, to add the package use:: |
| 47 |
|
| 48 |
pkg_add -r python |
| 49 |
|
| 50 |
pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/<insert your architecture here>/python-<version>.tgz |
| 51 |
|
| 52 |
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 |
|
| 57 |
On OpenSolaris |
| 58 |
-------------- |
| 59 |
|
| 60 |
You can get Python from `OpenCSW <https://www.opencsw.org/>`_. Various versions |
| 61 |
of Python are available and can be installed with e.g. ``pkgutil -i python27``. |
| 62 |
|
| 63 |
|
| 64 |
.. _building-python-on-unix: |
| 65 |
|
| 66 |
Building Python |
| 67 |
=============== |
| 68 |
|
| 69 |
If you want to compile CPython yourself, first thing you should do is get the |
| 70 |
`source <https://www.python.org/downloads/source/>`_. You can download either the |
| 71 |
latest release's source or just grab a fresh `clone |
| 72 |
<https://docs.python.org/devguide/setup.html#getting-the-source-code>`_. (If you want |
| 73 |
to contribute patches, you will need a clone.) |
| 74 |
|
| 75 |
The build process consists in the usual :: |
| 76 |
|
| 77 |
./configure |
| 78 |
make |
| 79 |
make install |
| 80 |
|
| 81 |
invocations. Configuration options and caveats for specific Unix platforms are |
| 82 |
extensively documented in the :source:`README` file in the root of the Python |
| 83 |
source tree. |
| 84 |
|
| 85 |
.. warning:: |
| 86 |
|
| 87 |
``make install`` can overwrite or masquerade the :file:`python` binary. |
| 88 |
``make altinstall`` is therefore recommended instead of ``make install`` |
| 89 |
since it only installs :file:`{exec_prefix}/bin/python{version}`. |
| 90 |
|
| 91 |
|
| 92 |
Python-related paths and files |
| 93 |
============================== |
| 94 |
|
| 95 |
These are subject to difference depending on local installation conventions; |
| 96 |
:envvar:`prefix` (``${prefix}``) and :envvar:`exec_prefix` (``${exec_prefix}``) |
| 97 |
are installation-dependent and should be interpreted as for GNU software; they |
| 98 |
may be the same. |
| 99 |
|
| 100 |
For example, on most Linux systems, the default for both is :file:`/usr`. |
| 101 |
|
| 102 |
+-----------------------------------------------+------------------------------------------+ |
| 103 |
| File/directory | Meaning | |
| 104 |
+===============================================+==========================================+ |
| 105 |
| :file:`{exec_prefix}/bin/python` | Recommended location of the interpreter. | |
| 106 |
+-----------------------------------------------+------------------------------------------+ |
| 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 |
+-----------------------------------------------+------------------------------------------+ |
| 115 |
| :file:`~/.pythonrc.py` | User-specific initialization file loaded | |
| 116 |
| | by the user module; not used by default | |
| 117 |
| | or by most applications. | |
| 118 |
+-----------------------------------------------+------------------------------------------+ |
| 119 |
|
| 120 |
|
| 121 |
Miscellaneous |
| 122 |
============= |
| 123 |
|
| 124 |
To easily use Python scripts on Unix, you need to make them executable, |
| 125 |
e.g. with :: |
| 126 |
|
| 127 |
$ chmod +x script |
| 128 |
|
| 129 |
and put an appropriate Shebang line at the top of the script. A good choice is |
| 130 |
usually :: |
| 131 |
|
| 132 |
#!/usr/bin/env python |
| 133 |
|
| 134 |
which searches for the Python interpreter in the whole :envvar:`PATH`. However, |
| 135 |
some Unices may not have the :program:`env` command, so you may need to hardcode |
| 136 |
``/usr/bin/python`` as the interpreter path. |
| 137 |
|
| 138 |
To use shell commands in your Python scripts, look at the :mod:`subprocess` module. |
| 139 |
|
| 140 |
|
| 141 |
Editors and IDEs |
| 142 |
================ |
| 143 |
|
| 144 |
There are a number of IDEs that support Python programming language. |
| 145 |
Many editors and IDEs provide syntax highlighting, debugging tools, and :pep:`8` checks. |
| 146 |
|
| 147 |
Please go to `Python Editors <https://wiki.python.org/moin/PythonEditors>`_ and |
| 148 |
`Integrated Development Environments <https://wiki.python.org/moin/IntegratedDevelopmentEnvironments>`_ |
| 149 |
for a comprehensive list. |