DEV Community

Cover image for Python Libraries and Virtual Environments– Day 15
augustineowino357-design
augustineowino357-design

Posted on • Edited on

Python Libraries and Virtual Environments– Day 15

  • Python Libraries and Virtual Environments

In the previous lesson, we learned about Object-Oriented Programming (OOP) and how Classes and Objects help organize Python applications. Today, we will learn about Python Libraries and Virtual Environments, which are essential tools in modern Python development.

Libraries and Virtual Environments help developers build powerful applications efficiently while keeping projects organized.


What are Python Libraries?

A Python Library is a collection of pre-written code that programmers can use to perform specific tasks without writing everything from scratch.

Libraries contain:

  • Functions
  • Classes
  • Modules

Python libraries save time and simplify development.


Why Libraries are Important

Libraries help programmers:

  • Reduce development time
  • Reuse existing solutions
  • Build advanced applications
  • Improve productivity
  • Access powerful tools easily

Most Python applications rely heavily on libraries.


Types of Python Libraries

Python libraries are mainly divided into:

  • Built-in Libraries
  • External Libraries

Built-in Libraries

Built-in Libraries come pre-installed with Python.

Examples include:

  • "math"
  • "random"
  • "datetime"
  • "os"
  • "sys"

Example

import math

print(math.sqrt(25))
Enter fullscreen mode Exit fullscreen mode

Output

5.0
Enter fullscreen mode Exit fullscreen mode

External Libraries

External Libraries are developed by other programmers and must be installed manually.

Examples include:

  • "requests"
  • "numpy"
  • "pandas"
  • "flask"
  • "django"

What is pip?

"pip" is Python’s package manager used to install libraries.

Syntax

pip install package_name
Enter fullscreen mode Exit fullscreen mode

Installing a Library

Example

pip install requests

This installs the "requests" library.
Enter fullscreen mode Exit fullscreen mode

Checking Installed Libraries

Example

pip list

This displays all installed packages.
Enter fullscreen mode Exit fullscreen mode

Upgrading a Library

Example

pip install --upgrade requests
Enter fullscreen mode Exit fullscreen mode

Uninstalling a Library

Example

pip uninstall requests
Enter fullscreen mode Exit fullscreen mode

Using an External Library

Example

import requests

response = requests.get("https://example.com")

print(response.status_code)
Enter fullscreen mode Exit fullscreen mode

Output

200
Enter fullscreen mode Exit fullscreen mode

Popular Python Libraries

Library| Purpose
"numpy"| Numerical computations
"pandas"| Data analysis
"matplotlib"| Data visualization
"requests"| HTTP requests
"flask"| Web development
"django"| Advanced web development
"tensorflow"| Machine learning
"opencv"| Image processing


What is a Virtual Environment?

A Virtual Environment is an isolated environment used to manage project dependencies separately.

It allows different projects to use different library versions without conflicts.


Why Virtual Environments are Important

Virtual Environments help developers:

  • Avoid dependency conflicts
  • Keep projects organized
  • Manage package versions
  • Create isolated workspaces
  • Improve project portability

Most professional Python developers use Virtual Environments.


Creating a Virtual Environment

Python uses the "venv" module to create Virtual Environments.

Example

python -m venv myenv
Enter fullscreen mode Exit fullscreen mode

This creates a virtual environment named "myenv".


Activating a Virtual Environment

Windows

myenv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Linux / macOS

source myenv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Deactivating a Virtual Environment

Example

deactivate
Enter fullscreen mode Exit fullscreen mode

This command exits the currently active virtual environment and returns to the system's default Python environment.

Installing Packages Inside a Virtual Environment

After activating a virtual environment, packages can be installed normally using "pip".

Example

pip install requests

The package will only be installed inside that virtual environment.


**Viewing Installed Packages**

**Example**
Enter fullscreen mode Exit fullscreen mode

pip list

This displays all packages installed in the current virtual environment.


**Saving Project Dependencies**

Developers often save installed packages into a file called
"requirements.txt".

**Example**
Enter fullscreen mode Exit fullscreen mode

pip freeze > requirements.txt

This creates a file containing all installed packages and their versions.


**Example requirements.txt File**
Enter fullscreen mode Exit fullscreen mode

requests==2.32.0
numpy==2.1.0
pandas==2.3.0

This file helps other developers install the same dependencies.


**Installing Dependencies from requirements.txt**

**Example**
Enter fullscreen mode Exit fullscreen mode

pip install -r requirements.txt



This installs all packages listed in the file.


**Best Practices for Using Virtual Environments**

When working on Python projects:

- Create a separate virtual environment for each project
- Keep dependencies updated
- Use requirements.txt files
- Avoid installing unnecessary packages
- Activate the environment before development

These practices help maintain clean and organized projects.


**Common Commands Summary**

Command| Purpose


"python -m venv myenv"| Create a virtual environment
"myenv\Scripts\activate"| Activate on Windows
"source myenv/bin/activate"| Activate on Linux/macOS
"deactivate"| Exit virtual environment
"pip install package_name"| Install package
"pip uninstall package_name"| Remove package
"pip list"| Show installed packages
"pip freeze > requirements.txt"| Save dependencies
"pip install -r requirements.txt"| Install dependencies


**Real-World Applications of Python Libraries**

Python libraries are widely used in:

- Web Development
- Data Science
- Artificial Intelligence
- Machine Learning
- Automation
- Cybersecurity
- Mobile Applications
- Cloud Computing

Without libraries, building complex applications would take significantly more time.


**Advantages of Virtual Environments**

Virtual Environments help developers:

- Isolate project dependencies
- Prevent version conflicts
- Improve project organization
- Simplify deployment
- Support teamwork and collaboration

They are considered a standard practice in professional Python development.


**Conclusion**

Python Libraries and Virtual Environments are essential tools for every Python developer. Libraries provide powerful pre-built functionality that speeds up development, while Virtual Environments help manage project dependencies safely and efficiently.

Understanding how to install, use, and manage libraries, as well as how to create and work with Virtual Environments, is an important step toward becoming a professional Python developer.

Practice installing libraries, creating virtual environments, and managing project dependencies to strengthen your Python development skills.

#Python #PythonForBeginners #CodingJourney #LearnPython #Programming
Enter fullscreen mode Exit fullscreen mode

Top comments (0)