Sobes.tech
Junior — Middle

How can you install a specific version of a package via pip?

sobes.tech AI

Answer from AI

To install a specific version of a package using pip, use the syntax with the version specified after the package name with ==.

Example:

pip install package_name==1.2.3

Where 1.2.3 is the desired version.

You can also use other operators to specify version ranges:

  • >=1.2.3 — version 1.2.3 or higher
  • <=1.2.3 — version 1.2.3 or lower
  • ~=1.2.3 — compatible version, e.g., >=1.2.3 and <1.3.0

If you need to lock versions for the entire project, you usually create a requirements.txt file with entries like:

package_name==1.2.3
another_package==4.5.6

And install all dependencies with the command:

pip install -r requirements.txt

This ensures that the specified versions are installed.