-V option displays the associated Python version. Consider the following examples:
If the command is simply
pip without a version specifier, running pip -V will help determine which Python version it is associated with. Using the wrong version might result in installing libraries in an unintended environment.Installing Packages
To install a package with PIP, use the following syntax. For example, installing the popular Flask web framework can be done as:site-packages folder where packages are stored. For instance, if Flask is installed using pip for Python 2, the installation path might look like this:
- For 32-bit packages
- For 64-bit packages
- Python 2.7:
/usr/lib64/python2.7/site-packages - Python 3.6:
/usr/lib/python3.6/site-packages
- Python 2.7:
pip show command:
Importing Packages and sys.path
When you import a package using theimport statement, Python searches through directories listed in sys.path. To inspect these directories, run:
sys.path can help identify whether the package was installed in a different location or for another Python version.
Managing Dependencies with requirements.txt
For larger applications that require multiple packages, it is common to list all dependencies in a file namedrequirements.txt. You can then install all dependencies simultaneously by running:
requirements.txt file includes package names along with specific versions to avoid compatibility issues. For example:
Upgrading and Uninstalling Packages
When a new version of a package becomes available, you can upgrade it using:Additional Package Management Tools
Apart from PIP, Python supports other package management tools such as easy_install. Originally, easy_install was used in combination with setuptools to package Python code into a zipped format known as “eggs” (similar to JAR files in Java). Alternatively, you can install or place the egg file in a directory accessible to Python. Another packaging format is the wheel (with the.whl extension). Unlike eggs, wheels require installation (unpacking) before use. They can be installed with a command like:
Summary
Python package management with PIP enables you to install, upgrade, and uninstall packages while managing dependencies through arequirements.txt file. These tools are essential for maintaining a consistent development environment, particularly when working with multiple Python versions or architectures (32-bit vs 64-bit).
Next, apply what you’ve learned by practicing package management with Python. In the exercise, you will:
- Identify the list of packages to install.
- Locate where packages are installed.
- Work with different versions of PIP.
- Upgrade and uninstall packages.
- Utilize
requirements.txtfor dependency management.