How to install third-party modules for Python

foreword

As the saying goes, " Life is short, I use Python ". One of the great advantages of Python is that it has rich and easy-to-use third-party modules, which saves a lot of time in reinventing the wheel and saves the lives of many developers. For those already familiar with Python development, installing third-party modules is routine. But if you are a novice who is just getting started, it is likely to be tossed. So let me briefly explain how to install third-party modules of Python.

There are many ways to install third-party modules in Python, the two most commonly used are: through the package manager , and directly downloading the source code for installation .

1. Package manager

Many systems and languages ​​provide package managers. You can think of a "package manager" as something like an app store . There are various third-party modules in Python's package manager. With it, you don't need 998 or 98. You only need one command to automatically download and install it for you.

The commonly used package manager for Python is pip . They will search for the module you want from a source called PyPI , and automatically download and install it after finding it. PyPI is Python's official third-party module repository for all developers to download or upload code.

If you are using Mac or Linux, then like Python, pip should come with your system. And if you are Windows, then when installing Python, check pip and Add python.exe to Path, it will help you install pip and set the path in the environment variable at the same time. If you cannot use pip, make sure that there is pip in the Scripts subdirectory under the Python installation directory, and the path of this subdirectory is added to the environment variable Path. If there is no pip, you need to install it by downloading setuptools, or it is recommended to reinstall Python directly.

Taking IPython as an example, to install it through the pip command, you only need to enter on the command line:

pip install ipython

If everything is normal and the network is not blowing, just wait for a while, you can see the download progress, and it can be used after automatic installation. If permission issues such as Permission denied are prompted under Mac/Linux, add sudo before the command.

IPython is an enhanced version of the Python shell, which can be opened and used by typing ipython on the command line. It is better than the one that runs python by default, and it will be very convenient to debug the code in it. However, for windows, you need to install a pyreadline module with pip to use IPython's tab key auto-completion function. (Developing with Windows is a lot of work)

If you are not very clear about the module name to download, you can also search, for example:

pip search ipython

However, because the pip warehouse server is located abroad, the download is often too slow or fails due to network connection problems. In view of this situation, one solution is to "change the source", and change the warehouse address to a domestic mirror website through configuration. Or add the -i parameter directly after the command to specify the download source, for example:

pip install ipython -i https://mirrors.aliyun.com/pypi/simple matplotlib

Here are several domestic source addresses:

  • Tsinghua University https://pypi.tuna.tsinghua.edu.cn/simple
  • University of Science and Technology of China https://pypi.mirrors.ustc.edu.cn/simple/
  • Alibaba Cloud http://mirrors.aliyun.com/pypi/simple/
  • Douban http://pypi.douban.com/simple/

Generally speaking, pip can handle most of the module installation. If that doesn't work, you can try the other way below.

2. Source installation

Almost all third-party modules can find source code on PyPI or github, and will provide compressed packages in zip, tar and other formats. Download the code compressed package to the local and unzip it, you should see a setup.py file. Enter its directory on the command line and execute:

python setup.py install

This third-party module will be installed. The end result is the same as using a package manager.

No matter which method is used, the third-party module code will be installed under the path of Python. Depending on the system, the location is different, and it is roughly called site-packages or dist-packages. So for some pure Python code packages that have no other dependencies and do not need to be compiled in other languages, you can also manually copy the source code to the site-packages or dist-packages directory. As long as the path is correct, you can import these modules in your code.

Friendly reminder of some pitfalls:

  • Before installing a third-party module, please confirm whether the version it supports includes the version of Python you are using.
  • A small number of complex packages may not be installed successfully with one command. Special cases are treated specially, and search engines will guide you.
  • Using pip can be confusing if you have multiple versions of Python installed on your computer. For this problem, virtualenv is a good solution, and I will talk about it next time.

In addition, there is a project called Awesome Python , which lists all kinds of excellent, practical and interesting Python libraries:

https://github.com/jobbole/awesome-python-cn

1. Learning routes in all directions of Python

Just started learning python, if you don't even plan the complete learning steps, it is basically impossible to learn python. He sorted out all the directions of Python to form a summary of knowledge points in various fields.(The wife in the picture is too big. I can’t put it here. If you don’t have a full version, you can get it for free at the end of the article)

insert image description here

2. Getting started with a full set of learning videos

When we watch videos and learn, we can’t just move our eyes and brain without using our hands. A more scientific learning method is to use them after understanding. At this time, the hands-on project is very suitable.

insert image description here

Three, Python operation example

Learning python is the same as learning mathematics. You can’t just read the book without doing the questions. Looking directly at the steps and answers will make people mistakenly think that you have mastered everything, but you will still be at a loss when you encounter a problem.

Therefore, in the process of learning python, you must remember to write more codes by hand. You only need to read the tutorial once or twice.

insert image description here

4. Python employment project actual combat

We must learn Python to find a high-paying job or a high-paying part-time job. The following are some practical projects that companies can use. After learning these, I believe everyone will be able to find a satisfactory job.

insert image description here

11 Django framework

insert image description here

16 WeChat public account
insert image description here

18 Common crawler module usage

insert image description here

21 Data Analysis

insert image description here

22 Machine Learning
insert image description here

There are other things, such as my own Python introductory graphic tutorials, you can use your mobile phone to learn knowledge when you don’t have a computer, and after learning the theory, you can type the code to practice verification, and there is also the library information of the Chinese version of Python. , MySQL and HTML tags, etc., these are things that can be given to fans.

Data collection

These are not very valuable things, but they are really good for learners who have no resources or the resources are not very good. If you can use them, you can scan the QR code of CSDN official certification below on WeChat [free access]↓↓↓ .

insert image description here

Good article recommendation

Understand the prospect of python: https://blog.csdn.net/SpringJavaMyBatis/article/details/127194835

Learn about python's part-time sideline: https://blog.csdn.net/SpringJavaMyBatis/article/details/127196603

insert image description here

Guess you like

Origin blog.csdn.net/weixin_49895216/article/details/130193265