no classification
no tag
no datas
posted on 2023-06-06 11:12 read(385) comment(0) like(0) collect(0)
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!
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 ? ?
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,
It's a cliché, just a direct command:
pip install 第三方库名称
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:
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'
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
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:
There is installation, there is uninstallation,
it is also very simple, a command
pip uninstall 第三方库名称
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 库名称
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!
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.
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...
Seeing this, it's time to summarize again.
Let's take a look at what we shared today:
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.
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.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!