Python3, choose Python to automatically install third-party libraries, and say goodbye to pip! !

1 Introduction

Continued from the previous article " Python3: I import all Python libraries with only one line of code in a low-key way! ", Xiaoyu found, don't say, there are really a lot of lazy people ~~

I don't know if they are all like Xiaoyu, and spend the rest of their time learning (chasing) and learning (sister).

In order to reflect Xiaoyu's laziness , Xiaoyu will share another show today:
Python automatically installs third-party libraries, completely freeing your hands!
insert image description here

2, pip manual installation

When it comes to installing third-party libraries in Python, our first reaction is that it must be installed in pip mode, no problem, it is necessary.
But think about it, if you change the computer (the local tyrant only changes the computer, and I change my friends ), there are so many third-party Python libraries, do you have to hit pip install again and again? ?

Are you still a primary school student and still need to practice typing on the keyboard ? ?

insert image description here

However, according to the process, we still have to first introduce the manual installation method of pip, and then introduce today's corner: automatic installation of third-party libraries,

2.1 Online installation

2.1.1 Default installation

A cliché, a direct command:

pip install  第三方库名称

2.1.2 Specified version installation

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

pip install  第三方库名称==版本号

E.g

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 the specific version number
  • ③<= Specify the highest version number
  • ④>= Specify the minimum version number
  • ⑤< no higher than a certain version number
  • ⑥> Not lower than a certain version number

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

pip install  'selenium>3.3'

2.2 Offline installation

Online installation of many python libraries will time out, or if there is no network, at this time, offline installation will be considered.

After all, it is not right to go fishing for work;
it is also wrong to occupy the company network to download the installation package.

Here are two very, very complete third-party library addresses , you are welcome, just take them away:

Xiao Diaosi : Brother Yu, how many steps are involved in offline installation?
Xiaoyu : There are three steps in total, please count with me:

Order

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

2.3 Set domestic source

Xiao Diaosi : Brother Yu, is there a way to install it quickly without downloading it locally.
Xiaoyu : This is a must, get in the car and watch the command.

If you don't want to download to the local, but also want to install directly with pip, consider the domestic source mirror.

Order

pip install plotly -i https://pypi.tuna.tsinghua.edu.cn/simple

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/
USTC mirror : https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/

Xiao Diaosi : Brother Yu, this method is good, but I don't want to enter the address every time. Can you make a one-and-done method ?
Xiaoyu : ... Your requirements are really high, but there are methods.

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

insert image description here

2.4 Uninstall and upgrade

2.4.1 Uninstall

There is installation, there is uninstallation, it
is also very simple, one command

pip uninstall 第三方库名称

2.4.2 Upgrade

Before upgrading, we need to check first, as follows:
1. Check the command

View installed libraries

pip list

②View the specified library

pip show 库名称

2. Upgrade command

① View the upgradeable library

pip list -o

②Upgrade command

pip install --upgrade 库名称

3, pip.main automatic installation

3.1 pip main installation

After a thousand calls, he came out, still holding the pipa without covering his face!
So much foreshadowing has been done before, the protagonist appears, you can applaud, don’t stop!
insert image description here
We have all experienced it, and then other people’s code ( refuse to take the blame ), continue to move forward, but we have not installed some libraries,
this time , it is very painful, keep prompting that there is no library, keep installing, and
think about it.
Xiao Diaosi : So is there a way to do it in one step? ?
Xiaoyu : It is necessary, get in the car and put the code.

Code display

# -*- coding:utf-8 -*-
# @Time   : 2021-08-03
# @Author : carl_DJ

"""如果引用的库未安装,则自动安装""" 
#为了明确异常信息,我们追加断言
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) to wait (mei).

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

3.2 os installation

Xiao Diaosi : Seeing this, I feel like I'm a jerk...
Xiaoyu : This is where, I use one line of code to install it automatically.
Little Diaosi : I'm sooooooooooooo...

Code display

# -*- coding:utf-8 -*-
# @Time   : 2021-08-03
# @Author : carl_DJ

import os

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

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

Little Diaosi : Oh, I'll go~~ Brother Yu, it's too good! ! !
Xiaoyu : I just want to akimbo for a while...

4. Summary

Seeing this, it's time to summarize again.
Let's see what the content shared today is:

  • pip online installation
  • pip offline installation
  • Domestic source mirror installation
  • Uninstall and upgrade
  • Python automatic installation

Think about it, come back and taste a product, that's what happened.
No matter what method is used to install, as long as it can only be installed on the line.
Just like this blog post by Xiaoyu " Python3: I import all Python libraries with only one line of code in a low-key way!" ", no matter how you import third-party libraries, as long as you can use them.
Therefore, we choose the method that suits us is the best.

Guess you like

Origin blog.csdn.net/wuyoudeyuer/article/details/119350545