Tutorials Logic, IN info@tutorialslogic.com

Python pip Install Packages, Virtual Environment

Python pip

pip installs Python packages so your project can use code that is not included in the standard library. Use it from the same Python interpreter that will run your program.

A virtual environment keeps each project's packages separate. This prevents one project upgrade from breaking another project that needs a different package version.

Most pip problems are environment problems: the package was installed, but not into the interpreter that is running the script.

Install Packages with pip

Use pip when your program needs a third-party package such as requests, flask, django, numpy, pandas, or pillow.

Prefer python -m pip because it connects pip to the selected Python interpreter instead of whichever pip command appears first on the system PATH.

  • Install with python -m pip install package_name.
  • Inspect a package with python -m pip show package_name.
  • Upgrade only when you know the project can use the newer version.
  • Remove unused packages so the environment stays understandable.

Create a Virtual Environment

Create a virtual environment inside the project, activate it, install packages, then run the project from that same activated terminal.

When sharing a project, include dependency information so another developer can rebuild the same environment.

  • Create the environment with python -m venv .venv.
  • Activate it before installing project packages.
  • Run python -m pip list to confirm what is installed.
  • Store repeatable dependencies in requirements.txt or pyproject.toml.

Environment Mismatch

pip problems usually happen when the package installs into a different Python environment than the one running the program.

  • Use python -m pip install package_name to bind pip to the selected interpreter.
  • Activate the virtual environment before installing project dependencies.
  • Check python -c "import sys; print(sys.executable)" when imports fail after installation.
  • Save repeatable dependencies in requirements.txt or pyproject.toml.

pip in Real Projects

  • Use a virtual environment before installing packages for a project.
  • Use python -m pip so pip runs under the same interpreter as your program.
  • Freeze dependencies only after the project imports and runs correctly.
  • Record the package name, version, and reason for installing it in project notes.

Check pip Uses the Active Python

Check pip Uses the Active Python
import sys

print(sys.executable)
# Then install packages with:
# python -m pip install requests

sys.executable shows the Python interpreter that will run the script. Use python -m pip from that same interpreter to avoid installing into the wrong environment.

Inspect Package Search Paths

Inspect Package Search Paths
import site
import sys

print(sys.prefix)
for path in site.getsitepackages():
    print(path)

This prints where the active Python looks for installed packages. If a package imports in one terminal but not another, compare these paths.

pip environment check

Can You Install Packages Into the Right Environment?

5 checks
  • Create a virtual environment before installing project packages.
  • Install packages with python -m pip to target the active interpreter.
  • Confirm imports with the same Python used to run the project.
  • Record dependencies in requirements.txt or pyproject.toml.
  • Do not upgrade packages blindly in a working project.

Environment Install Traps

  • Installing into the wrong Python

    Use python -m pip so pip matches the interpreter that runs your project.
  • Skipping virtual environments

    Create a project environment before installing packages for a real project.
  • Not recording dependencies

    Save installed package versions when a project must be reproduced.

Try this next

Install Into the Project Environment

0 of 3 completed

  1. Run python -m pip --version and identify which Python it belongs to.
  2. Create a virtual environment and install one harmless package inside it.
  3. Write current project dependencies to requirements.txt.

Questions About pip Environment

Check sys.executable or where python. It should point inside the virtual-environment directory for that project.

pip may have installed the package for a different interpreter. Run python -m pip show package and compare its location with sys.executable.

Applications usually benefit from pinned versions for repeatable installs. Libraries often allow compatible ranges instead.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.