How to install and use an external Library in Python

Configurare noua (How To)

Situatie

If you want to extend what Python can do, you can install external libraries (also called third-party packages).These are not part of the default Python installation, but they can give you access to lots of powerful features — like web scraping, data analysis, or sending emails.

Solutie

Step 1: Make sure Python and pip are installed

Most Python versions come with pip, the tool used to install libraries.

To check if Python and pip are installed, open Command Prompt (or Terminal) and type:

python --version
pip --version

If both commands work, you’re good to go. If not, you may need to download and install Python from the official website.

Step 2: Install a library using pip

Let’s say you want to install the requests library, which lets you send HTTP requests in Python (great for working with websites and APIs).

Run this command in your terminal:

pip install requests

If you see a success message, the package is now installed and ready to use in your Python scripts.

Step 3: Use the library in your code

Once installed, you can import and use it in your Python script like this:

import requests

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

This will fetch the webpage and print the status code (like 200 for OK).

Bonus Tip: Installing multiple libraries at once

If you have a list of libraries you need for a project, you can put them in a file called requirements.txt like this:

requests
flask
pandas

And install all of them at once with:

pip install -r requirements.txt

Tip solutie

Permanent

Voteaza

(3 din 6 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?