Python Environments and Packages

A Python environment combines one interpreter with the packages available to it. Learn how to identify the running interpreter, create a project virtual environment, install through `python -m pip`, record a dependency, and keep Browser Python separate from local package setup.

A Python program runs with one Python interpreter and the packages available to that interpreter. When a computer has more than one Python installation, a package can be installed successfully for one interpreter and still be missing from another.

This distinction matters before the NumPy chapters. NumPy is not part of Python's standard library. A local Python installation must be able to find an installed copy of the package before import numpy can work.

A Python Environment Is an Interpreter and Its Installed Packages

An environment is the Python interpreter used for a run together with the packages that interpreter can import. Two environments on the same computer can use the same Python version and still contain different packages.

Inspect the interpreter selected by the command python:

python --version
python -c "import sys; print(sys.executable)"

The first command prints the version. The second prints the path of the actual interpreter executable. That path is more useful than assuming which Python a short command name selects.

On a system where the command is named python3, use python3 consistently in the commands below. The important rule is that the command used to install a package and the command used to run the program should select the same interpreter.

Installation and Import Are Different Operations

A package is a distributable collection of Python code and related files. Installing a package places it in an environment. An import statement then loads an available module or package into a running program.

These operations happen at different times:

python -m pip install numpy
import numpy

The terminal command installs NumPy into the selected environment. The Python statement imports it during a program run. Writing import numpy does not install NumPy, and installing NumPy does not automatically import it into every program.

The name used for installation and the name used in import are often the same, but that is a convention rather than a rule. Follow the package's own documentation when the two names differ.

Run pip Through the Selected Python

pip is Python's common package installer. Use it through the interpreter:

python -m pip install numpy

The -m option asks that Python interpreter to run the pip module. This form keeps the installer connected to the same python command you just inspected. A bare command such as pip install numpy may select an installer belonging to a different Python installation.

Check the connection before diagnosing a failed import:

python -c "import sys; print(sys.executable)"
python -m pip --version

The pip output includes a location and Python version. If those details do not match the interpreter used to run the program, the package was installed in a different environment.

Give Each Project a Virtual Environment

A virtual environment is a project-specific environment stored in a folder. It lets one project install the packages it needs without changing the package set of another project.

From the project directory, create one named .venv:

python -m venv .venv

The .venv folder contains a Python interpreter and its package location. The leading dot is a naming convention; another folder name would also work.

Activate it in a macOS or Linux shell:

source .venv/bin/activate

Activate it in Windows PowerShell:

.venv\Scripts\Activate.ps1

Activation changes the current shell so python and python -m pip select the virtual environment first. Many shells also show (.venv) in the prompt, but the prompt is only a convenience. Verify the selected interpreter when it matters:

python -c "import sys; print(sys.executable)"

The printed path should be inside the project's .venv folder.

Now install the project dependency:

python -m pip install numpy

When finished working in that shell, leave the activated environment with:

deactivate

Activation is not permanent. A new terminal usually needs activation again before running the project.

Record the Packages a Project Needs

A teammate or a later version of you should not have to guess which packages to install. A small project can list direct dependencies in requirements.txt:

numpy

Install the listed packages into the active environment with:

python -m pip install -r requirements.txt

For a real project, a version range can record which releases have been tested:

numpy>=2.0,<3

Choosing and maintaining exact dependency versions is a larger packaging topic. At this stage, the important practice is to record the dependency and install it into the project's own environment.

Do not commit the .venv folder to source control. It contains generated, machine-specific files and can be recreated from the Python version and the dependency list.

Browser Python Is a Separate Environment

The runnable editors use Python inside the browser. Their packages and files belong to that browser runtime, not to a virtual environment on your computer. Installing NumPy in a local terminal does not change Browser Python, and a package available in Browser Python is not thereby installed locally.

The lessons arrange the packages needed by their browser examples. Use local environment commands only when running the code with a locally installed Python interpreter.

Diagnose an Import That Works in One Place but Not Another

Suppose an editor can import NumPy but a terminal reports:

ModuleNotFoundError: No module named 'numpy'

