程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Foundation: 1 Quickly handle Python libraries and environments

編輯:Python

Best practices

Use conda+pip Command to manage the library and environment , Do not install in the system python The environment , It's based on conda Virtual environment for operation , Environmental isolation can be achieved , Compatible with different python Version of the program runs at the same time .python Program = I wrote my own program + Third party package . We are github I often see it on the python programmatic readme file , There are two lines of code :

pip install -r requirements.txt
python main.py

conda And pip The main benefits of cooperation :
1. Easy to use
2. Support for mainstream operating systems :win,mac,linux
3. Support virtual environment , Quickly switch between multiple versions of python、 package
4. The installation success rate of the third-party library is high
Why not use it directly pip,easy_install,virtualenv etc. ?
Because these tools are focused python The system of , Ignoring non python We rely on tools , Such as mkl,LLVM etc. .
conda、pip function
pip:python Package manager
Conda: Package manager + Environment manager .
The comparison chart is as follows :

conda There are a lot of benefits to , The drawback is that the supported packages do not pip There are many kinds of . The nature of his dependency checks , Allows you to install other python When the package , Automatically find the dependent package for you , Installed together . When installing some packages that depend on a lot , Such as paddle-paddle, Use conda Success rate of installation , It's better than using pip The success rate of direct installation should be high . The reason lies in conda Of SAT Algorithm , Here's the picture :

Why not use it directly conda, And use it pip?

Niche bag , Still use pip To install . To sum up :

conda + pip Set up process

conda refer to miniconda, Go to official website (https://docs.conda.io/en/latest/miniconda.html) Download it . Then install it into the system , Remember to tick at the end , Configure environment variables .
About miniconda Version problem , It is OK to choose a higher version , Because we need to create a virtual environment , You can specify what you need python edition .
After installation , Use anaconda power shell Tools to test . Note that it must be power shell.
Check conda Version of the directive .

conda --version

conda Common orders

List all environments :conda env list
Create an environment :conda create --name Name of the environment ( english )
Enter the illusion :conda activate Name of the environment
Out of the environment :conda deactivate
Delete environment :conda remove --name Name of the environment --all all Is to delete clean
Create the specified version of python Environmental case :conda create --name Name of the environment python=3.7

After creating the environment , Activate the environment , Then input python, Check if it is as you set it python Environment created by version . After input exit() sign out .

Tips :python It's not good if the version is too high , Some packages are incompatible ,3.7,3.8 Good compatibility .

Install third party package

At home conda Speed up mirroring

because conda and pip Many of our resource servers are located abroad , Inevitable slow download , Here we need to change to the domestic accelerated image .
You can use the mirror station of open source software of Tsinghua University :https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
Follow the steps inside , It can be replaced conda It's the source of my life

windows in pip Source

Temporary use : pip install -i https://pypi.tuna.tsinghua.edu.cn/simple { Package name }

Permanent use :
First step : stay C:\Users\Administrator Under the table of contents establish pip Folder
The second step : Under the folder created in the first step (C:\Users\Administrator\pip) establish pip.ini file
The third step : Notepad edit save pip.ini The contents of the document are as follows :

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn

mac in pip Source

Temporary use : pip install -i https://pypi.tuna.tsinghua.edu.cn/simple { Package name }
Permanent use :

Execute the following statement

cd ~
mkdir .pip
cd .pip
vi pip.conf

pip.conf write in

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn

preservation pip.conf


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved