Why Virtual Environments?
A virtual environment is an isolated Python environment that allows you to install packages without affecting your system’s Python installation.Benefits of Virtual Environments
- Isolation: Each project has its own dependencies
- System Safety: Prevents breaking system Python (critical on Ubuntu)
- Version Control: Different projects can use different package versions
- Clean Dependencies: Easy to track what your project needs
- Reproducibility: Share exact dependency versions with others
Ubuntu Virtual Environment Issues
The Problem
When the Grupo de Anda team first ran the code on Ubuntu, they encountered a critical issue:Ubuntu’s code includes parts written in Python. Adding or updating libraries in the system Python would break Ubuntu’s functionality.
The Solution
Always use virtual environments on Ubuntu. This keeps your project dependencies completely separate from the system Python.Creating a Virtual Environment
Navigate to Your Project
Open a terminal and navigate to your project directory:Replace
/ruta/a/tu/proyecto with the actual path to your Grupo de Anda project.Example:Create the Virtual Environment
Run the following command to create a virtual environment:Or on Linux:This creates a
.venv folder in your project directory containing the isolated Python environment.The
.venv name is a convention. You can use any name, but .venv is recommended as it’s commonly recognized and often automatically excluded from version control.Using Virtual Environments
Installing Packages
Once your virtual environment is activated, install packages normally:Deactivating
When you’re done working on the project:Reactivating
Every time you open a new terminal to work on your project, you need to reactivate: Windows:VS Code Integration
VS Code can automatically detect and use your virtual environment:Select the Virtual Environment
- Press
Ctrl+Shift+Pto open the command palette - Type “Python: Select Interpreter”
- Choose the interpreter from
.venv(it will show the path)
If VS Code doesn’t detect your
.venv, try reloading the window: Ctrl+Shift+P → “Developer: Reload Window”Platform-Specific Considerations
Ubuntu
Ensure you have the venv package installed:Windows
PowerShell Execution Policy
If you get an error about execution policies when activating:macOS
Virtual environments work the same as Linux. Use:Best Practices
- Always activate before installing packages or running code
- One virtual environment per project
- Don’t commit
.venvto version control (add to.gitignore) - Document dependencies in
requirements.txt(see Dependencies guide) - Recreate easily from
requirements.txton new systems
Common Workflows
Starting Work on the Project
Installing New Dependencies
Running Your Code
Next Steps
Now that you have a virtual environment set up:- Install project dependencies
- Set up Arduino hardware (for hardware projects)
Troubleshooting
Command not found: python
Command not found: python
Linux: Use
python3 instead of python:Activation script not found
Activation script not found
Make sure you’re in the project directory where you created If
.venv:.venv doesn’t exist, recreate it:PowerShell execution policy error
PowerShell execution policy error
Run PowerShell as Administrator and execute:
Virtual environment not detected in VS Code
Virtual environment not detected in VS Code
- Ensure
.venvexists in your project root - Reload VS Code:
Ctrl+Shift+P→ “Developer: Reload Window” - Manually select interpreter:
Ctrl+Shift+P→ “Python: Select Interpreter” → Choose.venv
Packages still installing system-wide
Packages still installing system-wide
Verify your virtual environment is activated:If not activated, activate it:
