Python — Pip Commands you might not know
Tags: python, programming, codenewbie, computerscience
Published: 2021-03-18 16:50:33

<!-- wp:heading {"level":3} -->
<h3>Pip can be used to do more than just install packages. A list of common pip commands you might find useful are listed in the&nbsp;article</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3>Table of&nbsp;Contents</h3>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li>What is Pip?</li><li>Setting up Virtual Environment</li><li>Update Pip</li><li>Install A Package</li><li>Update Package</li><li>Install Specific version of a package</li><li>Uninstall a Package</li><li>Information about an installed package</li><li>List all installed packages</li><li>List all installed packages which are not up to date</li><li>Generate a requirements.txt file</li><li>Install all dependencies from requirements.txt file</li><li>Verify installed packages have compatible dependencies</li><li>A hack to update all packages</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>What is&nbsp;Pip?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Pip is a package manager tool for Python. If you have worked with Python before, you have most probably worked with pip as well. The most common use of pip is to install packages but there are a few other helpful commands you might not know.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Pip fetches packages from <a href="https://pypi.org/" rel="noreferrer noopener" target="_blank">PyPI</a></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Setting up Virtual Environment</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4>Install virtualenv</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>On macOS and Linux:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>python3 -m pip install --user virtualenv</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>On Windows:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>python -m pip install --user virtualenv</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:heading {"level":4} -->
<h4>Create virtual environment</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>On macOS and Linux:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>python3 -m venv env</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>On Windows:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>python -m venv env</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>This will create a virtual environment named <strong>venv</strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Activate Virtual Environment</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Ensure your file paths are correct.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>On macOS and Linux:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>source venv/bin/activate</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>On Windows:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>venv\Scripts\activate</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:heading {"level":4} -->
<h4>Select Interpreter for VS&nbsp;Code</h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Press <strong>‘Ctrl+Shift+P’</strong> for Windows/Linux and <strong>⇧⌘P</strong> for Mac to open the command palette</li><li>Search for <strong>‘Select Interpreter’</strong></li><li>Click <strong>‘Enter Interpreter Path’ &gt; ‘Find’</strong></li><li>A file explore should open up, go to <strong>venv&gt;Scripts&gt;python.exe</strong></li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>Deactivate/Leave the virtual environment</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Type the following command</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>deactivate</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:heading {"level":3} -->
<h3>Update Pip</h3>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>python -m pip install --upgrade pip</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:heading {"level":3} -->
<h3>Install A&nbsp;Package</h3>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip install &lt;package-name&gt;</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>If I want to install pandas, a python package, I’d type the following</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip install pandas</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>This is going to install the latest version of a package available on PyPI</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Update Package</h3>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip install -U &lt;package name&gt;</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>If I want to update my version of pandas, I’d type the following</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip install -U pandas</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>If an old version of the package exists, pip will first uninstall that version. It will then install the most up-to-date version from PyPI. If an up-to-date version is already installed, pip won’t do anything.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Install Specific version of a&nbsp;package</h3>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip install &lt;package-name&gt;==&lt;version&gt;</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>If I wanted to install pandas version 1.20, I’d type the following</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip install pandas==1.2.0</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>If a version of a package is already installed, pip will uninstall the existing package and install the specified version of the package</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you want to install a version of a package newer than a specific version, type the following</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip install &lt;packagename&gt;&gt;=&lt;version&gt;</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>To install a version of pandas, newer than 1.2.0</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip install pandas&gt;=1.2.0</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:heading {"level":3} -->
<h3>Uninstall a&nbsp;Package</h3>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip uninstall pandas</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:heading {"level":3} -->
<h3>Information about an installed package</h3>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip show &lt;package name&gt;</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>To get information about the package pandas which I installed earlier</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip show pandas</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>If you try to get information about a package that is not installed in your environment, pip will return a warning ‘Package Not Found’</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>List all installed packages</h3>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip list</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>This will list all the installed packages in the environment in a tabular format</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>An alternative is the following</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip freeze</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:heading {"level":3} -->
<h3>List all installed packages which are not up to&nbsp;date</h3>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip list -o</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:heading {"level":3} -->
<h3>Generate a requirements.txt file</h3>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip freeze &gt; requirements.txt</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:heading {"level":3} -->
<h3>Install all dependencies from requirements.txt file</h3>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip install -r requirements.txt</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>Pip will ignore all the packages which have already been installed before</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Verify installed packages have compatible dependencies</h3>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip check</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:heading {"level":3} -->
<h3>A hack to update all&nbsp;packages</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Generate a requirements.txt file</li></ul>
<!-- /wp:list -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip freeze &gt; requirements.txt</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:list -->
<ul><li>Open the requirements.txt file and replace all instances of <strong>==</strong> with <strong>&gt;=</strong></li><li>Installed dependencies from requirements.txt&nbsp;</li></ul>
<!-- /wp:list -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><strong>pip install -r requirements.txt --upgrade</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:list -->
<ul><li>Any packages which are up to date will be ignored and packages with a newer version available on PyPI will be installed.</li></ul>
<!-- /wp:list -->