This article source address Http://www.jianshu.com/p/9acc85d0ff16
Python
is an elegant language, concise grammar, 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, almost all can github
find the source code on or PyPI. Find the source format is probably zip
all tar.zip
, tar.bz2
the 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 (on the command line)
Python setup.py Install
This command, you can install the third library into the system, that is, your Python
path, windows
presumably in C:\Python\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 an virtulenv
environment, the package is installed in a directory under the established virtual Environment site-packages/
. To uninstall these libraries is also very simple, enter 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, for example Ruby
gem
nodejs
npm
. Python
of course, there is no exception, pip
with easy_install
the famous and.
As mentioned above pypi
is the python
source of some third library, use pip
or easy_install
install the module, will search this source, and then automatically download the installation. 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 uninstallflask
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 library in the 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, pip install
the first step is to pypi
look for the package, and then download it locally. If the network is not good, you can first build a local warehouse, the commonly used packages offline download. such as flask
the source code can be downloaded
Pip Intall flask-master.zip
It can also be installed.
The latest version of PIP also supports Git installation and only needs to use its git address.
Pip Install Git+https://github.com/kennethreitz/requests.git
But this installation is quite slow. It is the original clone to a local temporary directory, and then install the ZIP package.
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 build a virtual environment virtualenv
, for example, to create a venv
virtual environment. We just need to source
go in and we can use pip
the installation. However, sometimes, even in a virtual environment, using sudo
the prefix ( windows
ignore), then the installed library is not in the virtual environment, but is installed in the directory under the system site-package
.
windows
Users may be happy and do not need to be aware of this issue. Of course, windows
it also has its own problems. On the windows
installation python
, it is generally a compiled binary package exe
executable file. Usually there are 32-bit and 64-bit python
. For third-party libraries, there would not be much difference between 32 and 64. However, for some C
written python
libraries, such as,, mysqldb
PIL
pillow
will find the use pip
or easy_install
the way will be error:
Fixing Python error:unable to find Vcvarsall.bat
The reason is probably windows
missing some C
compiler stuff. The source code needs to compile to install, at this time windows
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
files, run the installation, this library is installed in the directory of the system site-package
, if I am windows
building a venv
virtual environment, so that the installation is not a break food?
Don't worry, the real trick in this article is to solve the problem. Let's install the 64-bit mysqldb
. Download MySQL?python?1.2.5.win?amd64?py2.7.exe
It first 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 a venv
virtual environment, the mysqldb
library is installed to the venv
next.
With the top three ways, the third- python
Party library installation covers almost all platforms. However, even though it Python
is a cross-platform, development environment, it is best to use Linux
or MacOS
, the two systems have better tools and save a lot of puzzling problems.
Python third-party library installation Tips