DEV Community

Gabriel Wu
Gabriel Wu

Posted on • Edited 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
Enter fullscreen mode Exit fullscreen mode

A version range:

pip install 'bar>=1.3.2,<=1.5.4'
Enter fullscreen mode Exit fullscreen mode

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.

foo==1.2.0
bar>=1.3.2,<=1.5.4
foobar
Enter fullscreen mode Exit fullscreen mode

Then you can run:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

2. Use a mirror when downloading packages

One-time use:

pip install -i <mirror-url> foo
Enter fullscreen mode Exit fullscreen mode

Permanent use:

pip config set global.index-url <mirror-url>
Enter fullscreen mode Exit fullscreen mode

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:

Top comments (1)

Collapse
 
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.