Fundamentals 5 min read

Understanding Python Third‑Party Libraries: Installation, Usage, and Virtual Environment Management

This guide explains what Python third‑party libraries are, how to install them with pip (including version control, batch installs, upgrades, and uninstalling), how to import and use them, manage virtual environments, and troubleshoot common installation issues.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding Python Third‑Party Libraries: Installation, Usage, and Virtual Environment Management

1. What are third‑party libraries?

Python's power lies not only in its standard library but also in the rich ecosystem of third‑party libraries contributed by developers worldwide, covering data analysis, machine learning, web development, automation, and more.

2. How to install third‑party libraries?

Python's package manager pip (Python Package Installer) is the primary way to install third‑party libraries.

(1) Basic installation

pip install package_name

For example, to install the requests library:

pip install requests

(2) Install a specific version

pip install package_name==version_number

For example, install NumPy version 1.24.0:

pip install numpy==1.24.0

(3) Batch install from requirements.txt

In team projects, a requirements.txt file records all dependencies:

pip install -r requirements.txt

(4) Upgrade and uninstall

Upgrade a package: pip install --upgrade package_name

Uninstall a package: pip uninstall package_name

3. Using third‑party libraries

After installation, import the library in Python code with import :

import requests
response = requests.get("https://www.example.com")
print(response.status_code)  # prints 200 if the request succeeds

(1) Alias import (as)

If a library name is long, you can use an alias:

import numpy as np
arr = np.array([1, 2, 3])
print(arr)

(2) Import specific functions (from ... import)

from math import sqrt
print(sqrt(16))  # prints 4.0

4. Virtual environment management (venv)

To avoid dependency conflicts between projects, use a virtual environment.

(1) Create a virtual environment

python -m venv myenv

(2) Activate the virtual environment

Windows: myenv\Scripts\activate

Linux/macOS: source myenv/bin/activate

(3) Deactivate the virtual environment

deactivate

5. Common issues and solutions

(1) pip installation is slow?

Use a domestic mirror to accelerate:

pip install package_name -i https://pypi.tuna.tsinghua.edu.cn/simple

(2) Installation fails?

Check Python version compatibility.

Ensure network connectivity.

Try installing with the --user flag: pip install --user package_name

Summary

pip is the most common Python package manager, supporting install, upgrade, and uninstall of third‑party libraries.

Virtual environments ( venv ) isolate project dependencies and prevent conflicts.

Domestic mirrors (e.g., Tsinghua, Aliyun) can speed up downloads.

Mastering third‑party library installation and management greatly improves Python development efficiency.

Pythondependency managementInstallationVirtual EnvironmentPackage ManagementThird-Party Librariespip
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.