How to install python and third-party packages

How to install python and third-party packages

1 Introduction to Python

Python is a very powerful language and we can do a lot of pretty advanced stuff with it.

Python provides us with a very complete basic code base, covering a lot of content such as network, file, GUI, database, text, etc. Developed in Python, many functions do not have to be written from scratch, and can be directly used off-the-shelf.

The following will introduce how to install Python and how to obtain third-party installation packages and use them. Of course, you can also skip all the installation and configuration work and use an integrated Python working environment such as Anaconda. For details, see 5 Python Integrated Environment Anaconda.

2 Python installation and verification

Currently, there are two versions of Python, one is version 2.x and the other is version 3.x, these two versions are incompatible. First, check your Windows version (64-bit or 32-bit), then download the 64-bit installer or 32-bit installer for Python 3.5 or Python 2.7 from the official Python website https://www.python.org/ . At present, the latest version of Python has two versions, 3.6.1 and 2.7.13. Select the required version to download.


For the 2.7.13 version of Python, install it by default. After selecting the file directory to be installed, check the option of adding Python to the environment variable, and wait for the program installation to complete.





After the installation is successful, open the command prompt window and type python, there are generally two situations:

In the first case, the Python installation is successful


Seeing the above situation means that your Python has been installed successfully, and python has also been added to the environment variable. The prompt >>> you see means that we are already in the Python interactive environment and can enter Any Python code will get the execution result immediately after pressing Enter.

Case 2: An error occurred: 'python' is not an internal or external command, nor a runnable program or batch file.


The above situation occurs because Windows will search for python.exe according to the path set by a Path environment variable. If it is not found, an error will be reported. If you missed to check Add Python3.5 to PATH during installation, you have to manually add the path where python.exe is located to Path. If you do not add environment variables, it is recommended to reinstall Python once, and remember to check the option to add environment variables.

3 Python third-party package installation and verification

Third-party package installation using the Pip tool

The Pip tool is the third package installation tool that comes with Pytho. It has been installed during the python installation process, and no independent installation is required. Attach the python third-party installation package address: https://pypi.python.org/pypi . Search the web page for the package name you want to install, and you can quickly get the package you want.

When choosing a third-party package, you need to choose a different third-party package according to your python version and the number of computer bits (32bit or 64bit). You may get the installation package file in two data formats, the first one is a file ending in .tar.gz that can be decompressed with a compression tool, the compressed file contains the main entry setup.py file, and the other one is ending in .whl file, the whl format is essentially a compressed package that contains .py files and compiled .pyd files.

The basic installation methods of these two third-party packages are as follows. First, for the .tar.gz file, the following steps are basically required:

1. Download the third-party package and unzip it.

2. Then use cd to enter the path of the third-party package. Use shift+right key to quickly open the command window in this directory.

3. Enter Python setup.py build

4. Enter python setup.py install

Similar for .whl files, just replace 3 and 4 in the above steps with:

5. Enter pip install xxxx.whl

The following will use the .tar.gz file of BeautifulSoup4 and the .whl file of matplotlib for installation demonstrations. Please follow the steps above to check.

First, for the beautifulsoup4-4.6.0.tar.gz file, decompress it and enter its decompression directory, open the command line and perform command installation.






The same is true for the matplotlib-2.0.2-cp27-cp27m-win32.whl file, enter the file download directory, open the command line and enter pip install matplotlib-2.0.2-cp27-cp27m-win32.whl, it can be installed automatically.

There is a problem to pay attention to here. If your computer user name is Chinese, the following encoding problems may occur:

UnicodeDecodeError: 'ascii' codec can't decodebyte 0xdb in position X: ordinal not in range(XXX)

Here is a solution: open the Lib folder in the installation directory to find the site.py file C:\Python27\python2.7.13\Lib\site.py, add a few lines of code after import and save (modify the default encoding):

import sys

reload(sys)

sys.setdefaultencoding('gbk')

As shown below:


Then try to install the .whl file. When the third-party package is installed, you can enter the command line of the system and enter the command pip list for verification. As shown in the figure below, you can see the BeautifulSoup4 package and matplotlib package just installed.


4 IDEs for Python

Writing programs on the interactive command line of Python has the advantage that you can get the results at once, but the disadvantage is that you cannot save it, and you have to type it again the next time you want to run it. Therefore, in actual development, we always use an integrated development environment (IDE) to write code, and after writing, save it as a file, so that the program can be run repeatedly.

The first recommendation is PyCharm, which is a Python IDE created by JetBrains. PyCharm has the functions of general Python IDE, such as: debugging, syntax highlighting, project management, code jumping, intelligent prompting, auto-completion, unit testing, version control, etc. Attach the official download address of PyCharm: http://www.jetbrains.com/pycharm/download/ . The following is a view of the PyCharm effect:


The second recommended text editor is Sublime Text. SublimeText has a beautiful user interface and powerful features such as code thumbnails, plugins for Python, code snippets, and more. Also customize key bindings, menus and toolbars. Key features of Sublime Text include: spell checking, bookmarks, full Python API, Goto functionality, instant item switching, multiple selection, multiple windows, and more. Sublime Text is a cross-platform editor that supports Windows, Linux, MacOS X and other operating systems. The following is the effect view of Sublime Text:


5 Python's integrated environment—Anaconda

In our actual operation, we often use Python's integrated environment, beautiful and easy-to-use interface, more convenient to manage those third-party Libraries, and more diligent in version updates. Attach the download address of Anaconda:

https://www.continuum.io/downloadsPlease choose the version suitable for your computer.

It should be noted here that if your computer username carries a Chinese path, you cannot install Anaconda. It is best to modify the username folder to make it an English name. These online tutorials will not be repeated here. After installation, open the Environment option of Navigtor to view all the currently integrated third-party libraries. Pandas, Numpy, and matplotlib are already installed, as shown in the following figure:


Of course, if there are some third-party packages that you still need but do not provide, we can use this package manager to download directly, download the basemap third-party package as shown below, search for packages that are not installed, and then click the apply button. :



Of course, Anaconda comes with its own interactive interface and IDE, etc. You can try to use it yourself. For example, the following figure is the interactive environment of Ipython:


For example, when we use PyCharm's IDE, we need to select our Python interpreter. You can find the project-Python interpreter option in the file settings, and you can select Anaconda's Python interpreter, as shown in the following figure:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326184763&siteId=291194637