Як встановити бібліотеку в Python через pip і venv

How to Install a Python Library with pip and venv

How to install a Python library is one of the first things you need to know when starting most projects. The usual tool is pip, but the right approach depends on which Python interpreter is running, whether you are using a virtual environment, and whether system permissions might block the install.

The simplest way to install a package with pip

The most reliable way to install a Python library is to run python -m pip install package_name. This ties the installation to the exact Python version you are using, which is safer than calling pip on its own.

For example, if you need the requests package, use:

  • python -m pip install requests
  • py -m pip install requests on Windows

Verification is straightforward: open Python and try import requests. If no error appears, the package is installed correctly. If you see a module-not-found message, check whether you installed the package into the same interpreter you are using to run the code.

When a virtual environment is the better choice

A Python virtual environment keeps one project’s libraries from interfering with another project’s dependencies. It is the best default choice for most real-world work, especially when you install several packages or maintain multiple projects at once.

How to create and activate venv

Creating a virtual environment takes only a few commands:

  • python -m venv .venv
  • activate on Windows: .venv\Scripts\activate
  • activate on macOS or Linux: source .venv/bin/activate

After activation, install the library again with pip. A quick check is to look for the environment name in the terminal and run python -m pip show package_name; the package path should point inside .venv. If it does not, the environment is not active or you are using a different terminal session.

What to do when a package will not install

Python library installation errors are often caused by the Python version, permissions, or an outdated pip rather than the package itself. The safest first step is to update the tooling before changing system settings or reinstalling Python.

  • python -m pip install –upgrade pip
  • check your Python version with python –version
  • make sure the package name is spelled correctly
  • if the system blocks writes, use a virtual environment instead of a global install

If the error mentions compilation or missing dependencies, the package may require extra system components. In that case, look for the official installation notes for that specific package instead of guessing. After fixing the issue, repeat the install command and try importing the library in Python again.

How to install a specific version or update a library

A specific Python library version is useful when a project depends on compatibility or when you need to reproduce an older environment. Add the version number after the double equals sign:

  • python -m pip install requests==2.31.0
  • python -m pip install –upgrade requests

Updating should be done carefully if the project is already in production or has pinned dependencies. After the update, verify the installed version with python -m pip show requests and test the code that uses the package. If something breaks, roll back to the previous version by installing the exact older release.

The most reliable habit for stable installs

The best way to install a Python library without confusion is to always combine venv with python -m pip. That approach reduces conflicts, makes verification easier, and keeps the project environment predictable.

If a library installs successfully but your code still cannot see it, the problem is usually a mismatch between the interpreter, the environment, and the command you used to run the script. Check which Python is actually executing the code, then repeat the installation in that same environment.