Home  >  Article  >  Backend Development  >  How to install Python third-party libraries

How to install Python third-party libraries

高洛峰
高洛峰Original
2017-03-03 15:04:422105browse

Library is a general term, generally used as a synthesis of modules that exist in the form of files and packages that exist in the form of folders. Here is a summary of the installation methods of Python third-party libraries, including source code installation, package manager installation and virtualization Explanation of three methods of environment-related installation

Python is an elegant language with concise syntax and powerful functions. Of course, rich third-party libraries can speed up development. So the question is, how to install these third-party libraries (packages)?

There are not many ways to install third-party libraries. Here are some tips.

Source code installation
Many third-party libraries are open source, and the source code can almost all be found on github or pypi. The source code formats found are probably zip, tar.zip, tar.bz2 format compressed packages. Unzip these packages and enter the unzipped folder. You will usually see a setup.py file. Open a command line and enter the folder. Run the command

python setup.py install

to install this third library into the system, which is your Python path. Windows is probably in C:\Python2 .7\Lib\site-packages.
Linux will be in /usr/local/lib/python2.7/dist-packages.
Mac should be in /Library/Python/2.7/site-packages. If installed in a virtulenv environment, this package will be installed in the site-packages/ directory of the created virtual environment. It is also very simple to uninstall these libraries. Go to site-packages and delete the library files directly. Of course, these installations may be a little troublesome. They need to be downloaded, decompressed, and then installed. Uninstalling is not very convenient. Is there a tool that can help outsiders manage these libraries?

Package Manager (pip and easy_install)
Many programming languages ​​now come with package managers, such as Ruby’s gem and nodejs’ npm. Python is of course no exception, with its famous pip and easy_install.

The pypi mentioned earlier is the source of some python third libraries. Use pip or easy_install to install the module. This source will be searched and then automatically downloaded and installed. For example, we need to install the flask framework with the following specific commands:

pip install flask

or

easy_install flask

Simple Well, a simple command will do it. Uninstalling is also very convenient. For example, we need to uninstall flask

pip uninstall flask

to view the installed packages, including those that come with the system and those that are manually installed

pip list

You can also search for packages

pip search flask

You can also redirect the output of libraries used by the project.

pip freeze > requirements.txt

This will redirect the third-party libraries in the environment to the requirements.txt file. If you install project dependencies for others, you only need to run :

pip install -r requirements.txt

It’s very convenient. Of course, sometimes our network is not so smooth. Pip is installed online, but can it be offline? Of course you can. The first step in pip install is to find the package on pypi and download it locally. If the network is not good, you can first build a local warehouse and download commonly used packages offline. For example, you can download the source code of flask

pip intall flask-master.zip

and you can also install it.

Virtual environment related installation
Of course, the above introduction is actually a very common and common method, and it is not a skill. Introduced below are some techniques that require understanding the principles and solving unconventional problems.

Using Python, we would like to use virtualenv to create a virtual environment, such as creating a venv virtual environment. We only need to source it and install it using pip. But sometimes, even in a virtual environment, if the sudo prefix is ​​used (ignored by windows), the installed library is not in the virtual environment, but is installed in the site-package directory under the system.

Windows users may be very happy and do not need to pay attention to this problem. Of course, windows also has its own problems. When installing python on windows, it is usually a compiled binary package exe executable file. There are usually 32-bit and 64-bit versions of Python. For third-party libraries, there won't be much difference between 32 and 64. However, for some python libraries written in C, such as mysqldb, PIL, and pillow, you will find that using pip or easy_install will report an error:

Fixing python error: Unable to find vcvarsall.bat

The reason is probably windows There is some missing C compiler stuff. The source code needs to be compiled before installation, which makes Windows very difficult. Of course, there are some kind people who help you compile some commonly used libraries into exe executable files and put them on this site. You just need to find the version, download it and install it with one click.

However, there is another problem. Download the exe file and run the installation. This library is installed in the site-package directory of the system. If I create a venv virtual environment in Windows, the installation will not be complete. What?

Don’t worry, the real technique of this article is to solve this problem. Let's install the 64-bit mysqldb. First download MySQL‑python‑1.2.5.win‑amd64‑py2.7.exe on Windows and use the command line to enter the virtual environment venv. Then run

 easy_install MySQL‑python‑1.2.5.win‑amd64‑py2.7.exe

Perfect solution, in the venv virtual environment, install the mysqldb library under venv.

With the above three methods, the installation of third-party libraries for almost all python platforms is covered. But having said that, even though Python is a cross-platform development environment, it is better to use Linux or MacOS as much as possible. These two systems have better tools and save many inexplicable problems.

For more articles related to the installation methods of Python third-party libraries, please pay attention to the PHP Chinese website!

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