Codectionary / Developer documentation / Python

Python Virtual Environments

Give each project its own packages and use pip through the same interpreter that runs your code.

What an environment does

A virtual environment is a folder containing a Python executable or links to one, plus project-specific installed packages. Two projects can use different package versions without changing each other. It is not a virtual machine or a security sandbox.

Install Python first. In your python-practice folder, create .venv using the interpreter you intend to use. Your source files belong beside .venv, not inside it. venv uses the Python version that creates it; it does not download a different language version.

Windows PowerShell

python -m venv .venv

macOS/Linux terminal

python3 -m venv .venv

Activate, or use the executable directly

Activation adjusts this terminal’s command lookup so python points into .venv. It is convenient, not mandatory. Choose the command for your shell; opening another terminal does not automatically activate this one’s environment.

Windows PowerShell

.\.venv\Scripts\Activate.ps1

Windows Command Prompt

.venv\Scripts\activate.bat

macOS/Linux — bash or zsh

source .venv/bin/activate

After activation — verify

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

Install and inspect a package

The first program needs no extra packages. When you need one, use python -m pip so the installer belongs to the same interpreter. For example, requests is a third-party HTTP library; installing it downloads code from the configured package index. Install only packages you intend to use.

The commands below run after activation. The freeze command writes installed versions to requirements.txt; do not overwrite an existing requirements file without reviewing it. A version list is useful but is not a guarantee of identical behavior on every platform.

Activated environment — terminal

python -m pip install requests
python -m pip show requests
python -m pip freeze > requirements.txt

Recreate packages in an activated environment

python -m pip install -r requirements.txt

Troubleshooting and finishing

If PowerShell blocks Activate.ps1, you can use the environment directly without changing execution policy: .\.venv\Scripts\python.exe hello.py. Install through .\.venv\Scripts\python.exe -m pip install requests. On macOS/Linux the equivalent executable is .venv/bin/python.

If an import works in the terminal but not the editor, compare sys.executable in both. Select .venv in the editor. Do not solve this by repeatedly installing into unrelated global interpreters.

Run deactivate to leave an activated environment. Exclude .venv from version control; keep source and dependency declarations. Recreate environments after moving a project or changing its base interpreter rather than copying the folder between computers.

Syntax

print("Hello, Python!")

Examples

Your first Python program

Save as hello.py in your project folder. Expected output: Hello, Python!

print("Hello, Python!")
  1. print is a built-in function that writes to the output stream.
  2. The quoted text is a string. The quotes are not printed.
  3. Parentheses pass that string to print. No extra package is required.

Best practices

  • Save your work before running it, and check the output after each small change.
  • Keep your learning project in its own folder. Do not name files after standard modules such as random.py or json.py.

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading