Best Method for installing library or packages using pip in Python?
The standard packaging tools are all designed to be used from the command line.
The following command will install the latest version of a module and its dependencies from the Python Packaging Index:
python -m pip install SomePackage
Note: For POSIX users (including Mac OS X and Linux users), the examples in this guide assume the use of a virtual environment.
For Windows users, the examples in this guide assume that the option to adjust the system PATH environ-ment variable was selected when installing Python.
It’s also possible to specify an exact or minimum version directly on the command line. When using comparator operators such as >, < or some other special character which get interpreted by shell, the package name and the version should be enclosed within double quotes:
python -m pip install SomePackage==1.0.4 # specific version python -m pip install "SomePackage>=1.0.4" # minimum version
Normally, if a suitable module is already installed, attempting to install it again will have no effect. Upgrading existing modules must be requested explicitly:
python -m pip install – upgrade SomePackage
More information and resources regarding pip and its capabilities can be found in the Python Packaging User Guide.
Creation of virtual environments is done through the venv module. Installing packages into an active virtual environment uses the commands shown above.