DEV Community

Gabriel Wu
Gabriel Wu

Posted on • Updated on

A few pip tips

Photo by David Clode @ Unsplash

I record these tips mainly for future references, but I would be glad if you also find this post helpful. And if you have other pip tips, please comment and I will update this post.

1. Install packages with a specific version or version range

A specific version:

pip install foo==1.2.0

A version range:

pip install 'bar>=1.3.2,<=1.5.4'

If the same package with a different version has already been installed, the --force-reinstall should be used.

For multiple packages, it is better to use a requirements.txt file. The syntax is just the same as the single-line version above.


Then you can run:

pip install -r requirements.txt

2. Use a mirror when downloading packages

One-time use:

pip install -i <mirror-url> foo

Permanent use:

pip config set global.index-url <mirror-url>

3. Use python -m pip instead of plain pip

This will ensure that you are using the correct pip as long as you are using the correct python.

Commented by:

k4ml image

Discussion (1)

k4ml profile image
Kamal Mustafa

I'd also suggest using python -mpip instead. That one less surprise in case the pip you're using is using a different interpreter than the one you intended to.