Python third-party library installation method summary, python Installation Method

Source: Internet
Author: User

Python third-party library installation method summary, python Installation Method

Python is an elegant language with simple syntax and powerful functions. Of course, a wide range of third-party libraries can accelerate development. So the question is, how to install these third-party libraries (packages?

There are not many ways to install third-party libraries. The following describes some tips.

Source code Installation
Many third-party libraries are open-source, and almost all source code can be found on github or pypi. The source code format is generally zip, tar.zip, and tar.bz2. Decompress these packages and enter the decompressed folder. A setup. py file is usually displayed. Open the command line to enter the folder. Run

python setup.py install

This command can install the third library to the system, that 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 the package is installed in the virtulenv environment, it is installed in the site-packages/directory of the created virtual environment. It's easy to uninstall these libraries. Go to site-packages and simply delete the library files. Of course, these installation may be a little troublesome. it is not convenient to download, decompress, run and install, and uninstall them. Is there a tool that can help you manage these libraries?

Package Manager (pip and easy_install)
Many programming languages now have package managers, such as Ruby gem and nodejs npm. Of course, Python is no exception. It has a well-known pip and easy_install.

The pypi mentioned above is the source of some third python libraries. If you use pip or easy_install to install the module, you will search for the source and automatically download and install it. For example, to install the flask framework, run the following command:

pip install flask

Or

easy_install flask

It's easy, just a simple command. It is also convenient to uninstall. For example, we need to uninstall flask.

pip uninstall flask

View the installed packages, including the built-in and manually installed packages.

pip list

You can also search for packages.

pip search flask

You can also redirect the library used by the output project.

pip freeze > requirements.txt

In this way, the third-party libraries in this environment will be redirected 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, can it be offline? Of course you can. The first step of pip install is to find the package on pypi and download it to the local device. If the network is poor, you can first create a local warehouse to download frequently used packages offline. For example, you can download the flask source code.

pip intall flask-master.zip

You can also install it.

Virtual Environment Installation
Of course, the method described above is also very common and not very skillful. The following describes some techniques for understanding the principles and solving unconventional problems.

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

Windows users may be very happy and do not need to pay attention to this issue. Of course, windows also has its own problems. Installing python on windows is generally a compiled Binary Package exe executable file. Usually there are 32-bit and 64-bit python. For third-party libraries, 32 and 64 are not significantly different. However, for some python libraries written in C, such as mysqldb, PIL, and pillow, the following error occurs when pip or easy_install is used:

Fixing python error: Unable to find vcvarsall.bat

The reason is probably that some C compiler stuff is missing on windows. The source code needs to be compiled before it can be installed. At this time, windows is very hard to install. Of course, there are some good people who will help you compile some frequently used libraries into exe executable files and put them on this site. You only need to find one-click installation for the version.

However, there is another problem: Download the exe file and run the installation. The library is installed in the site-package directory of the system. If I create a venv virtual environment in windows, isn't installation a stop?

Don't worry. The true skill in this article is to solve this problem. We will install 64-bit mysqldb. Download mysql1_python1_1.2.5.win1_amd641_py2.7.exe on windows and run 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 to venv.

With the above three methods, the installation of third-party libraries on almost all python platforms is covered. However, even though Python is a cross-platform and development environment, we should try to use Linux or MacOS as much as possible. These two systems have better tools and save a lot of inexplicable 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.