Skip to content

Commit 7e01f5a

Browse files
ncoghlanJon Wayne Parrott
authored andcommitted
Introduce a new dependency management tutorial (#402)
- this promotes the draft pipenv tutorial to the main guide as a *new* dependency management tutorial - several of the bootstrapping improvements have been incorporated into the initial package installation tutorial (that still needs more work, but it isn't the main focus of this update) - the main index page has been rewrapped in editing - the "new-tutorials" section has been removed completely for the time being (as I'm hoping that normal PR reviews will be sufficient for any further tutorial updates)
1 parent cdc3614 commit 7e01f5a

6 files changed

Lines changed: 142 additions & 125 deletions

File tree

‎source/index.rst‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,32 @@ happily accept any :doc:`contributions and feedback <contribute>`. 😊
2525
.. _GitHub: https://github.com/pypa/python-packaging-user-guide
2626
.. _Python Packaging Authority: https://pypa.io
2727

28-
.. note:: Looking for guidance on migrating from legacy PyPI to :term:`pypi.org`? Please see :doc:`guides/migrating-to-pypi-org`.
28+
.. note:: Looking for guidance on migrating from legacy PyPI to :term:`pypi.org`?
29+
Please see :doc:`guides/migrating-to-pypi-org`.
2930

3031
Get started
3132
===========
3233

33-
Essential tools and concepts for working with the Python packaging ecosystem are covered in our :doc:`tutorials/index` section:
34+
Essential tools and concepts for working with the Python packaging ecosystem are
35+
covered in our :doc:`tutorials/index` section:
3436

35-
* to learn how to install packages, see the :doc:`tutorial on installing packages <tutorials/installing-packages>`.
36-
* to learn how to package and distribute your projects, see the :doc:`tutorial on packaging and distributing <tutorials/distributing-packages>`
37+
* to learn how to install packages, see the
38+
:doc:`tutorial on installing packages <tutorials/installing-packages>`.
39+
* to learn how to manage dependencies in a collaborative project, see the
40+
:doc:`tutorial on managing application dependencies <tutorials/managing-dependencies>`.
41+
* to learn how to package and distribute your projects, see the
42+
:doc:`tutorial on packaging and distributing <tutorials/distributing-packages>`
3743

3844
Learn more
3945
==========
4046

4147
Beyond our :doc:`tutorials/index`, this guide has several other resources:
4248

43-
* the :doc:`guides/index` section for walk throughs, such as :doc:`guides/installing-using-linux-tools` or :doc:`guides/packaging-binary-extensions`
44-
* the :doc:`discussions/index` section for in-depth references on topics such as :doc:`discussions/deploying-python-applications` or :doc:`discussions/pip-vs-easy-install`
49+
* the :doc:`guides/index` section for walk throughs, such as
50+
:doc:`guides/installing-using-linux-tools` or :doc:`guides/packaging-binary-extensions`
51+
* the :doc:`discussions/index` section for in-depth references on topics such as
52+
:doc:`discussions/deploying-python-applications` or :doc:`discussions/pip-vs-easy-install`
4553
* the :doc:`specifications/index` section for packaging interoperability specifications
4654

47-
Additionally, there is a list of :doc:`other projects <key_projects>` maintained by members of the Python Packaging Authority.
55+
Additionally, there is a list of :doc:`other projects <key_projects>` maintained
56+
by members of the Python Packaging Authority.

‎source/new-tutorials/index.rst‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎source/tutorials/distributing-packages.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _distributing-packages:
2+
13
===================================
24
Packaging and Distributing Projects
35
===================================

‎source/tutorials/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ topics, see :doc:`/guides/index`.
99
:maxdepth: 1
1010

1111
installing-packages
12+
managing-dependencies
1213
distributing-packages

‎source/tutorials/installing-packages.rst‎

Lines changed: 93 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _installing-packages:
2+
13
===================
24
Installing Packages
35
===================
@@ -27,32 +29,82 @@ Requirements for Installing Packages
2729
This section describes the steps to follow before installing other Python
2830
packages.
2931

30-
Install pip, setuptools, and wheel
31-
----------------------------------
3232

33-
* If you have Python 2 >=2.7.9 or Python 3 >=3.4 installed from `python.org
34-
<https://www.python.org>`_, you will already have :ref:`pip` and
35-
:ref:`setuptools`, but will need to upgrade to the latest version:
33+
Ensure you can run Python from the command line
34+
-----------------------------------------------
35+
36+
Before you go any further, make sure you have Python and that the expected
37+
version is available from your command line. You can check this by running:
38+
39+
.. code-block:: bash
40+
41+
python --version
42+
43+
You should get some output like ``Python 3.6.3``. If you do not have Python,
44+
please install the latest 3.x version from `python.org`_ or refer to the
45+
`Installing Python`_ section of the Hitchhiker's Guide to Python.
46+
47+
.. Note:: If you're a newcomer and you get an error like this:
48+
49+
.. code-block:: python
50+
51+
>>> python --version
52+
Traceback (most recent call last):
53+
File "<stdin>", line 1, in <module>
54+
NameError: name 'python' is not defined
55+
56+
It's because this command and other suggested commands in this tutorial
57+
are intended to be run in a *shell* (also called a *terminal* or
58+
*console*). See the Python for Beginners `getting started tutorial`_ for
59+
an introduction to using your operating system's shell and interacting with
60+
Python.
61+
62+
If you're using an enhanced shell like IPython or the Jupyter notebook, you
63+
can run system commands like those in this tutorial by prefacing them with
64+
a ``!`` character::
65+
66+
.. code-block:: python
67+
68+
In [1]: !python --version
69+
Python 3.6.3
3670
37-
On Linux or macOS:
71+
.. Note:: Due to the way most Linux distributions are handling the Python 3
72+
migration, Linux users using the system Python without creating a virtual
73+
environment first should replace the ``python`` command in this tutorial
74+
with ``python3`` and the ``pip`` command with ``pip3 --user``. Do *not*
75+
run any of the commands in this tutorial with ``sudo``: if you get a
76+
permissions error, come back to the section on creating virtual environments,
77+
set one up, and then continue with the tutorial as written.
3878

39-
::
79+
.. _getting started tutorial: https://opentechschool.github.io/python-beginners/en/getting_started.html#what-is-python-exactly
80+
.. _python.org: https://python.org
4081

41-
pip install -U pip setuptools
82+
Ensure you can run pip from the command line
83+
--------------------------------------------
4284

85+
Additionally, you'll need to make sure you have :ref:`pip` available. You can
86+
check this by running:
4387

44-
On Windows:
88+
.. code-block:: bash
4589
46-
::
90+
pip --version
4791
48-
python -m pip install -U pip setuptools
92+
If you installed Python from source, with an installer from `python.org`_, or
93+
via `Homebrew`_ you should already have pip. If you're on Linux and installed
94+
using your OS package manager, you may have to install pip separately, see
95+
:doc:`/guides/installing-using-linux-tools`.
4996

50-
* If you're using a Python install on Linux that's managed by the system package
51-
manager (e.g "yum", "apt-get" etc...), and you want to use the system package
52-
manager to install or upgrade pip, then see :ref:`Installing
53-
pip/setuptools/wheel with Linux Package Managers`
97+
.. _Homebrew: https://brew.sh
98+
.. _Installing Python: http://docs.python-guide.org/en/latest/starting/installation/
5499

55-
* Otherwise:
100+
If ``pip`` isn't already installed, then first try to bootstrap it from the
101+
standard library:
102+
103+
.. code-block:: bash
104+
105+
python -m ensurepip --default-pip
106+
107+
If that still doesn't allow you to run ``pip``:
56108

57109
* Securely Download `get-pip.py
58110
<https://bootstrap.pypa.io/get-pip.py>`_ [1]_
@@ -71,26 +123,29 @@ Install pip, setuptools, and wheel
71123
software.
72124

73125

74-
Optionally, Create a virtual environment
75-
----------------------------------------
126+
Ensure pip, setuptools, and wheel are up to date
127+
------------------------------------------------
76128

77-
See :ref:`section below <Creating and using Virtual Environments>` for details,
78-
but here's the basic commands:
129+
While ``pip`` alone is sufficient to install from pre-built binary archives,
130+
up to date copies of the ``setuptools`` and ``wheel`` projects are useful
131+
to ensure you can also install from source archives::
79132

80-
Using :ref:`virtualenv`:
133+
python -m pip install --upgrade pip setuptools wheel
81134

82-
::
83135

84-
pip install virtualenv
85-
virtualenv <DIR>
86-
source <DIR>/bin/activate
136+
Optionally, create a virtual environment
137+
----------------------------------------
87138

88-
Using `venv`_: [3]_
139+
See :ref:`section below <Creating and using Virtual Environments>` for details,
140+
but here's the basic `venv`_ [3]_ command to use on a typical Linux system:
141+
142+
.. code-block:: bash
89143
90-
::
144+
python3 -m venv tutorial_env
145+
source tutorial_env/bin/activate
91146
92-
python3 -m venv <DIR>
93-
source <DIR>/bin/activate
147+
This will create a new virtual environment in the ``tutorial_env`` subdirectory,
148+
and configure the current shell to use it as the default ``python`` environment.
94149

95150

96151
.. _`Creating and using Virtual Environments`:
@@ -119,7 +174,7 @@ In all these cases, virtual environments can help you. They have their own
119174
installation directories and they don’t share libraries with other virtual
120175
environments.
121176

122-
Currently, there are two viable tools for creating Python virtual environments:
177+
Currently, there are two common tools for creating Python virtual environments:
123178

124179
* `venv`_ is available by default in Python 3.3 and later, and installs
125180
:ref:`pip` and :ref:`setuptools` into created virtual environments in
@@ -150,6 +205,11 @@ Using `venv`_:
150205
For more information, see the `virtualenv <http://virtualenv.pypa.io>`_ docs or
151206
the `venv`_ docs.
152207

208+
Managing multiple virtual environments directly can become tedious, so the
209+
:ref:`dependency management tutorial <managing-dependencies>` introduces a
210+
higher level tool, :ref:`Pipenv`, that automatically manages a separate
211+
virtual environment for each project and application that you work on.
212+
153213

154214
Use pip for Installing
155215
======================
@@ -159,10 +219,6 @@ usage scenarios. For more detail, see the `pip docs <https://pip.pypa.io>`_,
159219
which includes a complete `Reference Guide
160220
<https://pip.pypa.io/en/latest/reference/index.html>`_.
161221

162-
There are a few cases where you might want to use `easy_install
163-
<https://pip.pypa.io/en/latest/reference/index.html>`_ instead of pip. For
164-
details, see :ref:`pip vs easy_install`.
165-
166222

167223
Installing from PyPI
168224
====================
@@ -250,6 +306,9 @@ For more information see the `User Installs
250306
<https://pip.readthedocs.io/en/latest/user_guide.html#user-installs>`_ section
251307
from the pip docs.
252308

309+
Note that the ``--user`` flag has no effect when inside a virtual environment
310+
- all installation commands will affect the virtual environment.
311+
253312

254313
Requirements files
255314
==================

source/new-tutorials/installing-and-using-packages.rst renamed to source/tutorials/managing-dependencies.rst

Lines changed: 30 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,38 @@
1-
Installing and using packages
2-
=============================
3-
4-
This tutorial walks you through installing and using Python packages. It will
5-
show you how to install and use the necessary tools and make strong
6-
recommendations on best practices. Keep in mind that Python is used for a great
7-
many different purposes, and precisely how you want to manage your dependencies
8-
may change based on how you decide to publish your software. The guidance
9-
presented here is most directly applicable to the development and deployment of
10-
network services (including web applications), but is also very well suited to
11-
managing development and testing environments for any kind of project.
1+
.. _managing-dependencies:
122

13-
.. Note:: This guide is written for Python 3, however, these instructions
14-
should work fine on Python 2.7.
15-
16-
17-
Make sure you've got Python & pip
18-
---------------------------------
19-
20-
Before you go any further, make sure you have Python and that it's avalable
21-
from your command line. You can check this by simply running:
22-
23-
.. code-block:: bash
24-
25-
python --version
26-
27-
You should get some output like ``3.6.2``. If you do not have Python, please
28-
install the latest 3.x version from `python.org`_ or refer to the
29-
`Installing Python`_ section of the Hitchhiker's Guide to Python.
3+
Managing Application Dependencies
4+
=================================
305

31-
.. Note:: If you're a newcomer and you get an error like this:
32-
33-
.. code-block:: python
6+
The :ref:`package installation tutorial <installing-packages>`
7+
covered the basics of getting set up to install and update Python packages.
348

35-
>>> python
36-
Traceback (most recent call last):
37-
File "<stdin>", line 1, in <module>
38-
NameError: name 'python' is not defined
9+
However, running these commands interactively can get tedious even for your
10+
own personal projects, and things get even more difficult when trying to set up
11+
development environments automatically for projects with multiple contributors.
3912

40-
It's because this command and other suggested commands in this tutorial
41-
are intended to be run in a *shell* (also called a *terminal* or
42-
*console*). See the Python for Beginners `getting started tutorial`_ for
43-
an introduction to using your operating system's shell and interacting with
44-
Python.
13+
This tutorial walks you through the use of :ref:`Pipenv` to manage dependencies
14+
for an application. It will show you how to install and use the necessary tools
15+
and make strong recommendations on best practices.
4516

46-
Additionally, you'll need to make sure you have :ref:`pip` available. You can
47-
check this by running:
17+
Keep in mind that Python is used for a great many different purposes, and
18+
precisely how you want to manage your dependencies may change based on how you
19+
decide to publish your software. The guidance presented here is most directly
20+
applicable to the development and deployment of network services (including
21+
web applications), but is also very well suited to managing development and
22+
testing environments for any kind of project.
4823

49-
.. code-block:: bash
50-
51-
pip --version
52-
53-
If you installed Python from source, with an installer from `python.org`_, or
54-
via `Homebrew`_ you should already have pip. If you're on Linux and installed
55-
using your OS package manager, you may have to install pip separately, see
56-
:doc:`/guides/installing-using-linux-tools`.
57-
58-
.. _getting started tutorial: https://opentechschool.github.io/python-beginners/en/getting_started.html#what-is-python-exactly
59-
.. _python.org: https://python.org
60-
.. _Homebrew: https://brew.sh
61-
.. _Installing Python: http://docs.python-guide.org/en/latest/starting/installation/
24+
.. Note:: This guide is written for Python 3, however, these instructions
25+
should also work on Python 2.7.
6226

6327

6428
Installing Pipenv
6529
-----------------
6630

6731
:ref:`Pipenv` is a dependency manager for Python projects. If you're familiar
6832
with Node.js' `npm`_ or Ruby's `bundler`_, it is similar in spirit to those
69-
tools. While :ref:`pip` can install Python packages, Pipenv is recommended as
70-
it's a higher-level tool that simplifies dependency management for common use
71-
cases.
33+
tools. While :ref:`pip` alone is often sufficient for personal use, Pipenv is
34+
recommended for collaborative projects as it's a higher-level tool that
35+
simplifies dependency management for common use cases.
7236

7337
Use ``pip`` to install Pipenv:
7438

@@ -187,19 +151,14 @@ have access to your installed packages with ``pipenv shell``.
187151
Next steps
188152
----------
189153

190-
Congratulations, you now know how to install and use Python packages! ✨ 🍰 ✨
191-
192-
There's more resources you can look at to learn about installing and using
193-
Python packages:
194-
195-
.. TODO:: Link to additional guides and resources.
196-
197-
If you find this approach isn't working well for you or your use case, you may
198-
want to explore these other approaches:
154+
Congratulations, you now know how to effectively manage dependencies and
155+
development environments on a collaborative Python project! ✨ 🍰 ✨
199156

200-
.. TODO:: Link to alternatives
157+
If you find this particular approach isn't working well for you or your use
158+
case, you may want to explore these other approaches:
201159

202-
If you're interesting in creating and distributing Python packages, see the
203-
tutorial on packaging and distributing packages.
160+
* `pip-tools <https://github.com/jazzband/pip-tools>`_
161+
* `hatch <https://github.com/ofek/hatch>`_
204162

205-
.. TODO:: Link to packaging tutorial when it exists.
163+
If you're interesting in creating and distributing your own Python packages, see
164+
the :ref:`tutorial on packaging and distributing packages <distributing-packages>`.

0 commit comments

Comments
 (0)