From 207ca45715e8cd447e426808593f51bdc5704786 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Wed, 29 Nov 2017 21:13:01 +1000 Subject: [PATCH] Introduce a new dependency management tutorial - 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) --- source/index.rst | 23 +++- source/new-tutorials/index.rst | 13 -- source/tutorials/distributing-packages.rst | 2 + source/tutorials/index.rst | 1 + source/tutorials/installing-packages.rst | 127 +++++++++++++----- .../managing-dependencies.rst} | 101 +++++--------- 6 files changed, 142 insertions(+), 125 deletions(-) delete mode 100644 source/new-tutorials/index.rst rename source/{new-tutorials/installing-and-using-packages.rst => tutorials/managing-dependencies.rst} (59%) diff --git a/source/index.rst b/source/index.rst index 770d5e7fbf..dcc25d373d 100644 --- a/source/index.rst +++ b/source/index.rst @@ -25,23 +25,32 @@ happily accept any :doc:`contributions and feedback `. 😊 .. _GitHub: https://github.com/pypa/python-packaging-user-guide .. _Python Packaging Authority: https://pypa.io -.. note:: Looking for guidance on migrating from legacy PyPI to :term:`pypi.org`? Please see :doc:`guides/migrating-to-pypi-org`. +.. note:: Looking for guidance on migrating from legacy PyPI to :term:`pypi.org`? + Please see :doc:`guides/migrating-to-pypi-org`. Get started =========== -Essential tools and concepts for working with the Python packaging ecosystem are covered in our :doc:`tutorials/index` section: +Essential tools and concepts for working with the Python packaging ecosystem are +covered in our :doc:`tutorials/index` section: -* to learn how to install packages, see the :doc:`tutorial on installing packages `. -* to learn how to package and distribute your projects, see the :doc:`tutorial on packaging and distributing ` +* to learn how to install packages, see the + :doc:`tutorial on installing packages `. +* to learn how to manage dependencies in a collaborative project, see the + :doc:`tutorial on managing application dependencies `. +* to learn how to package and distribute your projects, see the + :doc:`tutorial on packaging and distributing ` Learn more ========== Beyond our :doc:`tutorials/index`, this guide has several other resources: -* the :doc:`guides/index` section for walk throughs, such as :doc:`guides/installing-using-linux-tools` or :doc:`guides/packaging-binary-extensions` -* 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` +* the :doc:`guides/index` section for walk throughs, such as + :doc:`guides/installing-using-linux-tools` or :doc:`guides/packaging-binary-extensions` +* 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` * the :doc:`specifications/index` section for packaging interoperability specifications -Additionally, there is a list of :doc:`other projects ` maintained by members of the Python Packaging Authority. +Additionally, there is a list of :doc:`other projects ` maintained +by members of the Python Packaging Authority. diff --git a/source/new-tutorials/index.rst b/source/new-tutorials/index.rst deleted file mode 100644 index ced8d62fa4..0000000000 --- a/source/new-tutorials/index.rst +++ /dev/null @@ -1,13 +0,0 @@ -:orphan: - -New Tutorials -============= - -.. warning:: This section is for work-in-progress tutorials! These will - eventually be promoted to the :doc:`/tutorials/index` section. - Do not link to these pages! - -.. toctree:: - :maxdepth: 1 - - installing-and-using-packages diff --git a/source/tutorials/distributing-packages.rst b/source/tutorials/distributing-packages.rst index 58dd1b801b..8df5e72b42 100644 --- a/source/tutorials/distributing-packages.rst +++ b/source/tutorials/distributing-packages.rst @@ -1,3 +1,5 @@ +.. _distributing-packages: + =================================== Packaging and Distributing Projects =================================== diff --git a/source/tutorials/index.rst b/source/tutorials/index.rst index abe7b9c355..a4178567c3 100644 --- a/source/tutorials/index.rst +++ b/source/tutorials/index.rst @@ -9,4 +9,5 @@ topics, see :doc:`/guides/index`. :maxdepth: 1 installing-packages + managing-dependencies distributing-packages diff --git a/source/tutorials/installing-packages.rst b/source/tutorials/installing-packages.rst index d8d8a2b751..883533a2af 100644 --- a/source/tutorials/installing-packages.rst +++ b/source/tutorials/installing-packages.rst @@ -1,3 +1,5 @@ +.. _installing-packages: + =================== Installing Packages =================== @@ -27,32 +29,82 @@ Requirements for Installing Packages This section describes the steps to follow before installing other Python packages. -Install pip, setuptools, and wheel ----------------------------------- -* If you have Python 2 >=2.7.9 or Python 3 >=3.4 installed from `python.org - `_, you will already have :ref:`pip` and - :ref:`setuptools`, but will need to upgrade to the latest version: +Ensure you can run Python from the command line +----------------------------------------------- + +Before you go any further, make sure you have Python and that the expected +version is available from your command line. You can check this by running: + +.. code-block:: bash + + python --version + +You should get some output like ``Python 3.6.3``. If you do not have Python, +please install the latest 3.x version from `python.org`_ or refer to the +`Installing Python`_ section of the Hitchhiker's Guide to Python. + +.. Note:: If you're a newcomer and you get an error like this: + + .. code-block:: python + + >>> python --version + Traceback (most recent call last): + File "", line 1, in + NameError: name 'python' is not defined + + It's because this command and other suggested commands in this tutorial + are intended to be run in a *shell* (also called a *terminal* or + *console*). See the Python for Beginners `getting started tutorial`_ for + an introduction to using your operating system's shell and interacting with + Python. + + If you're using an enhanced shell like IPython or the Jupyter notebook, you + can run system commands like those in this tutorial by prefacing them with + a ``!`` character:: + + .. code-block:: python + + In [1]: !python --version + Python 3.6.3 - On Linux or macOS: +.. Note:: Due to the way most Linux distributions are handling the Python 3 + migration, Linux users using the system Python without creating a virtual + environment first should replace the ``python`` command in this tutorial + with ``python3`` and the ``pip`` command with ``pip3 --user``. Do *not* + run any of the commands in this tutorial with ``sudo``: if you get a + permissions error, come back to the section on creating virtual environments, + set one up, and then continue with the tutorial as written. - :: +.. _getting started tutorial: https://opentechschool.github.io/python-beginners/en/getting_started.html#what-is-python-exactly +.. _python.org: https://python.org - pip install -U pip setuptools +Ensure you can run pip from the command line +-------------------------------------------- +Additionally, you'll need to make sure you have :ref:`pip` available. You can +check this by running: - On Windows: +.. code-block:: bash - :: + pip --version - python -m pip install -U pip setuptools +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`. -* If you're using a Python install on Linux that's managed by the system package - manager (e.g "yum", "apt-get" etc...), and you want to use the system package - manager to install or upgrade pip, then see :ref:`Installing - pip/setuptools/wheel with Linux Package Managers` +.. _Homebrew: https://brew.sh +.. _Installing Python: http://docs.python-guide.org/en/latest/starting/installation/ -* Otherwise: +If ``pip`` isn't already installed, then first try to bootstrap it from the +standard library: + +.. code-block:: bash + + python -m ensurepip --default-pip + +If that still doesn't allow you to run ``pip``: * Securely Download `get-pip.py `_ [1]_ @@ -71,26 +123,29 @@ Install pip, setuptools, and wheel software. -Optionally, Create a virtual environment ----------------------------------------- +Ensure pip, setuptools, and wheel are up to date +------------------------------------------------ -See :ref:`section below ` for details, -but here's the basic commands: +While ``pip`` alone is sufficient to install from pre-built binary archives, +up to date copies of the ``setuptools`` and ``wheel`` projects are useful +to ensure you can also install from source archives:: - Using :ref:`virtualenv`: + python -m pip install --upgrade pip setuptools wheel - :: - pip install virtualenv - virtualenv - source /bin/activate +Optionally, create a virtual environment +---------------------------------------- - Using `venv`_: [3]_ +See :ref:`section below ` for details, +but here's the basic `venv`_ [3]_ command to use on a typical Linux system: + +.. code-block:: bash - :: + python3 -m venv tutorial_env + source tutorial_env/bin/activate - python3 -m venv - source /bin/activate +This will create a new virtual environment in the ``tutorial_env`` subdirectory, +and configure the current shell to use it as the default ``python`` environment. .. _`Creating and using Virtual Environments`: @@ -119,7 +174,7 @@ In all these cases, virtual environments can help you. They have their own installation directories and they don’t share libraries with other virtual environments. -Currently, there are two viable tools for creating Python virtual environments: +Currently, there are two common tools for creating Python virtual environments: * `venv`_ is available by default in Python 3.3 and later, and installs :ref:`pip` and :ref:`setuptools` into created virtual environments in @@ -150,6 +205,11 @@ Using `venv`_: For more information, see the `virtualenv `_ docs or the `venv`_ docs. +Managing multiple virtual environments directly can become tedious, so the +:ref:`dependency management tutorial ` introduces a +higher level tool, :ref:`Pipenv`, that automatically manages a separate +virtual environment for each project and application that you work on. + Use pip for Installing ====================== @@ -159,10 +219,6 @@ usage scenarios. For more detail, see the `pip docs `_, which includes a complete `Reference Guide `_. -There are a few cases where you might want to use `easy_install -`_ instead of pip. For -details, see :ref:`pip vs easy_install`. - Installing from PyPI ==================== @@ -250,6 +306,9 @@ For more information see the `User Installs `_ section from the pip docs. +Note that the ``--user`` flag has no effect when inside a virtual environment +- all installation commands will affect the virtual environment. + Requirements files ================== diff --git a/source/new-tutorials/installing-and-using-packages.rst b/source/tutorials/managing-dependencies.rst similarity index 59% rename from source/new-tutorials/installing-and-using-packages.rst rename to source/tutorials/managing-dependencies.rst index 2bd75b63c6..0d4ec82880 100644 --- a/source/new-tutorials/installing-and-using-packages.rst +++ b/source/tutorials/managing-dependencies.rst @@ -1,64 +1,28 @@ -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 Python is used for a great -many different purposes, and precisely how you want to manage your dependencies -may change based on how you decide to publish your software. The guidance -presented here is most directly applicable to the development and deployment of -network services (including web applications), but is also very well suited to -managing development and testing environments for any kind of project. +.. _managing-dependencies: -.. Note:: This guide is written for Python 3, however, these instructions - should work fine on Python 2.7. - - -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.2``. If you do not have Python, please -install the latest 3.x version from `python.org`_ or refer to the -`Installing Python`_ section of the Hitchhiker's Guide to Python. +Managing Application Dependencies +================================= -.. Note:: If you're a newcomer and you get an error like this: - - .. code-block:: python +The :ref:`package installation tutorial ` +covered the basics of getting set up to install and update Python packages. - >>> python - Traceback (most recent call last): - File "", line 1, in - NameError: name 'python' is not defined +However, running these commands interactively can get tedious even for your +own personal projects, and things get even more difficult when trying to set up +development environments automatically for projects with multiple contributors. - It's because this command and other suggested commands in this tutorial - are intended to be run in a *shell* (also called a *terminal* or - *console*). See the Python for Beginners `getting started tutorial`_ for - an introduction to using your operating system's shell and interacting with - Python. +This tutorial walks you through the use of :ref:`Pipenv` to manage dependencies +for an application. It will show you how to install and use the necessary tools +and make strong recommendations on best practices. -Additionally, you'll need to make sure you have :ref:`pip` available. You can -check this by running: +Keep in mind that Python is used for a great many different purposes, and +precisely how you want to manage your dependencies may change based on how you +decide to publish your software. The guidance presented here is most directly +applicable to the development and deployment of network services (including +web applications), but is also very well suited to managing development and +testing environments for any kind of project. -.. 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`. - -.. _getting started tutorial: https://opentechschool.github.io/python-beginners/en/getting_started.html#what-is-python-exactly -.. _python.org: https://python.org -.. _Homebrew: https://brew.sh -.. _Installing Python: http://docs.python-guide.org/en/latest/starting/installation/ +.. Note:: This guide is written for Python 3, however, these instructions + should also work on Python 2.7. Installing Pipenv @@ -66,9 +30,9 @@ 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. +tools. While :ref:`pip` alone is often sufficient for personal use, Pipenv is +recommended for collaborative projects as it's a higher-level tool that +simplifies dependency management for common use cases. Use ``pip`` to install Pipenv: @@ -187,19 +151,14 @@ have access to your installed packages with ``pipenv shell``. Next steps ---------- -Congratulations, you now know how to install and use Python packages! ✨ 🍰 ✨ - -There's more resources you can look at to learn about installing and using -Python packages: - -.. TODO:: Link to additional guides and resources. - -If you find this approach isn't working well for you or your use case, you may -want to explore these other approaches: +Congratulations, you now know how to effectively manage dependencies and +development environments on a collaborative Python project! ✨ 🍰 ✨ -.. TODO:: Link to alternatives +If you find this particular approach isn't working well for you or your use +case, you may want to explore these other approaches: -If you're interesting in creating and distributing Python packages, see the -tutorial on packaging and distributing packages. +* `pip-tools `_ +* `hatch `_ -.. TODO:: Link to packaging tutorial when it exists. +If you're interesting in creating and distributing your own Python packages, see +the :ref:`tutorial on packaging and distributing packages `.