Visual Studio Code

 

Why use a terminal (not the Python interpreter) for creating and managing environments?

  1. Terminal vs Python Interpreter:

    • The Python interpreter is an interactive environment (the >>> prompt) where you can run Python code line-by-line. It is not designed to execute system-level commands like creating a virtual environment, installing packages, or managing files.
    • The terminal (Command Prompt, PowerShell, Bash, etc.) is the appropriate tool for running system-level commands such as python -m venv, pip install, or pytest.
  2. Managing Virtual Environments:

    • To create a virtual environment (python -m venv myenv) or activate/deactivate it, you need to execute commands at the system level. These commands cannot be executed inside the Python interpreter.
    • Installing packages with pip install also requires a terminal, as it interacts with the system’s package manager.
  3. Executing Python Scripts:

    • You run Python scripts (python myfile.py) from the terminal, not the Python interpreter.
    • Running a script from the terminal ensures it uses the Python interpreter and dependencies from the active virtual environment.

What Happens if You Use the Python Interpreter?

If you open the Python interpreter (>>>) and try to run commands like python -m venv myenv or pip install pandas, it will raise a SyntaxError because these commands are not valid Python code—they are system-level commands.


Best Practice

  • Use the terminal to:
    • Create and activate virtual environments.
    • Install or manage Python packages.
    • Run Python programs and tests.
  • Use the Python interpreter for quick experiments or debugging Python code.


====================================================================


Creating a Virtual Environment in Visual Studio Code

Follow these steps to create and manage a virtual environment in Visual Studio Code for Python projects:


1. Open Command Prompt, Bash, or PowerShell

Do not use the Python terminal for these commands. You will execute them in a terminal like Bash, PowerShell, or Command Prompt.

2. Verify Python Installation

Ensure Python is installed on your system:

bash

python --version

3. Verify pip Installation

pip (Python's package manager) is pre-installed in Python 3. Verify it with:

bash

python -m pip --version

4. Install or Upgrade pip (if required)

Install pip if it's missing:

bash

python -m ensurepip

Upgrade pip to the latest version:

bash

python -m ensurepip --upgrade

5. Create a Virtual Environment

Create a virtual environment for your project using the following command:

bash

python -m venv myenv

6. Activate the Virtual Environment

Activate the virtual environment:

  • On Windows:
    bash

    myenv\Scripts\activate
  • On Mac/Linux:
    bash

    source myenv/bin/activate

7. Install Required Packages

Once the virtual environment is activated, you can install the required Python libraries:

bash

pip install requests pandas notebook

8. Run Jupyter Notebook

To launch Jupyter Notebook (installed in step 7):

bash

jupyter notebook

To quit Jupyter Notebook, press Ctrl+C in the terminal.

9. Check Installed Packages

You can see all installed libraries in the virtual environment using:

  • List installed packages:
    bash

    pip list
  • Freeze the package list (useful for creating requirements.txt):
    bash

    pip freeze

10. Deactivate the Virtual Environment

When you’re done, deactivate the virtual environment:

bash

deactivate

Executing Python Programs

  1. Run a Python File:
    Execute your Python script using the following command:

    bash

    python name_of_the_file.py
  2. Run Pytest for Testing:

    • Install pytest first (if not installed):
      bash

      pip install pytest
    • Run pytest on your test file:
      bash

      pytest test_of_python_file.py

Summary

  • Always use a terminal (not the Python interpreter) for creating and managing environments.
  • After setting up, activate the virtual environment, install necessary packages, and execute your Python files and test scripts.

====================================

Creating a Virtual Environment in Visual Studio Code - summary

  1. Open Terminal
    Open Command Prompt, Bash, or PowerShell. Do not use the Python interpreter for these commands.

  2. Verify Python Installation
    Check if Python is installed:
    'python --version'

  3. Verify pip Installation
    Ensure pip (Python's package manager) is installed:
    'python -m pip --version'

  4. Install pip (if missing)
    Install pip if it is not available:
    'python -m ensurepip'

  5. Upgrade pip (optional)
    Update pip to the latest version:
    'python -m ensurepip --upgrade'

  6. Create a Virtual Environment
    Create a virtual environment for your project:
    'python -m venv myenv'

  7. Activate the Virtual Environment
    Activate the virtual environment:

    • On Windows: 'myenv\Scripts\activate'
    • On Mac/Linux: 'source myenv/bin/activate'
  8. Install Required Packages
    Install necessary Python libraries in the virtual environment:
    'pip install requests pandas notebook'

  9. Run Jupyter Notebook
    Launch Jupyter Notebook:
    'jupyter notebook'
    To quit Jupyter Notebook, press Ctrl+C in the terminal.

  10. Check Installed Packages

    • List installed packages: 'pip list'
    • Freeze package list (useful for creating requirements.txt): 'pip freeze'
  11. Deactivate the Virtual Environment
    Deactivate the virtual environment when you're done:
    'deactivate'


Executing Python Programs

  1. Run a Python File
    Execute your Python script:
    'python name_of_the_file.py'

  2. Run Pytest for Testing

    • Install pytest (if not already installed): 'pip install pytest'
    • Run pytest on your test file: 'pytest test_of_python_file.py'
Previous Post Next Post