blob: 5da1f233466a02cf0b2a66631f49fcc5a599775a [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
Sandro Tosi1abde362011-12-31 18:46:50 +010029 http://www.debian.org/doc/manuals/maint-guide/first.en.html
Christian Heimes8b0facf2007-12-04 19:30:01 +000030 for Debian users
Ezio Melottiac3da872013-04-13 20:07:42 +030031 http://en.opensuse.org/Portal:Packaging
Christian Heimes8b0facf2007-12-04 19:30:01 +000032 for OpenSuse users
Sandro Tosi1abde362011-12-31 18:46:50 +010033 http://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
Georg Brandl4ab57332014-10-28 22:52:49 +010058You can get Python from `OpenCSW <http://www.opencsw.org/>`_. Various versions
59of 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 Brandle73778c2014-10-29 08:36:35 +010068`source <https://www.python.org/download/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
135Editors
136=======
137
138Vim and Emacs are excellent editors which support Python very well. For more
Ezio Melotti0639d5a2009-12-19 23:26:38 +0000139information on how to code in Python in these editors, look at:
Christian Heimes8b0facf2007-12-04 19:30:01 +0000140
Benjamin Peterson92035012008-12-27 16:00:54 +0000141* http://www.vim.org/scripts/script.php?script_id=790
142* http://sourceforge.net/projects/python-mode
Christian Heimes8b0facf2007-12-04 19:30:01 +0000143
144Geany is an excellent IDE with support for a lot of languages. For more
Sandro Tosi8b2c71a2012-08-12 11:02:03 +0200145information, read: http://www.geany.org/
Christian Heimes8b0facf2007-12-04 19:30:01 +0000146
147Komodo edit is another extremely good IDE. It also has support for a lot of
Georg Brandl525d3552014-10-29 10:26:56 +0100148languages. For more information, read http://komodoide.com/.