How do I install a specific, older version of a Python package using PIP? Will it matter if I have another version already installed?
We can install an older version of a specific package by using the following syntax in our PIP command:
pip install requests==2.30.0
Note that, on Windows, the package name and version should be surrounded by quotation marks:
pip install "requests==2.30.0"
If a newer version of the package is already installed on our system or in our current virtual environment, we will need to add the
--force-reinstall flag to uninstall that version first:
pip install --force-reinstall requests==2.30.0
To ensure that future users of our project install the correct package versions when setting it up, we should create a requirements file. This is a file in our project’s root directory named
requirements.txt, which contains a list of requirement specifiers (i.e. package names and versions) on which our project depends. For this example, our requirements file would have one line:
We would then be able to install the correct version of this and any other packages added by running the following PIP command:
pip install -r requirements.txt
Again, we may need to provide the
--force-reinstall flag when running this command.
To avoid changing the packages on our entire system for individual Python projects, we can use a virtual environment. This is the recommended way to deal with Python dependencies in most circumstances.