blob: 97f0a49ca771e5905e0c02dec4997032f4bd5188 [file] [log] [blame]
Éric Araujoeb933de2011-09-09 19:03:41 +02001.. highlightlang:: 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
Georg Brandl5d941342016-02-26 19:37:12 +010033 https://docs.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
44 pkg_add -r python
45
46* OpenBSD users use::
47
Georg Brandl48310cd2009-01-03 21:18:54 +000048 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 +000049
Christian Heimes8b0facf2007-12-04 19:30:01 +000050 For example i386 users get the 2.5.1 version of Python using::
51
52 pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/i386/python-2.5.1p2.tgz
53
54
55On OpenSolaris
56--------------
57
Serhiy Storchaka6dff0202016-05-07 10:49:07 +030058You can get Python from `OpenCSW <https://www.opencsw.org/>`_. Various versions
Georg Brandl4ab57332014-10-28 22:52:49 +010059of Python are available and can be installed with e.g. ``pkgutil -i python27``.
Christian Heimes8b0facf2007-12-04 19:30:01 +000060
61
Larry Hastings3732ed22014-03-15 21:13:56 -070062.. _building-python-on-unix:
63
Christian Heimes8b0facf2007-12-04 19:30:01 +000064Building Python
65===============
66
67If you want to compile CPython yourself, first thing you should do is get the
Georg Brandl5d941342016-02-26 19:37:12 +010068`source <https://www.python.org/downloads/source/>`_. You can download either the
Éric Araujoeb933de2011-09-09 19:03:41 +020069latest release's source or just grab a fresh `clone
Georg Brandl525d3552014-10-29 10:26:56 +010070<https://docs.python.org/devguide/setup.html#getting-the-source-code>`_. (If you want
Éric Araujoeb933de2011-09-09 19:03:41 +020071to contribute patches, you will need a clone.)
Christian Heimes8b0facf2007-12-04 19:30:01 +000072
Éric Araujoeb933de2011-09-09 19:03:41 +020073The build process consists in the usual ::
Christian Heimes8b0facf2007-12-04 19:30:01 +000074
75 ./configure
76 make
77 make install
78
79invocations. Configuration options and caveats for specific Unix platforms are
Éric Araujoeb933de2011-09-09 19:03:41 +020080extensively documented in the :source:`README` file in the root of the Python
Christian Heimes8b0facf2007-12-04 19:30:01 +000081source tree.
82
83.. warning::
84
Éric Araujoeb933de2011-09-09 19:03:41 +020085 ``make install`` can overwrite or masquerade the :file:`python3` binary.
Christian Heimes8b0facf2007-12-04 19:30:01 +000086 ``make altinstall`` is therefore recommended instead of ``make install``
87 since it only installs :file:`{exec_prefix}/bin/python{version}`.
88
89
90Python-related paths and files
91==============================
Georg Brandl48310cd2009-01-03 21:18:54 +000092
Christian Heimes8b0facf2007-12-04 19:30:01 +000093These are subject to difference depending on local installation conventions;
94:envvar:`prefix` (``${prefix}``) and :envvar:`exec_prefix` (``${exec_prefix}``)
95are installation-dependent and should be interpreted as for GNU software; they
96may be the same.
97
98For example, on most Linux systems, the default for both is :file:`/usr`.
99
100+-----------------------------------------------+------------------------------------------+
101| File/directory | Meaning |
102+===============================================+==========================================+
Éric Araujoeb933de2011-09-09 19:03:41 +0200103| :file:`{exec_prefix}/bin/python3` | Recommended location of the interpreter. |
Christian Heimes8b0facf2007-12-04 19:30:01 +0000104+-----------------------------------------------+------------------------------------------+
105| :file:`{prefix}/lib/python{version}`, | Recommended locations of the directories |
106| :file:`{exec_prefix}/lib/python{version}` | containing the standard modules. |
107+-----------------------------------------------+------------------------------------------+
108| :file:`{prefix}/include/python{version}`, | Recommended locations of the directories |
109| :file:`{exec_prefix}/include/python{version}` | containing the include files needed for |
110| | developing Python extensions and |
111| | embedding the interpreter. |
112+-----------------------------------------------+------------------------------------------+
Georg Brandl48310cd2009-01-03 21:18:54 +0000113
Christian Heimes8b0facf2007-12-04 19:30:01 +0000114
115Miscellaneous
116=============
117
118To easily use Python scripts on Unix, you need to make them executable,
119e.g. with ::
120
121 $ chmod +x script
122
123and put an appropriate Shebang line at the top of the script. A good choice is
124usually ::
125
Éric Araujoeb933de2011-09-09 19:03:41 +0200126 #!/usr/bin/env python3
Christian Heimes8b0facf2007-12-04 19:30:01 +0000127
128which searches for the Python interpreter in the whole :envvar:`PATH`. However,
129some Unices may not have the :program:`env` command, so you may need to hardcode
Éric Araujoeb933de2011-09-09 19:03:41 +0200130``/usr/bin/python3`` as the interpreter path.
Christian Heimes8b0facf2007-12-04 19:30:01 +0000131
Ezio Melotti0639d5a2009-12-19 23:26:38 +0000132To use shell commands in your Python scripts, look at the :mod:`subprocess` module.
Christian Heimes8b0facf2007-12-04 19:30:01 +0000133
134
Senthil Kumaranb0d0e192017-01-25 01:47:49 -0800135Editors and IDEs
136================
Christian Heimes8b0facf2007-12-04 19:30:01 +0000137
Senthil Kumaranb0d0e192017-01-25 01:47:49 -0800138There are a number of IDEs that support Python programming language.
139Many editors and IDEs provide syntax highlighting, debugging tools, and PEP-8 checks.
Christian Heimes8b0facf2007-12-04 19:30:01 +0000140
Senthil Kumaranb0d0e192017-01-25 01:47:49 -0800141Please go to `Python Editors <https://wiki.python.org/moin/PythonEditors>`_ and
142`Integrated Development Environments <https://wiki.python.org/moin/IntegratedDevelopmentEnvironments>`_
143for a comprehensive list.