Skip to content
C

Python Interview Questions

Environment, CLI & Packaging Interview Questions

Virtual environments, dependency management, pyproject.toml/wheels, and Python's environment/CLI/filesystem standard library.

Question 1: What is environment variable usage in Python?

Ans

Environment variables provide configuration outside source code and can be read with os.environ or os.getenv.

Example

python
import os port = int(os.getenv("PORT", "8000")) print(port)

Important Point

Do not store secrets directly in source code; use appropriate secret/configuration management.

Question 2: What is pathlib?

Ans

pathlib provides object-oriented path handling that is generally clearer and more portable than manually concatenating path strings.

Example

python
from pathlib import Path path = Path("data") / "input.txt" print(path)

Important Point

Use Path operations instead of hard-coded OS-specific separators.

Question 3: What is argparse?

Ans

argparse is a standard-library module for parsing command-line arguments and generating help messages.

Example

python
import argparse p = argparse.ArgumentParser() p.add_argument("--name", default="Guest") args = p.parse_args() print(args.name)

Important Point

Validate arguments and provide useful help/error messages for command-line tools.

Question 4: What is virtualenv vs venv?

Ans

venv is Python's standard-library mechanism for creating virtual environments. virtualenv is a separate, older and feature-rich third-party tool that can support additional workflows.

Example

bash
python -m venv .venv

Important Point

For many projects, built-in venv is sufficient; follow the team's tooling choice.

Question 5: What is dependency management?

Ans

Dependency management records, resolves, and installs the external packages a project needs, ideally with reproducible versions and environments.

Example

text
pyproject.toml requirements.txt lock file/tooling

Important Point

Modern Python projects often use pyproject.toml plus a package/dependency manager rather than relying only on requirements.txt.

Question 6: What is pyproject.toml?

Ans

pyproject.toml is a standardized configuration file used by Python projects for build systems, project metadata, dependencies, and tool configuration.

Example

toml
[project] name = "sample-app" version = "0.1.0"

Important Point

The exact fields and supported tools depend on the chosen packaging/build configuration.

Question 7: What is packaging in Python?

Ans

Packaging turns Python project code into a distributable structure or artifact that can be installed and reused. Modern projects commonly define metadata and build settings in pyproject.toml.

Example

text
src/ package_name/ pyproject.toml

Important Point

Keep package metadata, dependencies, and build configuration consistent with the selected build backend.

Question 8: What is a wheel?

Ans

A wheel is a built Python distribution format designed for installation without running a source build in many cases.

Example

text
package_name-1.0.0-py3-none-any.whl

Important Point

A wheel's compatibility tags indicate which Python/platform combinations it supports.

Question 9: What is source distribution?

Ans

A source distribution, commonly an sdist, contains source files and packaging metadata from which a package can be built.

Example

text
package_name-1.0.0.tar.gz

Important Point

Installing an sdist can require build tools or compilation for packages with native extensions.

Question 10: What is virtual environment activation?

Ans

Activation changes shell commands such as python and pip to use the virtual environment's interpreter by adjusting environment variables such as PATH.

Example

bash
# Unix-like shells source .venv/bin/activate

Important Point

Activation is a convenience; you can also invoke the environment's interpreter directly.

Question 11: What is dependency conflict?

Ans

A dependency conflict occurs when installed packages require incompatible versions of the same dependency.

Example

text
Package A -> library >=2 Package B -> library <2

Important Point

Use isolated environments and a resolver/lock strategy to make dependency versions reproducible.

Question 12: What is dependency injection in Python?

Ans

Dependency injection means passing dependencies into a component instead of creating them internally, making code easier to test and replace.

Example

python
class OrderService: def __init__(self, payment_gateway): self.payment_gateway = payment_gateway service = OrderService(FakePaymentGateway())

Important Point

Python's flexible objects make simple constructor injection sufficient for many projects; a DI framework is not always necessary.

Question 13: What is pathlib Path.exists()?

Ans

Path.exists() checks whether a filesystem path exists.

Example

python
from pathlib import Path p = Path("data.txt") print(p.exists())

Important Point

Existence checks can race with later operations; for critical file operations, handle the actual operation's exception as well.

Question 14: What is os.environ?

Ans

os.environ exposes the process environment variables as a mapping-like object.

Example

python
import os mode = os.environ.get("APP_MODE", "dev") print(mode)

Important Point

Environment variables are strings; convert them to the required type.

Question 15: What is subprocess?

Ans

subprocess lets Python start and communicate with external processes.

Example

python
import subprocess result = subprocess.run(["python", "--version"], capture_output=True, text=True) print(result.stdout or result.stderr)

Important Point

Prefer argument lists over shell command strings and be careful with `shell=True` when inputs are untrusted.

Question 16: What is sys.argv?

Ans

sys.argv contains command-line arguments passed to a Python script, with the script name normally at index zero.

Example

python
import sys print(sys.argv)

Important Point

For nontrivial command-line interfaces, argparse provides parsing, validation, and help output.

Question 17: What is sys.path?

Ans

sys.path is the list of locations Python searches for modules during imports.

Example

python
import sys print(sys.path)

Important Point

Changing sys.path at runtime can hide packaging problems; prefer proper packages and environment configuration.

Continue Your Preparation