Why use a terminal (not the Python interpreter) for creating and managing environments?
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, orpytest.
- The Python interpreter is an interactive environment (the
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 installalso requires a terminal, as it interacts with the system’s package manager.
- To create a virtual environment (
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.
- You run Python scripts (
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:
3. Verify pip Installation
pip (Python's package manager) is pre-installed in Python 3. Verify it with:
4. Install or Upgrade pip (if required)
Install pip if it's missing:
Upgrade pip to the latest version:
5. Create a Virtual Environment
Create a virtual environment for your project using the following command:
6. Activate the Virtual Environment
Activate the virtual environment:
- On Windows:
- On Mac/Linux:
7. Install Required Packages
Once the virtual environment is activated, you can install the required Python libraries:
8. Run Jupyter Notebook
To launch Jupyter Notebook (installed in step 7):
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:
- Freeze the package list (useful for creating
requirements.txt):
10. Deactivate the Virtual Environment
When you’re done, deactivate the virtual environment:
Executing Python Programs
Run a Python File:
Execute your Python script using the following command:Run Pytest for Testing:
- Install pytest first (if not installed):
- Run pytest on your test file:
- Install pytest first (if not installed):
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
-
Open Terminal
Open Command Prompt, Bash, or PowerShell. Do not use the Python interpreter for these commands. -
Verify Python Installation
Check if Python is installed:
'python --version' -
Verify pip Installation
Ensure pip (Python's package manager) is installed:
'python -m pip --version' -
Install pip (if missing)
Install pip if it is not available:
'python -m ensurepip' -
Upgrade pip (optional)
Update pip to the latest version:
'python -m ensurepip --upgrade' -
Create a Virtual Environment
Create a virtual environment for your project:
'python -m venv myenv' -
Activate the Virtual Environment
Activate the virtual environment:- On Windows:
'myenv\Scripts\activate' - On Mac/Linux:
'source myenv/bin/activate'
- On Windows:
-
Install Required Packages
Install necessary Python libraries in the virtual environment:
'pip install requests pandas notebook' -
Run Jupyter Notebook
Launch Jupyter Notebook:
'jupyter notebook'
To quit Jupyter Notebook, pressCtrl+Cin the terminal. -
Check Installed Packages
- List installed packages:
'pip list' - Freeze package list (useful for creating
requirements.txt):'pip freeze'
- List installed packages:
-
Deactivate the Virtual Environment
Deactivate the virtual environment when you're done:
'deactivate'
Executing Python Programs
-
Run a Python File
Execute your Python script:
'python name_of_the_file.py' -
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'
- Install pytest (if not already installed):