-
Notifications
You must be signed in to change notification settings - Fork 932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a new tutorial on installing packages #369
Changes from 2 commits
f461ba7
e0f6ae6
9532e32
31ce3d5
8e4d638
b102d60
0bf3803
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
Installing and using packages | ||
============================= | ||
|
||
This tutorial walks you through installing and using Python packages. It will | ||
show you how to install and use the necessary tools and make strong | ||
recommendations on best practices. Keep in mind that the Python packaging | ||
ecosystem is a broad and varied one so this isn't the only way to install and | ||
use Python packages, however, it does cover the most common tools and use | ||
cases. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we may be able to provide better guidance on applicability here. Perhaps replace the second sentence with something like:
And then once we have a guide or discussion to link to, we can add a pointer to that with text like "If you find this approach isn't working well for you or your use case, you may want to explore these [other options](<link>)" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
.. Note:: This guide is written for Python 3, however, these instructions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This renders as two lines of unformatted text in the preview, is that a quirk of the preview or a bug (or intentional)? It looks like this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
should work fine on Python 2. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd qualify this as Python 2.7 (so we can ignore 2.6 compatibility) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
|
||
Make sure you've got Python & pip | ||
--------------------------------- | ||
|
||
Before you go any further, make sure you have Python and that it's avalable | ||
from your command line. You can check this by simply running: | ||
|
||
.. code-block:: bash | ||
|
||
python --version | ||
|
||
You should get some output like ``3.6.1``. If you do not have Python, please | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not 3.6.2? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's just example output (my mac has 3.6.1) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd just document it as 3.6.2, personally, even if I had 3.6.1 installed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
install the latest 3.x version from `python.org`_ or refer to the | ||
`Installing Python`_ section of the Hitchhiker's Guide to Python. | ||
|
||
Additionally, you'll need to make sure you have :ref:`pip` available. You can | ||
check this by running: | ||
|
||
.. code-block:: bash | ||
|
||
pip --version | ||
|
||
If you installed Python from source, with an installer from `python.org`_, or | ||
via `Homebrew`_ you should already have pip. If you're on Linux and installed | ||
using your OS package manager, you may have to install pip separately, see | ||
:doc:`/guides/installing-using-linux-tools`. | ||
|
||
.. _python.org: https://python.org | ||
.. _Homebrew: https://brew.sh | ||
.. _Installing Python: http://docs.python-guide.org/en/latest/starting/installation/ | ||
|
||
|
||
Installing Pipenv | ||
----------------- | ||
|
||
:ref:`Pipenv` is a dependency manager for Python projects. If you're familiar | ||
with Node.js' `npm`_ or Ruby's `bundler`_, it is similar in spirit to those | ||
tools. While :ref:`pip` can install Python packages, Pipenv is recommended as | ||
it's a higher-level tool that simplifies dependency management for common use | ||
cases. | ||
|
||
Use ``pip`` to install Pipenv: | ||
|
||
.. code-block:: python | ||
|
||
pip install --user pipenv | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
|
||
.. Note:: This does a `user installation`_ to prevent breaking any system-wide | ||
packages. If ``pipenv`` isn't available in your shell after installation, | ||
you'll need to add the `user base`_'s ``bin`` directory to your ``PATH``. | ||
You can find the user base by running ``import site; print(site.USER_BASE)`` | ||
in Python. For example, on Linux this will return ``~/.local`` so you'll | ||
need to add ``~/.local/bin`` to your ``PATH``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a note that this is typically done in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL! I switched to using that and added links to documentation on how to update your path. |
||
|
||
.. _npm: https://www.npmjs.com/ | ||
.. _bundler: http://bundler.io/ | ||
.. _user base: https://docs.python.org/3/library/site.html#site.USER_BASE | ||
.. _user installation: https://pip.pypa.io/en/stable/user_guide/#user-installs | ||
|
||
|
||
Installing packages for your project | ||
------------------------------------ | ||
|
||
Pipenv manages dependencies on a per-project basis. To install packages, | ||
change into your project's directory (or just an empty directory for this | ||
tutorial) and run: | ||
|
||
.. code-block:: bash | ||
|
||
cd myproject | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I can understand why you'd want to not have that, if you don't want Windows users to feel alienated. I don't think it harms them, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on user research from cloud.google.com we determined that users indeed do get tripped up on this (as they tend to copy/paste). There's a css trick to add the $ in the margin so that it's not picked up on copy, but that would need to be done separately from this PR. The blocks are all annotated as bash, so if we do decide to do it at a later date it'll be easy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha! |
||
pipenv install requests | ||
|
||
Pipenv will install the excellent `Requests`_ library and create a ``Pipfile`` | ||
for you in your project's directory. The :ref:`Pipfile` is used to track which | ||
dependencies your project needs in case you need to re-install them, such as | ||
when you share your project with others. You should get output similar to this: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add in parentheses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
.. code-block:: text | ||
|
||
Creating a Pipfile for this project... | ||
Creating a virtualenv for this project... | ||
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I def think 3.6.2 should be used for the docs, as best practice :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, just example output. It'll be difficult to keep this up-to-date with the latest patch version, but we can likely try to bump for every minor version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd just patch it by hand :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
New python executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python3.6 | ||
Also creating executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python | ||
Installing setuptools, pip, wheel...done. | ||
|
||
Virtualenv location: ~/.local/share/virtualenvs/tmp-agwWamBd | ||
Installing requests... | ||
Collecting requests | ||
Using cached requests-2.18.4-py2.py3-none-any.whl | ||
Collecting idna<2.7,>=2.5 (from requests) | ||
Using cached idna-2.6-py2.py3-none-any.whl | ||
Collecting urllib3<1.23,>=1.21.1 (from requests) | ||
Using cached urllib3-1.22-py2.py3-none-any.whl | ||
Collecting chardet<3.1.0,>=3.0.2 (from requests) | ||
Using cached chardet-3.0.4-py2.py3-none-any.whl | ||
Collecting certifi>=2017.4.17 (from requests) | ||
Using cached certifi-2017.7.27.1-py2.py3-none-any.whl | ||
Installing collected packages: idna, urllib3, chardet, certifi, requests | ||
Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22 | ||
|
||
Adding requests to Pipfile's [packages]... | ||
P.S. You have excellent taste! ✨ 🍰 ✨ | ||
|
||
.. _Requests: https://python-requests.org | ||
|
||
|
||
Using installed packages | ||
------------------------ | ||
|
||
Now that Requests is installed you can create a simple ``main.py`` file to | ||
use it: | ||
|
||
.. code-block:: python | ||
|
||
import requests | ||
|
||
response = requests.get('https://httpbin.org/ip') | ||
|
||
print('Your IP is {}'.format(response.json['origin'])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
Then you can run this script using ``pipenv run``: | ||
|
||
.. code-block:: bash | ||
|
||
pipenv run python main.py | ||
|
||
You should get output similar to this: | ||
|
||
.. code-block:: text | ||
|
||
Your IP is 8.8.8.8 | ||
|
||
Using ``pipenv run`` ensures that your installed packages are available to | ||
your script. It's also possible to spawn a new shell that ensures all commands | ||
have access to your installed packages with ``pipenv shell``. | ||
|
||
|
||
Next steps | ||
---------- | ||
|
||
Congratulations, you now know how to install and use Python packages! ✨ 🍰 ✨ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ;) |
||
|
||
There's more resources you can look at to learn about installing and using | ||
Python packages: | ||
|
||
.. TODO Link to additional guides and resources. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe make these TODOs visible in the rendered version so there aren't just blank spaces while the link targets are being worked on? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
If you're interesting in creating and distributing Python packages, see the | ||
tutorial on packaging and distributing packages. | ||
|
||
.. TODO Link to packaging tutorial when it exists. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than "... a replacement for the existing standard :ref:
pip
's ...", I'd word this as "... a higher level application-centric alternative to :ref:pip
's lower level ...".I think that's important, as Pipfile deliberately isn't a complete replacement for requirements.txt, and if you're doing things that don't quite fit the "develop and publish an integrated Python application" usage model, you're quite likely to end up wanting to drop back down to the less opinionated lower level interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just lifted this directly from PIpfile's repo, but changed it to match your wording.