News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

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

posted on 2023-06-06 11:12     read(385)     comment(0)     like(0)     collect(0)


1 Introduction

Continuation of the previous article " Python3: I import all Python libraries with only one line of code in a low-key manner! ", Xiaoyu found out, let alone, there are really many lazy people ~~

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

In order to reflect Xiaoyu's achievements in laziness , Xiaoyu shared another coquettish operation 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 your computer ( only local tyrants change computers, and I change friends ), there are so many third-party Python libraries, do you have to tap 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 introduce the manual installation method of pip first, and then introduce today's angle: automatically install third-party libraries,

2.1 Online installation

2.1.1 Default installation

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

pip install  第三方库名称

2.1.2 Specified version installation

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 one 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

Note
Here is a reminder that 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

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 is wrong to go fishing at work;
it is also wrong to occupy the company 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:

Little Diaosi : Brother Yu, how many steps are there for 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 quickly install it without downloading it locally
?

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

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 have to enter the address every time. Can you come up with a once-and-for-all method?
Xiaoyu : …Your requirements are really high, but there are methods.

Just under your current project, create a pip.ini file and call the content 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, a command

pip uninstall 第三方库名称

2.4.2 Upgrade

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

①View the installed library

pip list

View the specified library

pip show 库名称

2. Upgrade command

① View upgradeable libraries

pip list -o

Upgrade command

pip install --upgrade 库名称

3. pip.main is automatically installed

3.1 pip main installation

After a thousand calls, he came out, still hugging the pipa without covering his face!
So much preparation has been done before, the main character comes on stage, you can applaud, don’t stop!
insert image description here
We have all experienced it, and then follow other people’s code ( refusing to blame the man ), and continue to move forward, but we have not installed some libraries.
At this time , It’s very painful, it keeps prompting that there is no library, and it keeps installing,
it’s hard to think about it.
Little Diaosi : Then is there a way to get it done in one step? ?
Xiaoyu : Yes, 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) wait (mei).

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

3.2 os installation

Xiao Diaosi : Seeing this, I feel like I'm crazy...
Xiaoyu : This is where it goes. I use one line of code to install it automatically.
Little Diaosi : I don't know what to say...

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, you're awesome! ! !
Xiaoyu : I just want to akimbo for a while...

4. Summary

Seeing this, 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 is installed automatically

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



Category of website: technical article > Blog

Author:Soledad

link:http://www.pythonblackhole.com/blog/article/80162/77e822aa7b73aa281cc6/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

0 0
collect article
collected

Comment content: (supports up to 255 characters)