Quick Answer
Use python -m pip on Unix-like systems or py -m pip on Windows when you want pip tied to a specific interpreter. pip3 is a command name commonly used for a Python 3 installation, while bare pip depends on your PATH. In a virtual environment, use its Python executable so installation and execution target the same environment.

pip and pip3 are command names for installing Python packages. The confusing part is that the names do not always point to the same Python interpreter on every computer. On many current systems, both commands install into Python 3. On older systems, shared servers, or machines with several Python installs, pip may point somewhere else.
The safest habit is to run pip through the interpreter that will run your project: python -m pip. That form first starts Python, then asks that exact interpreter to run the pip module. It avoids guessing which standalone command your shell found first.
The official pip user guide, pip install reference, Python documentation for the -m command option, the venv module, and the Python Packaging User Guide on pip with virtual environments are the best references.
Use pip3 only when your platform documents it as the Python 3 installer you want. Use python3 -m pip when you specifically want the interpreter named python3. Use python -m pip inside a virtual environment when that environment is already active. The rule is simple: choose the Python interpreter first, then run pip from it.
Compare The Command Names
Do not assume that pip, pip3, and the active Python all describe the same install target. Build the commands explicitly before running anything.
import shlex
import sys
commands = {
"pip command": ["pip", "--version"],
"pip3 command": ["pip3", "--version"],
"active interpreter": [sys.executable, "-m", "pip", "--version"],
}
for label, command in commands.items():
line = " ".join(shlex.quote(part) for part in command)
print(f"{label}: {line}")
The first two commands depend on your shell path. The third command is tied to sys.executable, which is the Python process running the code. That makes it the most reliable form for scripts, notebooks, editor tasks, and CI checks.
Find The Interpreter Target
The package install location comes from the active interpreter. Print the executable, script directory, package directory, and virtual environment status before changing packages.
import sys
import sysconfig
paths = sysconfig.get_paths()
print("python:", sys.executable)
print("scripts:", paths["scripts"])
print("packages:", paths["purelib"])
print("venv active:", sys.prefix != sys.base_prefix)
If the package directory is not under the environment you expected, stop and fix interpreter selection first. Installing more packages with a mismatched command usually makes the situation harder to read.
This also explains why a package can install successfully but still fail to import. Pip may have installed it into one Python while the program is launched by another Python.
Build Install Commands Safely
When code needs to show or launch a pip command, keep each argument as a separate list item. That prevents quoting mistakes and keeps the package name separate from command options.
import re
import shlex
import sys
package_name = "requests"
if not re.fullmatch(r"[A-Za-z0-9_.-]+", package_name):
raise ValueError("package name contains unsupported characters")
command = [sys.executable, "-m", "pip", "install", package_name]
print(" ".join(shlex.quote(part) for part in command))
This example prints a command instead of installing anything. In real maintenance work, replace the package name with the dependency you actually need and run it in the project environment, not in a random global Python.
Use Virtual Environments
A virtual environment gives one project its own interpreter and package directory. After creating it, run pip through the environment’s Python instead of relying on a global command name.
import os
import shlex
import sys
bin_dir = "Scripts" if os.name == "nt" else "bin"
venv_python = os.path.join(".venv", bin_dir, "python")
commands = [
[sys.executable, "-m", "venv", ".venv"],
[venv_python, "-m", "pip", "install", "-r", "requirements.txt"],
]
for command in commands:
print(" ".join(shlex.quote(part) for part in command))
Activation changes what command names resolve to, but automation does not always run in an activated shell. Calling the environment interpreter directly is more explicit for deployment scripts, scheduled tasks, and documentation.
For local development, activation is still convenient. After activation, python -m pip should point into the environment. If it does not, check the interpreter path before installing dependencies.
Check Package Versions
Use installed metadata to check package versions from the same Python process that runs your program. This is better than asking a different terminal command and hoping it points to the same environment.
from importlib.metadata import PackageNotFoundError, version
for package_name in ["pip", "setuptools", "wheel"]:
try:
print(package_name, version(package_name))
except PackageNotFoundError:
print(package_name, "not installed")
If pip, setuptools, or wheel is missing or old, upgrade them through the interpreter that owns the environment. The command shape is python -m pip install --upgrade pip setuptools wheel, using the correct Python command for the project.
Verify A Subprocess Uses The Same Python
When a script launches another Python process, pass sys.executable. That keeps the child process aligned with the parent process.
import subprocess
import sys
script = "import sys; print(sys.executable); print(sys.prefix)"
result = subprocess.run(
[sys.executable, "-c", script],
capture_output=True,
text=True,
check=True,
)
for line in result.stdout.splitlines():
print(line)
This pattern is useful for tests that need to confirm interpreter selection. It is also safer than constructing one long command string, because each argument remains separate.
Common pip vs pip3 Fixes
If pip install package succeeds but import package fails, print sys.executable in the failing program and install with that interpreter. The fix is usually python -m pip install package from the same environment.
If pip3 works but pip does not, your machine may have command aliases or path order differences. That is not a package problem. Use python3 -m pip or the full interpreter path until the project setup is clear.
If a notebook cannot import a package that your terminal can import, check the notebook kernel. Kernels can point at a different Python than the terminal where installation happened. Restart the kernel after changing packages so it sees the updated environment.
If a CI job installs packages but tests fail with missing imports, print the Python executable during the install step and the test step. Both steps should use the same interpreter or the same virtual environment path. After selecting the correct installer, Install lxml in Python with pip and Wheels covers lxml wheels, native build dependencies, virtual environments, and import verification.
In short, pip and pip3 are only command names. The reliable target is the Python interpreter. Choose the interpreter first, run -m pip from that interpreter, keep projects inside virtual environments, and verify versions from the Python process that will run the code.
Verify Which Interpreter pip Uses
The command name alone is not proof of the target environment. Compare the interpreter path, pip version, and package location before troubleshooting an import error.
python -c "import sys; print(sys.executable)"
python -m pip --version
python -m pip show requests
On Windows, py -m pip uses the Python launcher. Inside a virtual environment, activate it first or call its interpreter directly. The most useful invariant is simple: the Python that runs the script should own the site-packages directory where pip installs the dependency.
Why python -m pip Is Usually the Clearer Choice
python -m pip install package invokes the pip module through the named interpreter. That avoids many PATH mismatches between a system Python, a virtual environment, an IDE interpreter, and a separate pip3 executable.
python -m venv .venv
. .venv/bin/activate
python -m pip install package_name
python -c "import package_name; print(package_name.__file__)"
There is no universal promise that pip means Python 2 or pip3 means a particular Python 3 version. Inspect the executable and use the command that makes the intended interpreter unambiguous.
Frequently Asked Questions
What is the difference between pip and pip3?
They are command names that may point to different Python installations. pip3 commonly refers to Python 3, but the actual target depends on PATH and system configuration.
Should I use python -m pip instead of pip?
Usually yes when you need pip tied to the exact Python interpreter named by python. On Windows, py -m pip or a virtual-environment Python is similarly explicit.
Why does pip install a package but Python cannot import it?
pip may belong to a different interpreter or virtual environment. Compare sys.executable with python -m pip –version and inspect the package location.
Does a virtual environment change pip and pip3?
It can. An activated environment places its executables first on PATH, but using the environment’s Python with -m pip removes ambiguity.



This was most informative. Thanks! i’ve been using pip all this time, despite NEVER having used python 2.7 Everything seemed to work fine so i never really questioned it.
Shouldn’t checking the version be:
python -m pip --versionYeah, this works as well. Here, you’re basically asking your python to locate its corresponding pip and check the version. This method is good if you have multiple python installations.