Still manually installing Python modules with pip? Today I will teach you how to install third-party libraries fully automatically, completely freeing your hands

preamble

Brothers, in order to help you catch fish more efficiently, today I will share a show operation, Python automatically installs third-party libraries!

In order to reflect the editor's achievements in laziness, today I will share another coquettish operation: Python automatically installs third-party libraries, fully automatic without you needing to move!

pip manual install

When we talk about which module to install in Python, our first reaction is to enter cmd with win+r, and pip install to install it~
Please add a picture description
But if you change your computer, there are so many third-party Python libraries, do you have to type pip install again and again? ?

However, according to the process, we still have to introduce the manual installation method of pip first

1. Online installation

It's a cliché, just a direct command:

pip install  第三方库名称
Install the specified version

Specify the version installation command of the third-party library:

pip install  第三方库名称==版本号
For example
pip install  selenium==3.3

There are several ways to specify the version number:

① If not specified, the latest version will be installed by default
②== specify a specific version number
③<= specify the highest version number
④>= specify the minimum version number
⑤< not higher than a certain version number
⑥> not lower than a certain version number

Notice

Here is a reminder, if you do not specify a specific version number, you need to use quotation marks (' '), as follows:

pip install  'selenium>3.3'

2. Offline installation

The online installation of many python libraries will time out, or if there is no network, at this time, offline installation will come to mind.

After all, it’s wrong to fish at work, and it’s wrong to occupy the company’s network to download and install packages~

Here are two very, very complete third-party library addresses recommended, don’t be shy, just take them away:

Health website: https://www.lfd.uci.edu/~gohlke/pythonlibs/
Pypi: https://pypi.org/
Please add a picture description
Offline installation, a total of three steps:

  • Step 1: Log in to the Python third-party library website;
  • Step 2: Download the files of the third library to the local
  • Step 3: Install locally

Order

pip install C:\Project\pyRXP-2.2.0-cp35-cp35m-win_amd64.whl

The domestic source address is as follows:

Alibaba Cloud Mirror: http://mirrors.aliyun.com/pypi/simple/
Tsinghua University Mirror: https://pypi.tuna.tsinghua.edu.cn/simple/
Douban Mirror: http://pypi.doubanio.com /simple/
University of Science and Technology of China Mirror: https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/
Some friends have to say: This method is good, but I don’t want to enter the address every time, I can Can't come up with a one-and-done solution?

The requirements are really high, but there are ways~

Just under your current project, create a pip.ini file and call the content of this file directly.

4. Uninstall and upgrade

uninstall

If there is installation, there is uninstallation. It is also very simple, just one command.

pip uninstall 第三方库名称
upgrade

Before upgrading, we need to check, as follows:

1. View commands

View installed libraries

pip list

View the specified library

pip show 库名称

2. Upgrade command

View upgradeable libraries

pip list -o

upgrade command

pip install --upgrade 库名称

pip.main auto-install

1. pip main installation

We have all experienced it, following other people's code (refused to blame the man), and moving on, but there are some libraries that we have not installed.

At this time, it is very painful. It keeps reminding that there is no library, and keeps installing. It is hard to think about it.

So is there a way to do it in one step? ?

Necessary, get in the car and put the code.

"""如果引用的库未安装,则自动安装""" 
#为了明确异常信息,我们追加断言
try:
    import requests
    import pandas as pd
    from bs4 import BeautifulSoup
    import jieba
    import jieba.analyse
    import matplotlib.pyplot as plt
    from wordcloud import WordCloud,STOPWORDS
    import numpy as np
    from PIL import Image
# 使用pip.main()方法进行依赖库的安装(例举几个常用的库)   
except  ImportError:
    import pip
    pip.main(["install", "--user", "requests","beautifulsoup4","matplotlib","wordcloud","pandas","pillow"])
    import requests
    import pandas as pd
    from bs4 import BeautifulSoup
    import jieba
    import jieba.analyse
    import matplotlib.pyplot as plt
    from wordcloud import WordCloud,STOPWORDS
    import numpy as np    
    from PIL import Image 

Execute this code, and the rest is to wait (liao) wait (mei).

Because it has already been installed, it will not be executed.

2. OS installation

code display

import os

#需要安装的库
libs = ["requests","beautifulsoup4","matplotlib","wordcloud","pandas","pillow"]

#循环遍历安装
for lib in libs:
    os.system("pip install " + lib)

Summarize

It's time to summarize again, let's take a look at what we shared today:

Pip online installation
pip offline installation
Domestic source mirror installation
Uninstall and upgrade
Python automatic installation
Come back and think about it, that's the case~

No matter what method is used to install, as long as it can only be installed.

Therefore, the method we choose, the one that suits us is the best.

Guess you like

Origin blog.csdn.net/weixin_72934044/article/details/128215657