How to install Python third-party libraries

Source: Internet
Author: User
Library is a general term, the general value as a file form of the existence of the module and a folder in the form of the composition of the package, here is a python third-party library installation method summary, including source installation, Package Manager installation and virtual environment related installation three ways to explain

Python is an elegant language, concise syntax, powerful features. Of course, the rich third-party library, more able to accelerate the development. So the question is, how do you install these third-party libraries (packages)?

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

SOURCE Installation
Many third-party libraries are open source, and almost all can be found on GitHub or PyPI. Find the source format is probably the zip, tar.zip, tar.bz2 format of the compressed package. Unpack the packages and go to the Unpacked folder, and you'll usually see a setup.py file. Open the command line and go to the folder. Run

Python setup.py Install

This command, you can install the third library into the system, that is, your Python path, Windows is probably in the 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, the package is installed in the site-packages/directory under the virtual environment that was created. To uninstall these libraries is also very simple, into the site-packages, directly delete the library file is OK. Of course, these installations may be a little cumbersome, need to download, unzip, and then run the installation, uninstall is not very convenient. Is there a tool that can help manage these libraries outside?

Package Manager (PIP and Easy_install)
Many programming languages now come with package managers, such as the Gem,nodejs npm of Ruby. Python is certainly no exception, with the famous Pip and Easy_install.

The pypi mentioned above is the source of some Python third libraries, using PIP or Easy_install to install the module, search for the source, and then download the installation automatically. For example we need to install the Flask framework specific commands as follows:

Pip Install flask

Or

Easy_install Flask

Simple, a simple command is done. Uninstall is also very convenient, for example we need to uninstall flask

Pip Uninstall flask

View the installed packages, including the system's own and manually installed

PIP List

You can also search for packages

Pip Search Flask

You can also redirect the libraries that the output project uses.

Pip Freeze > Requirements.txt

This will redirect the third-party libraries in that environment to the Requirements.txt file, and if you give others a dependency on the project, 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, can you leave floss? Of course, 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, the commonly used packages offline download. For example, you can download flask source code

Pip Intall flask-master.zip

It can also be installed.

Virtual Environment Related installation
Of course, the above mentioned, in fact, is also very common, but also not what skills. Here are some tips for understanding the principles and solving unconventional problems.

Using Python, we would like to use virtualenv to build a virtual environment, such as building a venv virtual environment. We just need the source to go in and we can use PIP to install it. However, sometimes, even in a virtual environment, using the sudo prefix (Windows ignore), the installed library is not in the virtual environment, but is installed in the system under the Site-package directory.

Windows users may be happy and don't need to be aware of this issue. Windows, of course, also has its own problems. Installing Python on Windows is generally a compiled binary package exe executable file. Typically there are 32-bit and 64-bit python. For third-party libraries, there would not be much difference between 32 and 64. But for some C-written Python libraries, such as Mysqldb,pil, pillow, you'll find that using PIP or Easy_install will cause an error:

Fixing Python error:unable to find Vcvarsall.bat

The reason is probably missing some C compiler stuff on Windows. The source code needs to compile to install, at this time Windows is very bitter force. Of course, there are some good-hearted people, help you to compile some of the commonly used libraries into EXE executable files, put to this site. You just need to look for the version, download one-click Install.

However, there is a problem, download EXE file, run the installation, this library is installed in the system's Site-package directory, if I set up a venv virtual environment in Windows, so installation is not a break?

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

Easy_install Mysql‑python‑1.2.5.win‑amd64‑py2.7.exe

Perfect solution, in the VENV virtual environment, the MYSQLDB Library was installed under VENV.

The

has the top three ways to almost cover the third-party library installation of all Python platforms. However, although Python is a cross-platform, development environment, or as much as possible using Linux or MacOS, these two systems have better tools and save a lot of puzzling problems.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.