3 ways to pip install a package - fastai2 use-case
A detailed description on how to pip install a python package using 3 different methods, and which method to pick depending on your specific use-case.
- Introduction
- 1- Installing from pypi repository
- 2- Installing from GitHub - Non-editable package
- 3- Installing from GitHub- Editable package
- Appendix A: How to install Anaconda
- Appendix B: Creating a conda enviroment
- Appendix C: Useful pip commands
- Appendix D: Useful conda commands
- Conclusion
Introduction
In this article, I will show 3 methods to install the new fastai2 library using pip install
. The 3 methods described here can be used to similar python libraries published on both pypi repository and on GitHub. I voluntarily omitted the conda install
option because I wanted to focus on the methods that can be reproduced in any free cloud environment such as Google Colab or Kaggle.
fastai2
by the name of the corresponding python package such as torch
Although, fastai2 is still under development and its official release is expected to be during the next summer, it is important to be familiar with this first method especially if you intend to use fastai2 in a production environment.
To install fastai2 from fastai2 pypi repository, type the following command either from:
-
the terminal:
pip install fastai2
-
or from a Jupyter notebook cell:
!pip install fastai2
The difference between the 2 options is the use of the !
in the case of Jupyter notebook. However, the result is the same, the fastai2 package will be installed in the current active virtual environment.
base
virtual environment in Ananconda (base
is the default virual environment created when you installed Anaconda). Check out the Appendices A and B, here below, on how to respectively install and create a virtual environment.
Running the following command:
conda env list
outputs the list of my current virtual environments:
# conda environments:
#
base /home/farid/anaconda3
fastai2 /home/farid/anaconda3/envs/fastai2
fastai2_dev * /home/farid/anaconda3/envs/fastai2_dev
fastai /home/farid/anaconda3/envs/fastai
gluon /home/farid/anaconda3/envs/gluon
prophet /home/farid/anaconda3/envs/prophet
The asterisk (*) indicates the active virtual environment
To check where the fastai2 package has been install, run the following command:
pip show fastai2
This command displays the following useful information:
Name: fastai2
Version: 0.0.11
Summary: Version 2 of the fastai library
Home-page: https://github.com/fastai/fastai2
Author: Jeremy Howard, Sylvain Gugger, and contributors
Author-email: info@fast.ai
License: Apache Software License 2.0
Location: /home/farid/anaconda3/envs/fastai2/lib/python3.6/site-packages
Requires: scikit-learn, pandas, fastprogress, requests, scipy, pillow, torchvision, matplotlib, pyyaml, spacy, torch, fastcore
Required-by:
It displays the package name, the version number, the location where it was installed (with the name of the virtual environment: in our case it is fastai2), and some other information.
2- Installing from GitHub - Non-editable package
The second method consists in installing the fastai2 package from the fastai2 GitHub repository. This begs the question: Why would we be interested in doing so? The answer is simple: If a python package is under development, and changes in the source code are introduced on a regular basis, chances are the package found on the pypi repository is lagging behind. In order to use the latest fastai2 features you need to install fastai2 from its GitHub repository.
As illustration of a situation where this option is convenient, I will cite the example where Jeremy shared a Kaggle kernel in which he wanted to show how to use a new developed module called medical. The latter was under development, and was not available on the pypi repository. Therefore the solution was to install the latest fastai2 version (that includes the medical module) by running the following command either on:
-
the terminal:
pip install git+https://github.com/fastai/fastai2.git
-
or from a Jupyter notebook cell:
!pip install git+https://github.com/fastai/fastai2.git
By comaparing the command above with the one used in method 1, you might have noticed that we replaced fastai2
by git+https://github.com/fastai/fastai2.git
: this means that we are directly installing the latest source code found in the fastai2 GitHub repository.
fastcore
library using the very same method:
The command shown, here below, should be run after installing the fastai2 package from its repository in order to align the 2 versions:
either from terminal:
pip install git+https://github.com/fastai/fastcore.git
and from a Jupyter notebook cell:
!pip install git+https://github.com/fastai/fastcore.git
You may ask yourself why do we have to also install the fastcore library from GitHub given the fact that fastai2 will always install fastcore because it is part of its requirements. The answer relies on fact that fastai2 will install the latest version found in the pypi repository as opposed to the latest version found on GitHub. If the fastai2 GitHub version is using new features found in the fastcore latest GitHub version, it will lead to an incompatibility if we use fastai2 latest GitHub version combined with the fastcore pypi version: basically, if we had done so we would have been mixing 2 misaligned versions. Consequently, we will end up experiencing some issues, and some errors will ultimately surface. Therefore, it is better to follow the good practice by pip installing both of them using the method described here above.
3- Installing from GitHub- Editable package
The goal is to keep our library (that depends on fastai2) aligned to the fastai2 one hand, and to avoid to keep pip installing both fastai2 and fastcore every time a new version is released (this would have been the case had we used Method 2). Thankfully, there exists a magic method (Method 3) that consists in using a so-called an editable version of the target python package (i.e. fastai2 and fastcore)
Installing an editable version of fastai2 is pretty straightforward. All we have to do is to follow these 3 simple steps by running the following commands from a terminal console:
git clone https://github.com/fastai/fastai2
cd fastai2
pip install -e .
Basically, we are cloning the fastai2 repository, then hopping into the cloned fastai2 folder, and finally running the command to pip install the package (fastai2) from the current folder (hence the use of the dot .) as an editable version by using the -e switch.
We also have to follow the same steps for the fastcore library:
git clone https://github.com/fastai/fastcore
cd fastcore
pip install -e .
For those following along, and who are familiar with this option, they might notice there is a tiny discrepancy between this method and the one found on the fastai2 repository (shown here below):
git clone https://github.com/fastai/fastai2
cd fastai2
pip install -e ".[dev]"
The difference is the use of the [dev] part. Using the [dev]
flag issue an order to the installer to pip install all the packages listed under the dev requirements (dev_requirements = nbdev
). This means the nbdev
package (its a fastai package) will be installed from the pypi repository. Given the fact that nbdev is also under development, we may experienced some issues caused by a misalignment between the 3 different fastai packages being fastai2, fastcore, and nbdev. For that reason, it is desirable to also install the nbdev
package as an editable package as follow:
git clone https://github.com/fastai/nbdev
cd nbdev
pip install -e .
Now that we installed our editable package, how are we going to update it? The answer is very simple: we git pull our latest version from the corresponding GitHub repository. Therefore, for the fastai2 package, we just have to hop into its cloned forder and run the following command:
git pull
This command will both update our cloned version of the fastai2 repository and update our editable package had its version changed. We have to run the command, here above, for both fastai2 and fastcore.
Anaconda3-2019.10-Linux-x86_64.sh
by a more recent filename found in the [Anaconda installer archive] (https://repo.continuum.io/archive/) .
On Windows
- Download the Anaconda Windows installer
- Select Python 3.6 or higher
- Run the installer, and follow the instructions
On either Linux and Windows run the following commands:
conda create -n fastai2 python=3.6 anaconda
conda activate fastai2
- Install a python package (e.g. fastai2) from the pypi repository:
pip install fastai2
- Uninstall a python package (e.g. fastai2):
pip uninstall fastai2
- Show some information about a package (e.g. fastai2)
pip show fastai2
This is what it displayed on my local machine, at the moment I published the present post:
Name: fastai2
Version: 0.0.11
Summary: Version 2 of the fastai library
Home-page: https://github.com/fastai/fastai2
Author: Jeremy Howard, Sylvain Gugger, and contributors
Author-email: info@fast.ai
License: Apache Software License 2.0
Location: /home/farid/anaconda3/envs/fastai2/lib/python3.6/site-packages
Requires: scikit-learn, pandas, fastprogress, requests, scipy, pillow, torchvision, matplotlib, pyyaml, spacy, torch, fastcore
Required-by:
- Create a conda virtual environment called
fastai2
with python 3.6 version
conda create -n fastai2 python=3.6 anaconda
- Activate a conda virtual environment called
fastai2
conda activate fastai2
- List all the conda virtual environments
conda env list
- Install a python package (e.g. torch) from the conda repository:
conda install torch -c torch
-c : indicate which channel to use to install the corresponding package
- Uninstall a python package (e.g. torch):
conda uninstall torch
- List all the packages installed in the current conda virtual environment
conda list
- List all the packages installed that start with the prefix
fast
in the current conda virtual environment
conda list fast
Output on my machine indicating the package name, current installed version, and the channel:
# Name Version Build Channel
fastai2 0.0.12 dev_0 <develop>
fastcache 1.1.0 py36h7b6447c_0
fastcore 0.1.15 dev_0 <develop>
fastprogress 0.2.2 pypi_0 pypi
fastscript 0.1.4 pypi_0 pypi
Conclusion
In this article, I presented an overview describing 3 methods to install any package published on both pypi repository and GitHub. I used fastai2 library as use-case. However, the methods described here can be applied to any other package: the name fastai2
can be replaced by the corresponding package name such as torch
. I also explained the goal of each of these 3 methods, and which method to pick depending on your specific use-case.