How to install third-party modules of Python_python how to install modules_

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 .

[----Help Python learning, all the following learning materials are free at the end of the article! ----】

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 code 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 a single 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.

insert image description here

Python technical resource sharing

If you are interested in Python, it is a very good choice to learn Python well, whether it is employment, sideline business to make money, or to improve learning and work efficiency , but you must have a systematic learning plan.

The editor is a Python development engineer , and I have compiled a set of [the latest Python system learning tutorials] , including basic python scripts to web development, crawlers, data analysis, data visualization, machine learning, etc.

If you are ready to learn Python or are currently learning, the following should be useful to you:

1. Python learning routes in all directions

The route of all directions in Python is to organize the commonly used technical points of Python to form a summary of knowledge points in various fields. Its usefulness lies in that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.

insert image description here

2. Learning software

If a worker wants to do a good job, he must first sharpen his tools. The commonly used development software for learning Python is here, which saves you a lot of time.

insert image description here

3. Excellent books

The advantage of books lies in their authority and sound system. When you first start learning, you can just watch videos or listen to someone’s lectures, but after you finish learning, you think you have mastered it. At this time, it is recommended to read books and read Authoritative technical books are also the only way for every programmer.

insert image description here

4. Introductory learning video

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.

img

5. Practical cases

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

img

6. Tsinghua programming boss produced "Learning Python by Reading Comics"

Use easy-to-understand comics to teach you Python, making it easier for you to remember and not boring.

insert image description here

7. Python sideline part-time and full-time routes

insert image description here

Data collection

This full version of the full set of Python learning materials has been uploaded to the official CSDN. If you need it, you can click the CSDN official certification WeChat card below to get it for free ↓↓↓ [Guaranteed 100% free]

insert image description here

Guess you like

Origin blog.csdn.net/weixin_49892805/article/details/132467640