Skip to content

Commit f3691de

Browse files
author
Jon Wayne Parrott
authored
Add a new tutorial on installing packages using pipenv (pypa#369)
1 parent e10c01b commit f3691de

File tree

4 files changed

+235
-2
lines changed

4 files changed

+235
-2
lines changed

source/conf.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# ones.
3333
extensions = [
3434
'sphinx.ext.intersphinx',
35+
'sphinx.ext.todo',
3536
]
3637

3738
# Add any paths that contain templates here, relative to this directory.
@@ -355,3 +356,9 @@
355356
'pip': ('https://pip.pypa.io/en/latest/', None),
356357
'pypa': ('https://pypa.io/en/latest/', None),
357358
}
359+
360+
361+
# f this is True, todo and todolist produce output, else they produce nothing.
362+
# The default is False.
363+
364+
todo_include_todos = True

source/key_projects.rst

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ Dev irc:#pypa-dev
7676

7777
A tool for installing Python packages.
7878

79+
.. _Pipfile:
80+
81+
Pipfile
82+
=======
83+
84+
`Source <https://github.com/pypa/pipfile>`__
85+
86+
``Pipfile`` and its sister ``Pipfile.lock`` are a higher-level
87+
application-centric alternative to :ref:`pip`'s lower-level
88+
``requirements.txt`` file.
89+
7990

8091
Python Packaging User Guide
8192
===========================
@@ -158,8 +169,8 @@ Warehouse
158169
Dev irc:#pypa-dev
159170

160171

161-
The current codebase powering :term:`PyPI`. It is hosted at
162-
`pypi.org <https://pypi.org/>`_.
172+
The current codebase powering the :term:`Python Package Index (PyPI)`. It is
173+
hosted at `pypi.org <https://pypi.org/>`_.
163174

164175

165176
.. _wheel:
@@ -310,6 +321,22 @@ files, standalone Python environments in the spirit of :ref:`virtualenv`.
310321
``#!/usr/bin/env python`` and special ``__main__.py``, and are designed to make
311322
deployment of Python applications as simple as ``cp``.
312323

324+
325+
.. _Pipenv:
326+
327+
Pipenv
328+
======
329+
330+
`Docs <http://docs.pipenv.org/en/latest/>`__ |
331+
`Source <https://github.com/kennethreitz/pipenv>`__ |
332+
`Issues <https://github.com/kennethreitz/pipenv/issues>`__ |
333+
`PyPI <https://pypi.python.org/pypi/pipenv>`__
334+
335+
Pipenv is a project that aims to bring the best of all packaging worlds to the
336+
Python world. It harnesses :ref:`Pipfile`, :ref:`pip`, and :ref:`virtualenv`
337+
into one single toolchain. It features very pretty terminal colors.
338+
339+
313340
.. _spack:
314341

315342
Spack