Check the environment before reinstalling repeatedly:

  1. print sys.executable from the failing Python;
  2. run python -m pip --version with that same command;
  3. run python -m pip show numpy to see whether NumPy is installed there;
  4. activate the intended .venv, if the project uses one;
  5. install from the project's dependency file when the package is absent.

Also check that the project does not contain a local file named numpy.py, which could hide the installed package just as math.py can hide the standard library module.

Exercise: Identify the running interpreter

Which command prints the path of the Python interpreter selected by python?

Choose the command

Select one choice, then check.

Review

Not marked done.

Your checked work will be saved automatically.

Correct records the checked result. Done is your learning status, and you can undo it.

Clearing an answer or resetting code starts the response again. It does not remove Done or Review.

Your checked work will be saved automatically.

HintInspect Python itself

The sys module records the path of the current interpreter.

SolutionPrint sys.executable

Use python -c "import sys; print(sys.executable)". The -c option runs the supplied Python statement and prints the executable path for that run.

Exercise: Install for the selected Python

Which command most clearly installs NumPy for the interpreter selected by python?

Choose the installation command

Select one choice, then check.

Review

Not marked done.

Your checked work will be saved automatically.

Correct records the checked result. Done is your learning status, and you can undo it.

Clearing an answer or resetting code starts the response again. It does not remove Done or Review.

Your checked work will be saved automatically.

HintRun the installer through Python

Use the interpreter's -m option to run the pip module.

SolutionUse python -m pip

python -m pip install numpy asks the selected Python interpreter to run its package installer. The later program can then use import numpy.

Exercise: Create an isolated project setup

Which sequence creates a virtual environment, activates it on macOS or Linux, and installs NumPy into it?

Choose the command sequence

Select one choice, then check.

Review

Not marked done.

Your checked work will be saved automatically.

Correct records the checked result. Done is your learning status, and you can undo it.

Clearing an answer or resetting code starts the response again. It does not remove Done or Review.

Your checked work will be saved automatically.

HintChoose the destination first

The active interpreter determines where python -m pip installs the package.

SolutionCreate, activate, and install

The first sequence creates .venv, changes the shell to use it, and then installs NumPy through that environment's Python.

Exercise: Diagnose a mismatched installation

python -m pip install numpy succeeds in one terminal, but a program launched from an editor cannot import it. What should you compare first?

First comparison

Select one choice, then check.

Review

Not marked done.

Your checked work will be saved automatically.

Correct records the checked result. Done is your learning status, and you can undo it.

Clearing an answer or resetting code starts the response again. It does not remove Done or Review.

Your checked work will be saved automatically.

HintCompare sys.executable

Print sys.executable in both places before installing again.

SolutionCompare the selected interpreters

Print sys.executable in the terminal run and in the editor. If the paths differ, install the dependency for the environment that actually runs the program or configure the editor to use the intended environment.

Exercise: Separate browser and local environments

NumPy imports successfully in a Browser Python lesson. What does that prove about a new local virtual environment?

Choose the conclusion

Select one choice, then check.

Review

Not marked done.

Your checked work will be saved automatically.

Correct records the checked result. Done is your learning status, and you can undo it.

Clearing an answer or resetting code starts the response again. It does not remove Done or Review.

Your checked work will be saved automatically.

HintKeep the runtimes separate

The browser runtime does not modify the package folders on your computer.

SolutionCheck the local environment independently

Browser availability says only that the lesson runtime provides NumPy. In a local virtual environment, check with python -m pip show numpy and install the package there when needed.

Keep the Interpreter and Installer Together

For a small local project:

  1. create one .venv in the project directory;
  2. activate it before running project commands;
  3. verify sys.executable when the selected Python is uncertain;
  4. install with python -m pip, not an unverified bare pip;
  5. record direct dependencies in requirements.txt;
  6. treat Browser Python as a separate environment.

These habits make an import failure a concrete question—“which interpreter and package location are in use?”—instead of a repeated cycle of installation and guessing. The next lesson returns to program structure and controls which workflow runs when a module is imported or started directly.

Review

Not marked done.