Home  >  Article  >  Backend Development  >  Python third-party library installation and common problems

Python third-party library installation and common problems

高洛峰
高洛峰Original
2016-11-22 15:43:251631browse

Source code installation

Almost all third-party Python libraries can find their source code on github or pypi. Source code package formats include zip, tar.zip, and tar.bz2. Unzip these packages and enter the unzipped folder. There will usually be a setup.py file. Open a command line and enter the folder. Run the following command to install this third library into the system:

python setup.py install

Or use pip, there is no need to decompress: pip install package.zip

Package manager installation

Many programming languages ​​now come with packages Manager, such as Ruby's gem, nodejs' npm.

In Python, installing third-party modules is done through the setuptools tool. Python has two package management tools that encapsulate setuptools: easy_install and pip. Currently, it is officially recommended to use pip.

It is very convenient to use easy_install and pip to install third-party libraries. Their principle is actually to download it from the official source of Python pypi.python.org/pypi to the local, and then unpack and install it.

The basic operating commands are as follows:

# 安装package
pip install packagename

# 卸载package
pip uninstall packagename

# 查看所安装的package
pip list

# 将项目依赖的库重定向输出到文件,cd到项目根目录
pip projectname > requirements.txt

# 他人安装项目的依赖库
pip install -r requirements.txt

Common pip commands can be viewed by entering pip -h on the command line
pip command -h to view how to use the command

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion
  help                        Show help for commands.

FAQ

The official pypi is unstable and very slow It can’t even be accessed

Solution 1:
Use the source code installation method, download it from github or other libraries, and install it with python setup.py install. For details, see [Source code installation] above. Solution 2:

Specify the source manually. After pip followed by -i, the command is as follows:

pip install packagename -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

pipy domestic mirrors currently include:

Douban http://pypi.douban.com/simple/

Alibaba Cloud http://mirrors.aliyun.com/pypi/simple/
University of Science and Technology of China https://pypi.mirrors.ustc.edu.cn/simple/
Tsinghua University https://pypi.tuna.tsinghua.edu.cn/simple/
Huazhong University of Science and Technology http://pypi.hustunique .com/
Shandong University of Technology http://pypi.sdutlinux.org/

Some packages can be installed on this computer, but cannot be installed on another computer

See whether the setuptools and pip versions are consistent, and upgrade to the latest version

pip install setuptools -U 
pip install pip -U

The error "error: Microsoft Visual C++ 10.0 is required (Unable to find vcvarsall.bat)." occurs when installing some packages.

The reason is probably that some C compilers are missing on Windows.

Solution 1: Install VC or VS, this method sometimes works, sometimes not.

Solution 2: A simpler solution: download the package in whl format, and then install it with pip. Take the numpy package as an example:

whl format download address: http://www.lfd.uci.edu/~gohlke/pythonlibs/

# Enter the full path where the whl file is located

pip install D:pythonnumpy-1.9 .2+mkl-cp33-none-win_amd64.whl

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn