diff --git a/source/guides/installing-using-pip-and-virtual-environments.rst b/source/guides/installing-using-pip-and-virtual-environments.rst index 287c059f55..2544aa40a6 100644 --- a/source/guides/installing-using-pip-and-virtual-environments.rst +++ b/source/guides/installing-using-pip-and-virtual-environments.rst @@ -1,203 +1,220 @@ -Installing packages using pip and virtual environments -====================================================== +Install packages in a virtual environment using pip and venv +============================================================ -This guide discusses how to install packages using :ref:`pip` and -a virtual environment manager: either :ref:`venv` for Python 3 or :ref:`virtualenv` -for Python 2. These are the lowest-level tools for managing Python -packages and are recommended if higher-level tools do not suit your needs. +This guide discusses how to create and activate a virtual environment using +the standard library's virtual environment tool :ref:`venv` and install packages. +The guide covers how to: -.. note:: This doc uses the term **package** to refer to a - :term:`Distribution Package` which is different from an :term:`Import - Package` that which is used to import modules in your Python source code. +* Create and activate a virtual environment +* Prepare pip +* Install packages into a virtual environment using the ``pip`` command +* Use and create a requirements file -Installing pip --------------- +.. note:: This guide applies to supported versions of Python, currently 3.8 + and higher. -:ref:`pip` is the reference Python package manager. It's used to install and -update packages. You'll need to make sure you have the latest version of pip -installed. +.. note:: This guide uses the term **package** to refer to a + :term:`Distribution Package`, which commonly is installed from an external + host. This differs from the term :term:`Import Package` which refers to + import modules in your Python source code. -.. tab:: Unix/macOS - Debian and most other distributions include a `python-pip`_ package; if you - want to use the Linux distribution-provided versions of pip, see - :doc:`/guides/installing-using-linux-tools`. +.. important:: + This guide has the prerequisite that you are using an official Python version obtained from + . If you are using your operating + system's package manager to install Python, please ensure that Python is + installed before proceeding with these steps. - You can also install pip yourself to ensure you have the latest version. It's - recommended to use the system pip to bootstrap a user installation of pip: - .. code-block:: bash +Create and Use Virtual Environments +----------------------------------- - python3 -m pip install --user --upgrade pip - python3 -m pip --version +Create a new virtual environment +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Afterwards, you should have the latest version of pip installed in your - user site: +:ref:`venv` (for Python 3) allows you to manage separate package installations for +different projects. It creates a "virtual" isolated Python installation. When +you switch projects, you can create a new virtual environment which is isolated +from other virtual environments. You benefit from the virtual environment +since packages can be installed confidently and will not interfere with +another project's environment. - .. code-block:: text +.. tip:: + It is recommended to use a virtual environment when working with third + party packages. - pip 21.1.3 from $HOME/.local/lib/python3.9/site-packages (python 3.9) - - .. _python-pip: https://packages.debian.org/stable/python/python3-pip - -.. tab:: Windows - - The Python installers for Windows include pip. You can make sure that pip is - up-to-date by running: +To create a virtual environment, go to your project's directory and run +``venv``. This will create a new virtual environment in a local folder ``.venv``: - .. code-block:: bat +.. tab:: Unix/macOS - py -m pip install --upgrade pip - py -m pip --version + .. code-block:: bash - Afterwards, you should have the latest version of pip: + python3 -m venv .venv - .. code-block:: text +.. tab:: Windows - pip 21.1.3 from c:\python39\lib\site-packages (Python 3.9.4) + .. code-block:: bat + py -m venv .venv +The second argument is the location to create the virtual environment. Generally, you +can just create this in your project and call it ``.venv``. -Installing virtualenv ---------------------- +``venv`` will create a virtual Python installation in the ``.venv`` folder. -.. Note:: If you are using Python 3.3 or newer, the :mod:`venv` module is - the preferred way to create and manage virtual environments. - venv is included in the Python standard library and requires no additional installation. - If you are using venv, you may skip this section. +.. Note:: You should exclude your virtual environment directory from your version + control system using ``.gitignore`` or similar. -:ref:`virtualenv` is used to manage Python packages for different projects. -Using virtualenv allows you to avoid installing Python packages globally -which could break system tools or other projects. You can install virtualenv -using pip. +Activate a virtual environment +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Before you can start installing or using packages in your virtual environment you'll +need to ``activate`` it. Activating a virtual environment will put the +virtual environment-specific ``python`` and ``pip`` executables into your +shell's ``PATH``. .. tab:: Unix/macOS .. code-block:: bash - python3 -m pip install --user virtualenv + source .venv/bin/activate .. tab:: Windows .. code-block:: bat - py -m pip install --user virtualenv + .venv\bin\Activate.bat + +To confirm the virtual environment is activated, check the location of your +Python interpreter: + +.. tab:: Unix/macOS + + .. code-block:: bash + which python +.. tab:: Windows -Creating a virtual environment ------------------------------- + .. code-block:: bat -:ref:`venv` (for Python 3) and :ref:`virtualenv` (for Python 2) allow -you to manage separate package installations for -different projects. They essentially allow you to create a "virtual" isolated -Python installation and install packages into that virtual installation. When -you switch projects, you can simply create a new virtual environment and not -have to worry about breaking the packages installed in the other environments. -It is always recommended to use a virtual environment while developing Python -applications. + where python -To create a virtual environment, go to your project's directory and run -venv. If you are using Python 2, replace ``venv`` with ``virtualenv`` -in the below commands. +When the virtual environment is activated, the location will include +the ``.venv`` directory: .. tab:: Unix/macOS .. code-block:: bash - python3 -m venv env + .../.venv/bin/python .. tab:: Windows .. code-block:: bat - py -m venv env + ...\.venv\bin\python.exe -The second argument is the location to create the virtual environment. Generally, you -can just create this in your project and call it ``env``. -venv will create a virtual Python installation in the ``env`` folder. +While a virtual environment is activated, pip will install packages into that +specific environment. This enables you to import and use packages in your +Python application. -.. Note:: You should exclude your virtual environment directory from your version - control system using ``.gitignore`` or similar. +Deactivate a virtual environment +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Activating a virtual environment --------------------------------- +If you want to switch projects or leave your virtual environment, +``deactivate`` the environment: -Before you can start installing or using packages in your virtual environment you'll -need to *activate* it. Activating a virtual environment will put the -virtual environment-specific -``python`` and ``pip`` executables into your shell's ``PATH``. +.. code-block:: bash -.. tab:: Unix/macOS + deactivate - .. code-block:: bash +.. note:: + Closing your shell will deactivate the virtual environment. If + you open a new shell window and want to use the virtual environment, + reactivate it. - source env/bin/activate +Reactivate a virtual environment +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. tab:: Windows +If you want to reactivate an existing virtual environment, follow the same +instructions about activating a virtual environment. There's no need to create +a new virtual environment. - .. code-block:: bat - .\env\Scripts\activate +Prepare pip +----------- -You can confirm you're in the virtual environment by checking the location of your -Python interpreter: +:ref:`pip` is the reference Python package manager. +It's used to install and update packages into a virtual environment. -.. tab:: Unix/macOS + +.. tab:: Unix + + You can make sure that pip is up-to-date by running: .. code-block:: bash - which python + python3 -m pip install --upgrade pip + python3 -m pip --version -.. tab:: Windows + Afterwards, you should have the latest version of pip installed in your + user site: - .. code-block:: bat + .. code-block:: text - where python + pip 23.3.1 from .../.venv/lib/python3.9/site-packages (python 3.9) -It should be in the ``env`` directory: +.. tab:: macOS -.. tab:: Unix/macOS + The Python installers for macOS include pip. You can make sure that pip is + up-to-date by running: .. code-block:: bash - .../env/bin/python + python3 -m pip install --upgrade pip + python3 -m pip --version -.. tab:: Windows + Afterwards, you should have the latest version of pip installed in your + user site: - .. code-block:: bat + .. code-block:: text - ...\env\Scripts\python.exe + pip 23.3.1 from .../.venv/lib/python3.9/site-packages (python 3.9) +.. tab:: Windows -As long as your virtual environment is activated pip will install packages into that -specific environment and you'll be able to import and use packages in your -Python application. + The Python installers for Windows include pip. You can make sure that pip is + up-to-date by running: + .. code-block:: bat + + py -m pip install --upgrade pip + py -m pip --version -Leaving the virtual environment -------------------------------- + Afterwards, you should have the latest version of pip: -If you want to switch projects or otherwise leave your virtual environment, simply run: + .. code-block:: text -.. code-block:: bash + pip 23.3.1 from .venv\lib\site-packages (Python 3.9.4) - deactivate -If you want to re-enter the virtual environment just follow the same instructions above -about activating a virtual environment. There's no need to re-create the virtual environment. +Install packages using pip +-------------------------- +When your virtual environment is activated, you can install packages. Use the +``pip install`` command to install packages. -Installing packages -------------------- +Install a package +~~~~~~~~~~~~~~~~~ -Now that you're in your virtual environment you can install packages. Let's install the +For example,let's install the `Requests`_ library from the :term:`Python Package Index (PyPI)`: .. tab:: Unix/macOS @@ -232,8 +249,8 @@ pip should download requests and all of its dependencies and install them: .. _Requests: https://pypi.org/project/requests/ -Installing specific versions ------------------------------ +Install a specific package version +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pip allows you to specify which version of a package to install using :term:`version specifiers `. For example, to install @@ -280,8 +297,8 @@ To install pre-release versions of packages, use the ``--pre`` flag: py -m pip install --pre requests -Installing extras ------------------ +Install extras +~~~~~~~~~~~~~~ Some packages have optional `extras`_. You can tell pip to install these by specifying the extra in brackets: @@ -302,10 +319,11 @@ specifying the extra in brackets: https://setuptools.readthedocs.io/en/latest/userguide/dependency_management.html#optional-dependencies -Installing from source ----------------------- +Install a package from source +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -pip can install a package directly from source, for example: +pip can install a package directly from its source code. For example, to install +the source code in the ``google-auth`` directory: .. tab:: Unix/macOS @@ -339,8 +357,8 @@ installed package without needing to re-install: py -m pip install --editable . -Installing from version control systems ---------------------------------------- +Install from version control systems +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pip can install packages directly from their version control system. For example, you can install directly from a git repository: @@ -353,8 +371,8 @@ For more information on supported version control systems and syntax, see pip's documentation on :ref:`VCS Support `. -Installing from local archives ------------------------------- +Install from local archives +~~~~~~~~~~~~~~~~~~~~~~~~~~~ If you have a local copy of a :term:`Distribution Package`'s archive (a zip, wheel, or tar file) you can install it directly with pip: @@ -392,8 +410,8 @@ connectivity or if you want to strictly control the origin of distribution packages. -Using other package indexes ---------------------------- +Install from other package indexes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If you want to download packages from a different index than the :term:`Python Package Index (PyPI)`, you can use the ``--index-url`` flag: @@ -444,8 +462,8 @@ install the latest version of ``requests`` and all of its dependencies: py -m pip install --upgrade requests -Using requirements files ------------------------- +Using a requirements file +------------------------- Instead of installing packages individually, pip allows you to declare all dependencies in a :ref:`Requirements File `. For @@ -504,5 +522,5 @@ Which will output a list of package specifiers such as: six==1.11.0 urllib3==1.22 -This is useful for creating :ref:`pip:Requirements Files` that can re-create -the exact versions of all packages installed in an environment. +The ``pip freeze`` command is useful for creating :ref:`pip:Requirements Files` +that can re-create the exact versions of all packages installed in an environment.