Зачем использовать python -m pip
И снова здравствуйте. В преддверии старта нового потока по курсу «Machine Learning», хотим поделиться переводом статьи, которая имеет довольно косвенное отношение к ML, но наверняка будет полезна подписчикам нашего блога.
Мариатта — разработчик из Канады, спросила в Твиттере о python -m pip, попросив рассказать об этой идиоме и объяснить принцип ее работы.
Недавно я узнала, что нужно писать python -m pip вместо обычного pip install, но теперь я не могу вспомнить от кого я это услышала. Наверное, от @brettsky или @zooba. У кого-нибудь из вас есть пост в блоге, чтобы я могла поделиться им с читателями?
— Мариатта ( @mariatta ) 29 октября 2019 г. (https://twitter.com/mariatta/status/1189243515739561985?ref_src=twsrc%5Etfw)
Я не уверен, что именно я сказал Мариатте о python -m pip, но есть все шансы, что это был именно я, поскольку я же просил, чтобы эта инструкция для установки пакетов с помощью PyPI писалась именно так с 2016 года. Итак, эта статья должна пояснить, что такое python -m pip и почему вы должны использовать именно ее при запуске pip.
Что такое python -m pip?
Для начала, python -m pip выполняет pip с помощью той версии Python, которую вы указали для инструкции python. Таким образом, /usr/bin/python3.7 -m pip значит, что вы выполните pip для интерпретатора, расположенного в /usr/bin/python3.7 . Вы можете прочитать документацию про флаг -m , если вы не знаете, как он работает (кстати, он крайне полезный).
Зачем использовать python -m pip вместо pip/pip3?
Вы можете сказать: «Ладно, но почему я не могу просто воспользоваться pip, запустив команду pip?» Ответом будет: «Да, но контролировать вы ее будете меньше». Я объясню, что значит «контролировать меньше» на примере.
Предположим, у меня установлены две версии Python, например, Python 3.7 и 3.8 (это очень распространено среди людей, которые работают на Mac OS или Linux, не говоря уже о том, что вы возможно захотели поиграться с Python 3.8, и у вас уже стоял Python 3.7). Итак, если вы введете pip в терминале, для какого интерпретатора Python вы установите пакет?
Без более подробной информации ответа вы не узнаете. Сначала вам нужно будет понять, что лежит в PATH, то есть /usr/bin идет первым или же /usr/local/bin (которые являются самыми распространенными местами для установки Python, кстати обычно /usr/local/ идет первым). Итак, вы помните, где вы установили Python 3.7 и 3.8 и что это были разные каталоги, и вы будете знать, что пришло в PATH первым. Предположим, что вы установили оба вручную, возможно в вашей системе был уже предустановлен Python 3.7.3, и вы установили Python 3.7.5. В этом случае обе версии Python устанавливаются в /usr/local/bin . Можете ли вы сказать мне теперь, к чему теперь привязан pip?
Ответ вы не знаете. Если вы не знаете, когда устанавливали каждую версию, и понимаете, что последняя версия pip была записана в /usr/local/bin/pip , но вы не знаете, какой интерпретатор будет использоваться для команды pip. Теперь вы можете сказать: «Я всегда ставлю самые последние версии, так что это значит, что Python 3.8.0 будет установлен последним, поскольку он новее, чем, допустим, 3.7.5″. Хорошо, но что происходит, когда выходит Python 3.7.6? Ваш pip использовался бы уже не из Python 3.8, а из Python 3.7.
Когда вы используете python -m pip с конкретным интерпретатором python, который вам нужен, вся неопределенность исчезает. Если я пишу python3.8 -m pip, я точно знаю какой pip будет использован и что пакет будет установлен для Python 3.8 (то же самое было бы, если бы я указал python3.7).
Если вы пользуетесь Windows, то у вас есть дополнительный стимул использовать python -m pip, поскольку он позволяет pip обновлять себя. В основном, потому что pip.exe считается запущенным, когда вы пишете pip install —upgrade pip. В этот момент Windows не позволит вам переустановить pip.exe. Однако если вы делаете python-m pip install —upgrade pip, вы обходите эту проблему, поскольку запускается python.exe, а не pip.exe.
А что происходит, когда я нахожусь в активированной среде?
Обычно, когда я объясняю суть этой статьи людям, обязательно находится кто-то, кто скажет: «Я всегда использую виртуальную среду, и это ко мне не относится». Что ж, для начала хорошо бы ВСЕГДА использовать виртуальную среду! (Я расскажу, почему я так думаю, в одной из своих следующих статей!) Но если честно, то я бы все еще настаивал на использовании python -m pip, даже если, строго говоря, это не нужно.
Во-первых, если вы пользуетесь Windows, вам все равно захочется использовать python-m pip, чтобы вы в своей среде могли обновить pip.
Во-вторых, даже если вы используете другую операционную системы, я бы сказал, что все равно нужно пользоваться python-m pip, поскольку он будет работать независимо от ситуации. Он предупредит вас об ошибке, если вы забудете активировать среду, а любой человек, который за вами будет наблюдать, будет перенимать лучшие практики. И лично я не считаю, что экономия 10 нажатий на клавиатуру – весомая цена для неиспользования хорошей практики. А еще эта команда поможет вам предотвратить ошибки при написании сценариев автоматизации, которые будут выполнять заведомо некорректные операции, если вы забудете активировать среду.
Лично я, когда пользуюсь любым инструментом, работа которого зависит от того, каким интерпретатором он запускается, всегда пользуюсь -m , вне зависимости того, активирована виртуальная среда или нет. Мне всегда важно понимать, какой интерпретатор Python я использую.
ВСЕГДА пользуйтесь средой! Не ставьте все подряд в глобальный интерпретатор!
Когда мы говорим о том, как избежать путаницы при установке в Python, хочу подчеркнуть, что мы вообще не должны устанавливать ничего в глобальный интерпретатор Python, когда работаем локально (контейнеры – это совсем другое дело)! Если это предустановленный Python вашей системы, то в случае, если вы установите какую-то несовместимую версию библиотеки, на которую опирается ваша ОС, то фактически сломаете систему.
Но даже если вы установите отдельно для себя копию python, я все равно настоятельно не рекомендую ставить прямо в нее при локальной разработке. В конечном счете в своих проектах вы будете использовать различные пакеты, которые могут друг с другом конфликтовать, и у вас не будет четкого представления о зависимостях внутри ваших проектов. Гораздо лучше использовать среды, чтобы изолировать отдельные проекты и инструменты для них друг от друга. В сообществе Python используются два типа сред: виртуальные среды и conda среды. Существует даже специальный способ изолированной установки инструментов Python.
Если вам нужно установить инструмент
Для изолированной установки инструмента, я могу порекомендовать использовать pipx. Каждый инструмент получит свою собственную виртуальную среду, чтобы не конфликтовать с другими. Таким образом, если вы хотите иметь всего одну установку, к примеру, Black, вы можете работать, не сломав случайно свою единственную установку mypy.
Если вам нужна среда для проекта (и вы не пользуетесь conda)
Когда нужно создать среду для проекта, лично я всегда обращаюсь к venv и виртуальным средам. Она включена в stdlib Python, поэтому всегда доступна с помощью python-m venv (если, конечно, вы не используете Debian или Ubuntu, в этом случае вам может потребоваться установить пакет python3-venv apt). Немножко истории: Я фактически удалил старую команду pyvenv, которую Python устанавливал для создания виртуальных сред с помощью venv, по тем же причинам, почему нужно пользоваться python -m pip вместо pip. То есть непонятно для какого интерпретатора вы создали виртуальную среду при помощи старой команды pyvenv. И помните о том, что вам не нужно активировать среду, чтобы использовать интерпретатор содержащийся в ней, ведь .venv/bin/python работает так же хорошо, как активация среды и ввод команды python.
Сегодня некоторые разработчики по-прежнему отдают предпочтение virtualenv, поскольку она доступна на Python 2 и в ней есть некоторые дополнительные функции. Лично меня мало интересуют дополнительные функции, и наличие интегрированной venv означает, что мне не нужно использовать pipx для установки virtualenv на каждой машине. Но если venv не отвечает вашим потребностям, и вы хотите виртуальную среду, то посмотрите, предлагает ли virtualenv то, что вам нужно.
Если вы используете conda
Если вы используете conda, то можете использовать среды conda для получения того же эффекта, который могут предложить виртуальные среды, предоставляемые venv. Я не собираюсь вдаваться в то, нужно ли вам использовать conda или venv в вашей конкретной ситуации, но если вы используете conda, то знаете, что вы можете (и должны) создавать среды conda для своей работы, вместо того чтобы устанавливать все подряд в свою системную установку. Так вы сможете получить четкое понимание того, какие зависимости есть у вашего проекта (и это хорошая причина, чтобы использовать miniconda вместо полноценной anaconda, поскольку в первой меньше десятой части объема последней).
Всегда есть контейнеры
Работать в контейнере – это способ не разбираться со средой вообще, так как вся ваша «машина» станет отдельной средой. До тех пор, пока вы не установили Python в систему контейнера, вы должны спокойно иметь возможность сделать глобальную установку, чтобы ваш контейнер оставался простым и понятным.
Повторюсь, чтобы вы действительно поняли суть…
Не устанавливайте ничего в свой глобальный интерпретатор Python! Всегда старайтесь использовать среду для локальной разработки!
Я уже не могу сказать, сколько раз мне приходилось помогать кому-то, кто думал, что pip устанавливал в один интерпретатор Python, а на самом деле устанавливал в другой. И это неизмеримое количество также относится к тем моментам, когда люди ломали всю систему или задавались вопросом, почему они не смогли установить что-то, что противоречило какой-то другой вещи, которую они поставили ранее для другого проекта и т.д. из-за того, что они не потрудились настроить среду на своей локальной машине.
Поэтому, чтобы и вы и я могли спать спокойно, используйте python-m pip и старайтесь всегда использовать среду.
- Блог компании OTUS
- Python
- Программирование
Pip для Python — что это?

В этой статье пойдет разговор о pip для Python — что это, зачем применяется, как устанавливается, какие нюансы работы надо знать. Материал предназначен для начинающих разработчиков.
Pip (пип) — система управления пакетами, установка которой необходима для управления программными пакетами на Python. Но прежде, чем устанавливать pip на Python, давайте поговорим о пакете — что это, чем он отличается от модуля, как с ним работать.
Применительно к Python у термина существуют два значения: 1) Python-пакеты — это Py-дополнения, приложения или утилиты, которые можно устанавливать из внешнего репозитория: Bitbucket, Github, Google Code либо официального Python Package Index (PyPI). Речь идет о пакетах, находящихся в центральном репозитории PyPI («пипей»). Они хранятся на сервере в архивированном виде (.zip и .tar), а также в дополнительной упаковке .egg (старый формат) либо .whl. Сценарий установки setup.py присутствует в составе пакета, хранящего сведения о зависимостях — прочих модулях, без которых пакет функционировать не будет.
2) Рассмотрим другую сторону вопроса. Если мы говорим про архитектуру приложения на «Пайтоне», то пакет — это каталог, содержащий файл init.py, а также (опционально) и другие файлы .py. Таким образом, к примеру, большая Python-программа разбивается на модули. Под модулем понимается файл с исходным кодом, который без проблем применяется в других приложениях в качестве заготовки для будущих проектов либо как часть фреймворка/библиотеки. Однако это не имеет прямого отношения к теме нашей заметки, поэтому далее будем говорить лишь о Python-пакетах из репозиториев.
Продолжаем. Чтобы устанавливать пакеты в Python со всеми зависимостями, применяют менеджер пакетов pip либо модуль easy_install . Чаще всего рекомендуют использовать pip. Но если у вас присутствует инфраструктура на пакетах .egg (их «пип» не открывает), то потребуется easy_install .
Установка pip для Python 2 и 3
Установка pip затруднений не вызывает. Мало того, если вы применяете виртуальные окружения на базе virtualenv и venv, то система «пип» уже установлена.
Начиная с «Пайтон» версии 3.4 (а для 2-го «Пайтона» — с 2.7.9), «пип» поставляется одновременно с интерпретатором. Если же говорить о более ранних версиях, то устанавливать менеджер пакетов pip нужно вручную. И вот здесь у нас есть два варианта: 1. Установка посредством скрипта get_pip.py . Главный плюс — скорость. 2. Установка с помощью setuptools. Здесь кроме «пип» мы сможем использовать и easy_install .
Как выглядит установка pip на практике? Вариант 1
Рассмотрим вариант установки с помощью скрипта. Для этого скачиваем get_pip.py , а потом запускаем его в консоли. Терминал можно открыть с помощью команды Win+R>»cmd»>OK, после чего написать:
python get_pip.pyДалее установщик всё сделает сам. А если надо, то попутно установит и wheel (если нужно будет распаковать .whl), и setuptools. Да, если вы хотите запретить установку дополнительных инструментов, добавьте в строку ключи --no-wheels и/или --no-setuptools.
Кстати, если возникнет ошибка, то существует вероятность, что в переменной среды $PATH не прописан путь к Python. Чтобы решить проблему, найдите данную переменную в системном реестре и задайте её значение. Также можно указывать каждый раз полный путь до python.exe, а потом имя исполняемого Py-файла:
C:/python32/python.exe get_pip.pyНельзя не добавить, что полный путь полезен и тогда, когда на компьютере есть несколько версий Python, а вы ставите пакет лишь для одной.
Вариант 2
Здесь тоже установка не вызывает сложностей. Вам надо скачать архив с setuptools из PYPI и распаковать его в отдельный каталог. После этого в терминале перейдите в директорию с файлом setup.py и напишите:
python setup.py installЕсли хотите обновить pip в Windows, тогда:
python pip install -U pipНе сработало? Пропишите путь к папке с pip в $PATH.
Установка пакета в pip
Что же, пришло время запустить «пип» и начать устанавливать Python-пакеты путём короткой команды из консоли:
pip install имя_пакетаДа, если установка производится в операционную среду Windows, тогда перед pip нужно добавлять "python -m". Кстати, обновить пакет в Python тоже несложно посредством простой команды:
pip install имя_пакета –UСледующая команда вам пригодится, если у вас последняя версия пакета, но есть желание принудительно переустановить его:
pip install --force-reinstallХотите посмотреть список пакетов, установленных на Python? Используйте следующую команду:
pip listЕсли желаете найти конкретный пакет, зная его имя, подойдёт "pip search". Да и вообще, вы можете узнать все команды в справке, написав "pip help".
Как удалять пакеты в Python?
Иногда пакет в Python больше не нужен, поэтому его можно легко удалить:
pip uninstall имя_пакетаУстанавливаем пакеты в Python, не используя pip
Установка возможна с помощью модуля easy_install . Он умеет устанавливать как обычные пакеты, так и .egg, правда, последний формат сегодня используется довольно редко, поэтому он не поддерживается pip.
Если вас интересует установка посредством easy_install , ознакомьтесь сначала с важными минусами этого способа: — модуль не удаляет пакеты в Python; — easy_install может пытаться устанавливать недозагруженный пакет.
После установки setuptools вы можете сразу использовать easy_install . Данный модуль хранится в папке Scripts вашего интерпретатора. И если путь в $PATH прописан верно, то ставить пакеты из PYPI вы сможете простой и короткой командой:
easy_install имя_пакетаЧтобы выполнить обновление (upgrade pip), перед именем пакета и после install достаточно поставить ключ -U.
Вот, к примеру, как выглядит команда обновления для операционной системы Windows:
python -m pip install -U pipОткатиться до нужной вам версии можно следующим образом:
easy_install имя_пакета=0.2.3Хотите скачать пакет для Python из альтернативного источника? Задайте URL либо локальный адрес на ПК:
easy_install http://адрес_репозитория.ру/директория/пакет-1.1.2.zipЖелаете узнать об опциях easy_install? Выполните запуск с ключом -h:
easy_install -hКстати, пакеты, которые установлены с помощью easy_install , хранятся в файле easy-install.pth списком в директории /libs/site-packages/.
И ещё один момент: пакеты, установленные посредством easy_install , можно удалять с помощью «пип». Если же он отсутствует, вы можете удалить пакет вручную, стерев сведения о нём из easy-install.pth.
При необходимости вы можете найти определенный пакет. Искать можно следующим образом:
pip search "your_query"
Команда выше выполняет поиск и без проблем находит конкретный пакет, интересующий пользователя.
Pip eel для Python — что это?
Eel — специальная библиотека, позволяющие создавать современные программные приложения на «Питоне» с красивым интерфейсом. Но прежде, чем начать работать с этой библиотекой, ее надо установить. Однако инсталляция eel происходит точно так же, как и в случае с любым другим модулем «Питона»:
pip install eelБолее подробно на эту тему читайте здесь.
Что же, теперь вы знаете о pip для Python — что это, как используется, как установить pip. Также умеете устанавливать и удалять пакеты для «Питона».
Если же хотите освоить этот язык программирования на более высоком уровне, воспользуйтесь курсами OTUS!
pip-tools = pip-compile + pip-sync
A set of command line tools to help you keep your pip -based packages fresh, even when you've pinned them. You do pin them, right? (In building your Python application and its dependencies for production, you want to make sure that your builds are predictable and deterministic.)
Installation
Similar to pip , pip-tools must be installed in each of your project's virtual environments:
/path/to/venv/bin/activate (venv) python -m pip install pip-toolsNote: all of the remaining example commands assume you've activated your project's virtual environment.
Example usage for pip-compile
The pip-compile command lets you compile a requirements.txt file from your dependencies, specified in either pyproject.toml , setup.cfg , setup.py , or requirements.in .
Run it with pip-compile or python -m piptools compile (or pipx run --spec pip-tools pip-compile if pipx was installed with the appropriate Python version). If you use multiple Python versions, you can also run py -X.Y -m piptools compile on Windows and pythonX.Y -m piptools compile on other systems.
pip-compile should be run from the same virtual environment as your project so conditional dependencies that require a specific Python version, or other environment markers, resolve relative to your project's environment.
Note: If pip-compile finds an existing requirements.txt file that fulfils the dependencies then no changes will be made, even if updates are available. To compile from scratch, first delete the existing requirements.txt file, or see Updating requirements for alternative approaches.
Requirements from pyproject.toml
The pyproject.toml file is the latest standard for configuring packages and applications, and is recommended for new projects. pip-compile supports both installing your project.dependencies as well as your project.optional-dependencies . Thanks to the fact that this is an official standard, you can use pip-compile to pin the dependencies in projects that use modern standards-adhering packaging tools like Setuptools, Hatch or flit.
Suppose you have a 'foobar' Python application that is packaged using Setuptools , and you want to pin it for production. You can declare the project metadata as:
If you have a Django application that is packaged using Hatch , and you want to pin it for production. You also want to pin your development tools in a separate pin file. You declare django as a dependency and create an optional dependency dev that includes pytest :You can produce your pin files as easily as:pip-compile -o requirements.txt pyproject.toml This file is autogenerated by pip-compile with Python by the following command: pip-compile --output-file pyproject.toml via django via my-cool-django-app via django pip-compile --extra dev -o dev-requirements.txt pyproject.toml This file is autogenerated by pip-compile with Python by the following command: pip-compile --extra --output-file pyproject.toml via django via pytest via my-cool-django-app via pytest via pytest via pytest via pytest via my-cool-django-app via django via pytestThis is great for both pinning your applications, but also to keep the CI of your open-source Python package stable.
Requirements from setup.py and setup.cfg
pip-compile has also full support for setup.py - and setup.cfg -based projects that use setuptools .
Just define your dependencies and extras as usual and run pip-compile as above.
Requirements from requirements.in
You can also use plain text files for your requirements (e.g. if you don't want your application to be a package). To use a requirements.in file to declare the Django dependency:
# requirements.in djangoNow, run pip-compile requirements.in :
pip-compile requirements.in This file is autogenerated by pip-compile with Python by the following command: pip-compile requirements.in via django via -r requirements.in via djangoAnd it will produce your requirements.txt , with all the Django dependencies (and all underlying dependencies) pinned.
Updating requirements
pip-compile generates a requirements.txt file using the latest versions that fulfil the dependencies you specify in the supported files.
If pip-compile finds an existing requirements.txt file that fulfils the dependencies then no changes will be made, even if updates are available.
To force pip-compile to update all packages in an existing requirements.txt , run pip-compile --upgrade .
To update a specific package to the latest or a specific version use the --upgrade-package or -P flag:
only update the django package pip-compile --upgrade-package django update both the django and requests packages pip-compile --upgrade-package django --upgrade-package requests update the django package to the latest, and requests to v2.0.0 pip-compile --upgrade-package django --upgrade-package You can combine --upgrade and --upgrade-package in one command, to provide constraints on the allowed upgrades. For example to upgrade all packages whilst constraining requests to the latest version less than 3.0:pip-compile --upgrade --upgrade-packageIf you would like to use Hash-Checking Mode available in pip since version 8.0, pip-compile offers --generate-hashes flag:
pip-compile --generate-hashes requirements.in This file is autogenerated by pip-compile with Python by the following command: pip-compile --generate-hashes requirements.in via django via -r requirements.in via djangoOutput File
To output the pinned requirements in a filename other than requirements.txt , use --output-file . This might be useful for compiling multiple files, for example with different constraints on django to test a library with both versions using tox:
pip-compile --upgrade-package --output-file requirements-django0x.txt pip-compile --upgrade-package --output-file requirements-django1x.txtOr to output to standard output, use --output-file=- :
pip-compile --output-file > requirements.txt pip-compile - --output-file < requirements.in > requirements.txtForwarding options to pip
Any valid pip flags or arguments may be passed on with pip-compile 's --pip-args option, e.g.
pip-compile requirements.in --pip-argsConfiguration
You can define project-level defaults for pip-compile and pip-sync by writing them to a configuration file in the same directory as your requirements input files (or the current working directory if piping input from stdin). By default, both pip-compile and pip-sync will look first for a .pip-tools.toml file and then in your pyproject.toml . You can also specify an alternate TOML configuration file with the --config option.
For example, to by default generate pip hashes in the resulting requirements file output, you can specify in a configuration file:
Options to pip-compile and pip-sync that may be used more than once must be defined as lists in a configuration file, even if they only have one value.pip-tools supports default values for all valid command-line flags of its subcommands. Configuration keys may contain underscores instead of dashes, so the above could also be specified in this format:
You might be wrapping the pip-compile command in another script. To avoid confusing consumers of your custom script you can override the update command generated at the top of requirements files by setting the CUSTOM_COMPILE_COMMAND environment variable.pip-compile requirements.in This file is autogenerated by pip-compile with Python by the following command: ./pipcompilewrapper via django via -r requirements.in via djangoWorkflow for layered requirements
If you have different environments that you need to install different but compatible packages for, then you can create layered requirements files and use one layer to constrain the other.
For example, if you have a Django project where you want the newest 2.1 release in production and when developing you want to use the Django debug toolbar, then you can create two *.in files, one for each layer:
# requirements.in djangoAt the top of the development requirements dev-requirements.in you use -c requirements.txt to constrain the dev requirements to packages already selected for production in requirements.txt .
# dev-requirements.in -c requirements.txt django-debug-toolbarFirst, compile requirements.txt as usual:
$ pip-compile # # This file is autogenerated by pip-compile with Python 3.10 # by the following command: # # pip-compile # django==2.1.15 # via -r requirements.in pytz==2023.3 # via djangoNow compile the dev requirements and the requirements.txt file is used as a constraint:
pip-compile dev-requirements.in This file is autogenerated by pip-compile with Python by the following command: pip-compile dev-requirements.in via -c requirements.txt django-debug-toolbar via -r dev-requirements.in via -c requirements.txt django via django-debug-toolbarAs you can see above, even though a 2.2 release of Django is available, the dev requirements only include a 2.1 version of Django because they were constrained. Now both compiled requirements files can be installed safely in the dev environment.
To install requirements in production stage use:
pip-syncYou can install requirements in development stage by:
pip-sync requirements.txt dev-requirements.txtVersion control integration
You might use pip-compile as a hook for the pre-commit. See pre-commit docs for instructions. Sample .pre-commit-config.yaml :
- https://github.com/jazzband/pip-tools 7.3.0 - pip-compileYou might want to customize pip-compile args by configuring args and/or files , for example:
- https://github.com/jazzband/pip-tools 7.3.0 - pip-compile ^requirements/production\.(in|txt)$ [:, ]If you have multiple requirement files make sure you create a hook for each file.
- https://github.com/jazzband/pip-tools 7.3.0 - pip-compile pip-compile setup.py ^(setup\.py|requirements\.txt)$ - pip-compile pip-compile requirements-dev.in [] ^requirements-dev\.(in|txt)$ - pip-compile pip-compile requirements-lint.in [] ^requirements-lint\.(in|txt)$ - pip-compile pip-compile requirements.in [] ^requirements\.(in|txt)$Example usage for pip-sync
Now that you have a requirements.txt , you can use pip-sync to update your virtual environment to reflect exactly what's in there. This will install/upgrade/uninstall everything necessary to match the requirements.txt contents.
Run it with pip-sync or python -m piptools sync . If you use multiple Python versions, you can also run py -X.Y -m piptools sync on Windows and pythonX.Y -m piptools sync on other systems.
pip-sync must be installed into and run from the same virtual environment as your project to identify which packages to install or upgrade.
Be careful: pip-sync is meant to be used only with a requirements.txt generated by pip-compile .
pip-syncTo sync multiple *.txt dependency lists, just pass them in via command line arguments, e.g.
pip-sync dev-requirements.txt requirements.txtPassing in empty arguments would cause it to default to requirements.txt .
Any valid pip install flags or arguments may be passed with pip-sync 's --pip-args option, e.g.
pip-sync requirements.txt --pip-argsNote: pip-sync will not upgrade or uninstall packaging tools like setuptools , pip , or pip-tools itself. Use python -m pip install --upgrade to upgrade those packages.
Should I commit requirements.in and requirements.txt to source control?
Generally, yes. If you want a reproducible environment installation available from your source control, then yes, you should commit both requirements.in and requirements.txt to source control.
Note that if you are deploying on multiple Python environments (read the section below), then you must commit a separate output file for each Python environment. We suggest to use the -requirements.txt format (ex: win32-py3.7-requirements.txt , macos-py3.10-requirements.txt , etc.).
Cross-environment usage of requirements.in / requirements.txt and pip-compile
The dependencies of a package can change depending on the Python environment in which it is installed. Here, we define a Python environment as the combination of Operating System, Python version (3.7, 3.8, etc.), and Python implementation (CPython, PyPy, etc.). For an exact definition, refer to the possible combinations of PEP 508 environment markers.
As the resulting requirements.txt can differ for each environment, users must execute pip-compile on each Python environment separately to generate a requirements.txt valid for each said environment. The same requirements.in can be used as the source file for all environments, using PEP 508 environment markers as needed, the same way it would be done for regular pip cross-environment usage.
If the generated requirements.txt remains exactly the same for all Python environments, then it can be used across Python environments safely. But users should be careful as any package update can introduce environment-dependent dependencies, making any newly generated requirements.txt environment-dependent too. As a general rule, it's advised that users should still always execute pip-compile on each targeted Python environment to avoid issues.
Other useful tools
- pipdeptree to print the dependency tree of the installed packages.
- requirements.in / requirements.txt syntax highlighting:
- requirements.txt.vim for Vim.
- Python extension for VS Code for VS Code.
- pip-requirements.el for Emacs.
Deprecations
This section lists pip-tools features that are currently deprecated.
- In the next major release, the --allow-unsafe behavior will be enabled by default (https://github.com/jazzband/pip-tools/issues/989). Use --no-allow-unsafe to keep the old behavior. It is recommended to pass --allow-unsafe now to adapt to the upcoming change.
- The legacy resolver is deprecated and will be removed in future versions. The new default is --resolver=backtracking .
- In the next major release, the --strip-extras behavior will be enabled by default (https://github.com/jazzband/pip-tools/issues/1613). Use --no-strip-extras to keep the old behavior.
A Note on Resolvers
You can choose from either default backtracking resolver or the deprecated legacy resolver.
The legacy resolver will occasionally fail to resolve dependencies. The backtracking resolver is more robust, but can take longer to run in general.
You can continue using the legacy resolver with --resolver=legacy although note that it is deprecated and will be removed in a future release.
Using Python's pip to Manage Your Projects' Dependencies

The standard package manager for Python is pip . It allows you to install and manage packages that aren’t part of the Python standard library. If you’re looking for an introduction to pip , then you’ve come to the right place!
In this tutorial, you’ll learn how to:
- Set up pip in your working environment
- Fix common errors related to working with pip
- Install and uninstall packages with pip
- Manage projects’ dependencies using requirements files
You can do a lot with pip , but the Python community is very active and has created some neat alternatives to pip . You’ll learn about those later in this tutorial.
Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.
Getting Started With pip
So, what exactly does pip do? pip is a package manager for Python. That means it’s a tool that allows you to install and manage libraries and dependencies that aren’t distributed as part of the standard library. The name pip was introduced by Ian Bicking in 2008:
I’ve finished renaming pyinstall to its new name: pip. The name pip is [an] acronym and declaration: pip installs packages. (Source)
Package management is so important that Python’s installers have included pip since versions 3.4 and 2.7.9, for Python 3 and Python 2, respectively. Many Python projects use pip , which makes it an essential tool for every Pythonista.
The concept of a package manager might be familiar to you if you’re coming from another programming language. JavaScript uses npm for package management, Ruby uses gem, and the .NET platform uses NuGet. In Python, pip has become the standard package manager.
Finding pip on Your System
The Python 3 installer gives you the option to install pip when installing Python on your system. In fact, the option to install pip with Python is checked by default, so pip should be ready for you to use after installing Python.
Note: On some Linux (Unix) systems like Ubuntu, pip comes in a separate package called python3-pip , which you need to install with sudo apt install python3-pip . It’s not installed by default with the interpreter.
You can verify that pip is available by looking for the pip3 executable on your system. Select your operating system below and use your platform-specific command accordingly:
Windows Command Prompt
C:\> where pip3The where command on Windows will show you where you can find the executable of pip3 . If Windows can’t find an executable named pip3 , then you can also try looking for pip without the three ( 3 ) at the end.
$ which pip3The which command on Linux systems and macOS shows you where the pip3 binary file is located.
On Windows and Unix systems, pip3 may be found in more than one location. This can happen when you have multiple Python versions installed. If you can’t find pip in any location on your system, then you may consider reinstalling pip.
Instead of running your system pip directly, you can also run it as a Python module. In the next section, you’ll learn how.
Running pip as a Module
When you run your system pip directly, the command itself doesn’t reveal which Python version pip belongs to. This unfortunately means that you could use pip to install a package into the site-packages of an old Python version without noticing. To prevent this from happening, you can run pip as a Python module:
$ python3 -m pipNotice that you use python3 -m to run pip . The -m switch tells Python to run a module as an executable of the python3 interpreter. This way, you can ensure that your system default Python 3 version runs the pip command. If you want to learn more about this way of running pip , then you can read Brett Cannon’s insightful article about the advantages of using python3 -m pip .
Sometimes you may want to be more explicit and limit packages to a specific project. In situations like this, you should run pip inside a virtual environment.
Using pip in a Python Virtual Environment
To avoid installing packages directly into your system Python installation, you can use a virtual environment. A virtual environment provides an isolated Python interpreter for your project. Any packages that you use inside this environment will be independent of your system interpreter. This means that you can keep your project’s dependencies separate from other projects and the system at large.
Using pip inside a virtual environment has three main advantages. You can:
- Be sure that you’re using the right Python version for the project at hand
- Be confident that you’re referring to the correct pip instance when running pip or pip3
- Use a specific package version for your project without affecting other projects
Python 3 has the built-in venv module for creating virtual environments. This module helps you create virtual environments with an isolated Python installation. Once you’ve activated the virtual environment, then you can install packages into this environment. The packages that you install into one virtual environment are isolated from all other environments on your system.
You can follow these steps to create a virtual environment and verify that you’re using the pip module inside the newly created environment:
Windows Command Prompt
C:\> python -m venv venv C:\> venv\Scripts\activate.bat (venv) C:\> pip3 --version pip 21.2.3 from . \lib\site-packages\pip (python 3.10) (venv) C:\> pip --version pip 21.2.3 from . \lib\site-packages\pip (python 3.10)$ python3 -m venv venv $ source venv/bin/activate (venv) $ pip3 --version pip 21.2.3 from . /python3.10/site-packages/pip (python 3.10) (venv) $ pip --version pip 21.2.3 from . /python3.10/site-packages/pip (python 3.10)Here you create a virtual environment named venv by using Python’s built-in venv module. Then you activate it with the source command. The parentheses ( () ) surrounding your venv name indicate that you successfully activated the virtual environment.
Finally, you check the version of the pip3 and pip executables inside your activated virtual environment. Both point to the same pip module, so once your virtual environment is activated, you can use either pip or pip3 .
Reinstalling pip When Errors Occur
When you run the pip command, you may get an error in some cases. Your specific error message will depend on your operating system:
Operating System Error Message Windows 'pip' is not recognized as an internal or external command,
operable program or batch file.Linux bash: pip: command not found macOS zsh: command not found: pip Error messages like these indicate that something went wrong with the installation of pip .
Note: Before you start any troubleshooting when the pip command doesn’t work, you can try out using the pip3 command with the three ( 3 ) at the end.
Getting errors like the ones shown above can be frustrating because pip is vital for installing and managing external packages. Some common problems with pip are related to how this tool was installed on your system.
Although the error messages for various systems differ, they all point to the same problem: Your system can’t find pip in the locations listed in your PATH variable. On Windows, PATH is part of the system variables. On macOS and Linux, PATH is part of the environment variables. You can check the contents of your PATH variable with this command:
Windows Command Prompt
C:\> echo %PATH%$ echo $PATHThe output of this command will show a list of locations (directories) on your disk where the operating system looks for executable programs. Depending on your system, locations can be separated by a colon ( : ) or a semicolon ( ; ).
By default, the directory that contains the pip executable should be present in PATH after you install Python or create a virtual environment. However, missing pip is a common issue. Two supported methods can help you install pip again and add it to your PATH :
- The ensurepip module
- The get-pip.py script
The ensurepip module has been part of the standard library since Python 3.4. It was added to provide a straightforward way for you to reinstall pip if, for example, you skipped it when installing Python or you uninstalled pip at some point. Select your operating system below and run ensurepip accordingly:
Windows Command Prompt
C:\> python -m ensurepip --upgrade$ python3 -m ensurepip --upgradeIf pip isn’t installed yet, then this command installs it in your current Python environment. If you’re in an active virtual environment, then the command installs pip into that environment. Otherwise, it installs pip globally on your system. The --upgrade option ensures that the pip version is the same as the one declared in ensurepip .
Note: The ensurepip module doesn’t access the internet. The latest version of pip that ensurepip can install is the version that’s bundled in your environment’s Python installation. For example, running ensurepip with Python 3.10.0 installs pip 21.2.3. If you want a newer pip version, then you’d need to first run ensurepip . Afterward, you can update pip manually to its latest version.
Another way to fix your pip installation is to use the get-pip.py script. The get-pip.py file contains a full copy of pip as an encoded ZIP file. You can download get-pip.py directly from the PyPA bootstrap page. Once you have the script on your machine, then you run the Python script like this:
Windows Command Prompt
C:\> python get-pip.py$ python3 get-pip.pyThis script will install the latest version of pip , setuptools , and wheel in your current Python environment. If you only want to install pip , then you can add the --no-setuptools and --no-wheel options to your command.
If none of the methods above work, then it might be worth trying to download the latest Python version for your current platform. You can follow the Python 3 Installation & Setup Guide to make sure that pip is appropriately installed and works without errors.
Installing Packages With pip
Python is considered a batteries included language. This means that the Python standard library contains an extensive set of packages and modules to help developers with their coding projects.
At the same time, Python has an active community that contributes an even more extensive set of packages that can help you with your development needs. These packages are published to the Python Package Index, also known as PyPI (pronounced Pie Pea Eye).
Note: When you’re installing third-party packages, you have to be careful. Check out How to Evaluate the Quality of Python Packages for a full guide to ensuring your packages are trustworthy.
PyPI hosts an extensive collection of packages, including development frameworks, tools, and libraries. Many of these packages provide friendly interfaces to the Python standard library’s functionality.
Using the Python Package Index (PyPI)
One of the many packages that PyPI hosts is called requests . The requests library helps you to interact with web services by abstracting the complexities of HTTP requests. You can learn all about requests on its official documentation site.
When you want to use the requests package in your project, you must first install it into your environment. If you don’t want to install it in your system Python site-packages, then you can create a virtual environment first, as shown above.
Once you’ve created the virtual environment and activated it, then your command-line prompt shows the name of the virtual environment inside the parentheses. Any pip commands that you perform from now on will happen inside your virtual environment.
To install packages, pip provides an install command. You can run it to install the requests package:
Windows Command Prompt
(venv) C:\> python -m pip install requests(venv) $ python3 -m pip install requestsIn this example, you run pip with the install command followed by the name of the package that you want to install. The pip command looks for the package in PyPI, resolves its dependencies, and installs everything in your current Python environment to ensure that requests will work.
The pip install command always looks for the latest version of the package and installs it. It also searches for dependencies listed in the package metadata and installs them to ensure that the package has all the requirements that it needs.
It’s also possible to install multiple packages in a single command:
Windows Command Prompt
(venv) C:\> python -m pip install rptree codetiming(venv) $ python3 -m pip install rptree codetimingBy chaining the packages rptree and codetiming in the pip install command, you install both packages at once. You can add as many packages as you want to the pip install command. In cases like this, a requirements.txt file can come in handy. Later in this tutorial, you’ll learn how to use a requirements.txt file to install many packages at once.
Note: Unless the specific version number of a package is relevant to this tutorial, you’ll notice the version string takes the generic form of x.y.z . This is a placeholder format and can stand for 3.1.4 , 2.9 , or any other version number. When you follow along, the output in your terminal will display your actual package version numbers.
You can use the list command to display the packages installed in your environment, along with their version numbers:
Windows Command Prompt
(venv) C:\> python -m pip list Package Version ------------------ --------- certifi x.y.z charset-normalizer x.y.z codetiming x.y.z idna x.y.z pip x.y.z requests x.y.z rptree x.y.z setuptools x.y.z urllib3 x.y.z(venv) $ python3 -m pip list Package Version ------------------ --------- certifi x.y.z charset-normalizer x.y.z idna x.y.z pip x.y.z requests x.y.z setuptools x.y.z urllib3 x.y.zThe pip list command renders a table that shows all installed packages in your current environment. The output above shows the version of the packages using an x.y.z placeholder format. When you run the pip list command in your environment, pip displays the specific version number that you’ve installed for each package.
To get more information about a specific package, you can look at the package’s metadata by using the show command in pip :
Windows Command Prompt
(venv) C:\> python -m pip show requests Name: requests Version: x.y.z Summary: Python HTTP for Humans. . Requires: certifi, idna, charset-normalizer, urllib3 Required-by:(venv) $ python3 -m pip show requests Name: requests Version: x.y.z Summary: Python HTTP for Humans. . Requires: certifi, idna, charset-normalizer, urllib3 Required-by:The output of this command on your system will list the package’s metadata. The Requires line lists packages, such as certifi , idna , charset-normalizer , and urllib3 . These were installed because requests depends on them to work correctly.
Now that you’ve installed requests and its dependencies, you can import it just like any other regular package in your Python code. Start the interactive Python interpreter and import the requests package:
>>> import requests >>> requests.__version__ "x.y.z"After starting the interactive Python interpreter, you imported the requests module. By calling requests.__version__ , you verified that you were using the requests module within your virtual environment.
Using a Custom Package Index
By default, pip uses PyPI to look for packages. But pip also gives you the option to define a custom package index.
Using pip with a custom index can be helpful when the PyPI domain is blocked on your network or if you want to work with packages that aren’t publicly available. Sometimes system administrators also create their own internal package index to better control which package versions are available to pip users on the company’s network.
A custom package index must comply with PEP 503 – Simple Repository API to work with pip . You can get an impression of how such an API (Application Programming Interface) looks by visiting the PyPI Simple Index—but be aware that this is a large page with a lot of hard-to-parse content. Any custom index that follows the same API can be targeted with the --index-url option. Instead of typing --index-url , you can also use the -i shorthand.
For example, to install the rptree tool from the TestPyPI package index, you can run the following command:
Windows Command Prompt
(venv) C:\> python -m pip install -i https://test.pypi.org/simple/ rptree(venv) $ python3 -m pip install -i https://test.pypi.org/simple/ rptreeWith the -i option, you tell pip to look at a different package index instead of PyPI, the default one. Here, you’re installing rptree from TestPyPI rather than from PyPI. You can use TestPyPI to fine-tune the publishing process for your Python packages without cluttering the production package index on PyPI.
If you need to use an alternative index permanently, then you can set the index-url option in the pip configuration file. This file is called pip.conf , and you can find its location by running the following command:
Windows Command Prompt
(venv) C:\> python -m pip config list -vv(venv) $ python3 -m pip config list -vvWith the pip config list command, you can list the active configuration. This command only outputs something when you have custom configurations set. Otherwise, the output is empty. That’s when the additive --verbose , or -vv , option can be helpful. When you add -vv , pip shows you where it looks for the different configuration levels.
If you want to add a pip.conf file, then you can choose one of the locations that pip config list -vv listed. A pip.conf file with a custom package index looks like this:
Configuration File
# pip.conf [global] index-url = https://test.pypi.org/simple/When you have a pip.conf file like this, pip will use the defined index-url to look for packages. With this configuration, you don’t need to use the --index-url option in your pip install command to specify that you only want packages that can be found in the Simple API of TestPyPI.
Installing Packages From Your GitHub Repositories
You’re not limited to packages hosted on PyPI or other package indexes. pip also provides the option to install packages from a GitHub repository. But even when a package is hosted on PyPI, like the Real Python directory tree generator, you can opt to install it from its Git repository:
Windows Command Prompt
(venv) C:\> python -m pip install git+https://github.com/realpython/rptree(venv) $ python3 -m pip install git+https://github.com/realpython/rptreeWith the git+https scheme, you can point to a Git repository that contains an installable package. You can verify that you installed the package correctly by running an interactive Python interpreter and importing rptree :
>>> import rptree >>> rptree.__version__ "x.y.z"After starting the interactive Python interpreter, you import the rptree module. By calling rptree.__version__ , you verify that you’re using the rptree module that’s based in your virtual environment.
Note: If you’re using a version control system (VCS) other than Git, pip has you covered. To learn how to use pip with Mercurial, Subversion, or Bazaar, check out the VCS Support chapter of the pip documentation.
Installing packages from a Git repository can be helpful if the package isn’t hosted on PyPI but has a remote Git repository. The remote repository you point pip to can even be hosted on an internal Git server on your company’s intranet. This can be useful when you’re behind a firewall or have other restrictions for your Python projects.
Installing Packages in Editable Mode to Ease Development
When working on your own package, installing it in an editable mode can make sense. By doing this, you can work on the source code while still using your command line like you would in any other package. A typical workflow is to first clone the repository and then use pip to install it as an editable package in your environment:
Windows Command Prompt
1C:\> git clone https://github.com/realpython/rptree 2C:\> cd rptree 3C:\rptree> python3 -m venv venv 4C:\rptree> venv\Scripts\activate.bat 5(venv) C:\rptree> python -m pip install -e .1$ git clone https://github.com/realpython/rptree 2$ cd rptree 3$ python3 -m venv venv 4$ source venv/bin/activate 5(venv) $ python3 -m pip install -e .With the commands above, you installed the rptree package as an editable module. Here’s a step-by-step breakdown of the actions you just performed:
- Line 1 cloned the Git repository of the rptree package.
- Line 2 changed the working directory to rptree/ .
- Lines 3 and 4 created and activated a virtual environment.
- Line 5 installed the content of the current directory as an editable package.
The -e option is shorthand for the --editable option. When you use the -e option with pip install , you tell pip that you want to install the package in editable mode. Instead of using a package name, you use a dot ( . ) to point pip to the current directory.
If you hadn’t used the -e flag, pip would’ve installed the package normally into your environment’s site-packages/ folder. When you install a package in editable mode, you’re creating a link in the site-packages to the local project path:
~/rptree/venv/lib/python3.10/site-packages/rptree.egg-linkUsing the pip install command with the -e flag is just one of many options that pip install offers. You can check out pip install examples in the pip documentation. There you’ll learn how to install specific versions of a package or point pip to a different index that’s not PyPI.
In the next section, you’ll learn how requirements files can help with your pip workflows.
Using Requirements Files
The pip install command always installs the latest published version of a package, but sometimes your code requires a specific package version to work correctly.
You want to create a specification of the dependencies and versions that you used to develop and test your application so that there are no surprises when you use the application in production.
Pinning Requirements
When you share your Python project with other developers, you may want them to use the same versions of external packages that you’re using. Maybe a specific version of a package contains a new feature that you rely on, or the version of a package that you’re using is incompatible with former versions.
These external dependencies are also called requirements. You’ll often find Python projects that pin their requirements in a file called requirements.txt or similar. The requirements file format allows you to specify precisely which packages and versions should be installed.
Running pip help shows that there’s a freeze command that outputs the installed packages in requirements format. You can use this command, redirecting the output to a file to generate a requirements file:
Windows Command Prompt
(venv) C:\> python -m pip freeze > requirements.txt(venv) $ python3 -m pip freeze > requirements.txtThis command creates a requirements.txt file in your working directory with the following content:
Python Requirements
certifi==x.y.z charset-normalizer==x.y.z idna==x.y.z requests==x.y.z urllib3==x.y.zRemember that x.y.z displayed above is a placeholder format for the package versions. Your requirements.txt file will contain real version numbers.
The freeze command dumps the name and version of the currently installed packages to standard output. You can redirect the output to a file that you can later use to install your exact requirements into another system. You can name the requirements file whatever you want. However, a widely adopted convention is to name it requirements.txt .
When you want to replicate the environment in another system, you can run pip install , using the -r switch to specify the requirements file:
Windows Command Prompt
(venv) C:\> python -m pip install -r requirements.txt(venv) $ python3 -m pip install -r requirements.txtIn the command above, you tell pip to install the packages listed in requirements.txt into your current environment. The package versions will match the version constraints that the requirements.txt file contains. You can run pip list to display the packages you just installed, with their version numbers:
Windows Command Prompt
(venv) C:\> python -m pip list Package Version ------------------ --------- certifi x.y.z charset-normalizer x.y.z idna x.y.z pip x.y.z requests x.y.z setuptools x.y.z urllib3 x.y.z(venv) $ python3 -m pip list Package Version ------------------ --------- certifi x.y.z charset-normalizer x.y.z idna x.y.z pip x.y.z requests x.y.z setuptools x.y.z urllib3 x.y.zNow you’re ready to share your project! You can submit requirements.txt into a version control system like Git and use it to replicate the same environment on other machines. But wait, what happens if new updates are released for these packages?
Fine-Tuning Requirements
The problem with hardcoding your packages’ versions and dependencies is that packages are updated frequently with bug and security fixes. You probably want to leverage those updates as soon as they’re published.
The requirements file format allows you to specify dependency versions using comparison operators that give you some flexibility to ensure packages are updated while still defining the base version of a package.
Open requirements.txt in your favorite editor and turn the equality operators ( == ) into greater than or equal to operators ( >= ), like in the example below:
Python Requirements
# requirements.txt certifi>=x.y.z charset-normalizer>=x.y.z idna>=x.y.z requests>=x.y.z urllib3>=x.y.zYou can change the comparison operator to >= to tell pip to install an exact or greater version that has been published. When you set a new environment by using the requirements.txt file, pip looks for the latest version that satisfies the requirement and installs it.
Next, you can upgrade the packages in your requirements file by running the install command with the --upgrade switch or the -U shorthand:
Windows Command Prompt
(venv) C:\> python -m pip install -U -r requirements.txt(venv) $ python3 -m pip install -U -r requirements.txtIf a new version is available for a listed package, then the package will be upgraded.
In an ideal world, new versions of packages would be backward compatible and would never introduce new bugs. Unfortunately, new versions can introduce changes that’ll break your application. To fine-tune your requirements, the requirements file syntax supports additional version specifiers.
Imagine that a new version, 3.0 , of requests is published but introduces an incompatible change that breaks your application. You can modify the requirements file to prevent 3.0 or higher from being installed:
Python Requirements
# requirements.txt certifi==x.y.z charset-normalizer==x.y.z idna==x.y.z requests>=x.y.z, 3.0 urllib3==x.y.zChanging the version specifier for the requests package ensures that any version greater than or equal to 3.0 doesn’t get installed. The pip documentation provides extensive information about the requirements file format, and you can consult it to learn more.
Separating Production and Development Dependencies
Not all packages that you install during the development of your applications will be production dependencies. For example, you’ll probably want to test your application, so you need a test framework. A popular framework for testing is pytest . You want to install it in your development environment, but you don’t want it in your production environment, because it isn’t a production dependency.
You create a second requirements file, requirements_dev.txt , to list additional tools to set up a development environment:
Python Requirements
# requirements_dev.txt pytest>=x.y.zHaving two requirements files will demand that you use pip to install both of them, requirements.txt and requirements_dev.txt . Fortunately, pip allows you to specify additional parameters within a requirements file, so you can modify requirements_dev.txt to also install the requirements from the production requirements.txt file:
Python Requirements
# requirements_dev.txt -r requirements.txt pytest>=x.y.zNotice that you use the same -r switch to install the production requirements.txt file. Now, in your development environment, you only have to run this single command to install all requirements:
Windows Command Prompt
(venv) C:\> python -m pip install -r requirements_dev.txt(venv) $ python3 -m pip install -r requirements_dev.txtBecause requirements_dev.txt contains the -r requirements.txt line, you’ll install not only pytest but also the pinned requirements of requirements.txt . In a production environment, it’s sufficient to install the production requirements only:
Windows Command Prompt
(venv) C:\> python -m pip install -r requirements.txt(venv) $ python3 -m pip install -r requirements.txtWith this command, you install the requirements listed in requirements.txt . In contrast to your development environment, your production environment won’t have pytest installed.
Freezing Requirements for Production
You created the production and development requirement files and added them to source control. These files use flexible version specifiers to ensure that you leverage bug fixes published by your dependencies. You’ve also tested your application and are now ready to deploy it to production.
You know that all the tests pass and the application works with the dependencies that you used in your development process, so you probably want to ensure that you deploy identical versions of dependencies to production.
The current version specifiers don’t guarantee that the identical versions will be deployed to production, so you want to freeze the production requirements before releasing your project.
After you’ve finished development with your current requirements, a workflow to create a new release of your current project can look like this:
Step Command Explanation 1 pytest Run your tests and verify that your code is working properly. 2 pip install -U -r requirements.txt Upgrade your requirements to versions that match the constraints in your requirements.txt file. 3 pytest Run your tests and consider downgrading any dependency that introduced errors to your code. 4 pip freeze > requirements_lock.txt Once the project works correctly, freeze the dependencies into a requirements_lock.txt file. With a workflow like this, the requirements_lock.txt file will contain exact version specifiers and can be used to replicate your environment. You’ve ensured that when your users install the packages listed in requirements_lock.txt into their own environments, they’ll be using the versions that you intend them to use.
Freezing your requirements is an important step to ensure that your Python project works the same way for your users in their environments as it did in yours.
Uninstalling Packages With pip
Once in a while, you’ll have to uninstall a package. Either you found a better library to replace it, or it’s something that you don’t need. Uninstalling packages can be a bit tricky.
Notice that when you installed requests , you got pip to install other dependencies too. The more packages you install, the bigger the chance that multiple packages depend on the same dependency. This is where the show command in pip comes in handy.
Before you uninstall a package, make sure to run the show command for that package:
Windows Command Prompt
(venv) C:\> python -m pip show requests Name: requests Version: 2.26.0 Summary: Python HTTP for Humans. Home-page: https://requests.readthedocs.io Author: Kenneth Reitz Author-email: me@kennethreitz.org License: Apache 2.0 Location: . /python3.9/site-packages Requires: certifi, idna, charset-normalizer, urllib3 Required-by:(venv) $ python3 -m pip show requests Name: requests Version: 2.26.0 Summary: Python HTTP for Humans. Home-page: https://requests.readthedocs.io Author: Kenneth Reitz Author-email: me@kennethreitz.org License: Apache 2.0 Location: . /python3.9/site-packages Requires: certifi, idna, charset-normalizer, urllib3 Required-by:Notice the last two fields, Requires and Required-by . The show command tells you that requests requires certifi , idna , charset-normalizer , and urllib3 . You probably want to uninstall those too. Notice that requests isn’t required by any other package. So it’s safe to uninstall it.
You should run the show command against all of the requests dependencies to ensure that no other libraries also depend on them. Once you understand the dependency order of the packages that you want to uninstall, then you can remove them using the uninstall command:
Windows Command Prompt
(venv) C:\> python -m pip uninstall certifi(venv) $ python3 -m pip uninstall certifiThe uninstall command shows you the files that will be removed and asks for confirmation. If you’re sure that you want to remove the package because you’ve checked its dependencies and know that nothing else is using it, then you can pass a -y switch to suppress the file list and confirmation dialog:
Windows Command Prompt
(venv) C:\> python -m pip uninstall urllib3 -y(venv) $ python3 -m pip uninstall urllib3 -yHere you uninstall urllib3 . Using the -y switch, you suppress the confirmation dialog asking you if you want to uninstall this package.
In a single call, you can specify all the packages that you want to uninstall:
Windows Command Prompt
(venv) C:\> python -m pip uninstall -y charset-normalizer idna requests(venv) $ python3 -m pip uninstall -y charset-normalizer idna requestsYou can pass in multiple packages to the pip uninstall command. If you didn’t add any additional switches, then you’d need to confirm uninstalling each package. By passing the -y switch, you can uninstall them all without any confirmation dialog.
You can also uninstall all the packages listed in a requirements file by providing the -r option. This command will prompt a confirmation request for each package, but you can suppress it with the -y switch:
Windows Command Prompt
(venv) C:\> python -m pip uninstall -r requirements.txt -y(venv) $ python3 -m pip uninstall -r requirements.txt -yRemember to always check the dependencies of packages that you want to uninstall. You probably want to uninstall all dependencies, but uninstalling a package used by others will break your working environment. In consequence, your project may not work correctly anymore.
If you’re working in a virtual environment, it can be less work to just create a new virtual environment. Then you can install the packages that you need instead of trying to uninstall the packages that you don’t need. However, pip uninstall can be really helpful when you need to uninstall a package from your system Python installation. Using pip uninstall is a good way to declutter your system if you accidentally install a package system-wide.
Exploring Alternatives to pip
The Python community provides excellent tools and libraries for you to use beyond pip . These include alternatives to pip that try to simplify and improve package management.
Here are some other package management tools that are available for Python:
Tool Description Conda Conda is a package, dependency, and environment manager for many languages, including Python. It comes from Anaconda, which started as a data science package for Python. Consequently, it’s widely used for data science and machine learning applications. Conda operates its own index to host compatible packages. Poetry Poetry will look very familiar to you if you’re coming from JavaScript and npm. Poetry goes beyond package management, helping you build distributions for your applications and libraries and deploying them to PyPI. Pipenv Pipenv is another package management tool that merges virtual environment and package management in a single tool. Pipenv: A Guide to the New Python Packaging Tool is a great place to start learning about Pipenv and its approach to package management. Only pip comes bundled in the standard Python installation. If you want to use any alternatives listed above, then you have to follow the installation guides in their documentation. With so many options, you’re sure to find the right tools for your programming journey!
Conclusion
Many Python projects use the pip package manager to manage their dependencies. It’s included with the Python installer, and it’s an essential tool for dependency management in Python.
In this tutorial, you learned how to:
- Set up and run pip in your working environment
- Fix common errors related to working with pip
- Install and uninstall packages with pip
- Define requirements for your projects and applications
- Pin dependencies in requirements files
In addition, you’ve learned about the importance of keeping dependencies up to date and alternatives to pip that can help you manage those dependencies.
By taking a closer look at pip , you’ve explored an essential tool in your Python development workflows. With pip , you can install and manage any additional packages that you find on PyPI. You can use external packages from other developers as requirements and concentrate on the code that makes your project unique.
Mark as Completed
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: A Beginner's Guide to pip
Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

About Philipp Acsany
Philipp is a Berlin-based software engineer with a graphic design background and a passion for full-stack web development.
Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:







Master Real-World Python Skills With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
Master Real-World Python Skills
With Unlimited Access to Real PythonJoin us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
What Do You Think?
Rate this article:
What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.
Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal. Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!
Related Tutorial Categories: basics tools