source/new-tutorials/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ New Tutorials
66
.. warning:: This section is for work-in-progress tutorials! These will
77
eventually be promoted to the :doc:`/tutorials/index` section.
88
Do not link to these pages!
9+
10+
.. toctree::
11+
:maxdepth: 1
12+
13+
installing-and-using-packages
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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.
12+
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.
30+
31+
.. Note:: If you're a newcomer and you get an error like this:
32+
33+
.. code-block:: python
34+
35+
>>> python
36+
Traceback (most recent call last):
37+
File "<stdin>", line 1, in <module>
38+
NameError: name 'python' is not defined
39+
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.
45+
46+
Additionally, you'll need to make sure you have :ref:`pip` available. You can
47+
check this by running:
48+
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/
62+
63+
64+
Installing Pipenv
65+
-----------------
66+
67+
:ref:`Pipenv` is a dependency manager for Python projects. If you're familiar
68+
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.
72+
73+
Use ``pip`` to install Pipenv:
74+
75+
.. code-block:: python
76+
77+
pip install --user pipenv
78+
79+
80+
.. Note:: This does a `user installation`_ to prevent breaking any system-wide
81+
packages. If ``pipenv`` isn't available in your shell after installation,
82+
you'll need to add the `user base`_'s ``bin`` directory to your ``PATH``.
83+
You can find the user base by running ``python -m site`` which will print
84+
site information including the user base. For example, on Linux this will
85+
return ``USER_BASE: '~/.local'`` so you'll need to add ``~/.local/bin`` to
86+
your ``PATH``. On Linux and macOS you can set your ``PATH`` permanently
87+
by `modifying ~/.profile`_. On Windows you can set the user
88+
``PATH`` permanently in the `Control Panel`_.
89+
90+
.. _npm: https://www.npmjs.com/
91+
.. _bundler: http://bundler.io/
92+
.. _user base: https://docs.python.org/3/library/site.html#site.USER_BASE
93+
.. _user installation: https://pip.pypa.io/en/stable/user_guide/#user-installs
94+
.. _modifying ~/.profile: https://stackoverflow.com/a/14638025
95+
.. _Control Panel: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776899(v=vs.85).aspx
96+
97+
Installing packages for your project
98+
------------------------------------
99+
100+
Pipenv manages dependencies on a per-project basis. To install packages,
101+
change into your project's directory (or just an empty directory for this
102+
tutorial) and run:
103+
104+
.. code-block:: bash
105+
106+
cd myproject
107+
pipenv install requests
108+
109+
Pipenv will install the excellent `Requests`_ library and create a ``Pipfile``
110+
for you in your project's directory. The :ref:`Pipfile` is used to track which
111+
dependencies your project needs in case you need to re-install them, such as
112+
when you share your project with others. You should get output similar to this
113+
(although the exact paths shown will vary):
114+
115+
.. code-block:: text
116+
117+
Creating a Pipfile for this project...
118+
Creating a virtualenv for this project...
119+
Using base prefix '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6'
120+
New python executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python3.6
121+
Also creating executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python
122+
Installing setuptools, pip, wheel...done.
123+
124+
Virtualenv location: ~/.local/share/virtualenvs/tmp-agwWamBd
125+
Installing requests...
126+
Collecting requests
127+
Using cached requests-2.18.4-py2.py3-none-any.whl
128+
Collecting idna<2.7,>=2.5 (from requests)
129+
Using cached idna-2.6-py2.py3-none-any.whl
130+
Collecting urllib3<1.23,>=1.21.1 (from requests)
131+
Using cached urllib3-1.22-py2.py3-none-any.whl
132+
Collecting chardet<3.1.0,>=3.0.2 (from requests)
133+
Using cached chardet-3.0.4-py2.py3-none-any.whl
134+
Collecting certifi>=2017.4.17 (from requests)
135+
Using cached certifi-2017.7.27.1-py2.py3-none-any.whl
136+
Installing collected packages: idna, urllib3, chardet, certifi, requests
137+
Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
138+
139+
Adding requests to Pipfile's [packages]...
140+
P.S. You have excellent taste! ✨ 🍰 ✨
141+
142+
.. _Requests: https://python-requests.org
143+
144+
145+
Using installed packages
146+
------------------------
147+
148+
Now that Requests is installed you can create a simple ``main.py`` file to
149+
use it:
150+
151+
.. code-block:: python
152+
153+
import requests
154+
155+
response = requests.get('https://httpbin.org/ip')
156+
157+
print('Your IP is {0}'.format(response.json['origin']))
158+
159+
Then you can run this script using ``pipenv run``:
160+
161+
.. code-block:: bash
162+
163+
pipenv run python main.py
164+
165+
You should get output similar to this:
166+
167+
.. code-block:: text
168+
169+
Your IP is 8.8.8.8
170+
171+
Using ``pipenv run`` ensures that your installed packages are available to
172+
your script. It's also possible to spawn a new shell that ensures all commands
173+
have access to your installed packages with ``pipenv shell``.
174+
175+
176+
Next steps
177+
----------
178+
179+
Congratulations, you now know how to install and use Python packages! ✨ 🍰 ✨
180+
181+
There's more resources you can look at to learn about installing and using
182+
Python packages:
183+
184+
.. TODO:: Link to additional guides and resources.
185+
186+
If you find this approach isn't working well for you or your use case, you may
187+
want to explore these other approaches:
188+
189+
.. TODO:: Link to alternatives
190+
191+
If you're interesting in creating and distributing Python packages, see the
192+
tutorial on packaging and distributing packages.
193+
194+
.. TODO:: Link to packaging tutorial when it exists.

0 commit comments

Comments
 (0)