6.1 KiB
MOTM Application - Virtual Environment Guide
🎯 Why Use Virtual Environments?
Virtual environments provide isolated Python environments for your projects, which means:
- ✅ No conflicts between different project dependencies
- ✅ Reproducible environments across different machines
- ✅ Clean separation of project requirements
- ✅ Easy deployment with exact dependency versions
🚀 Quick Start
Windows Users
-
Open Command Prompt (not PowerShell initially)
-
Navigate to motm_app directory:
cd path\to\motm_app -
Create virtual environment:
python -m venv venvIf this fails, try
py -m venv venvor find your Python installation -
Activate virtual environment:
venv\Scripts\activate.batYou should see
(venv)at the beginning of your command prompt -
Install dependencies:
pip install --upgrade pip pip install -r requirements.txt -
Run the application:
python main.py
Mac/Linux Users
-
Open Terminal
-
Navigate to motm_app directory:
cd path/to/motm_app -
Create virtual environment:
python3 -m venv venv -
Activate virtual environment:
source venv/bin/activateYou should see
(venv)at the beginning of your command prompt -
Install dependencies:
pip install --upgrade pip pip install -r requirements.txt -
Run the application:
python main.py
📁 What Gets Created
When you create a virtual environment, you'll see a new venv folder:
motm_app/
├── venv/ # Virtual environment (don't edit this)
│ ├── Scripts/ # Windows activation scripts
│ ├── bin/ # Unix activation scripts
│ ├── Lib/ # Installed packages
│ └── pyvenv.cfg # Environment configuration
├── main.py # Your application
├── requirements.txt # Dependencies list
└── ... # Other files
🔧 Daily Usage
Starting Your Work Session
-
Activate the virtual environment:
- Windows:
venv\Scripts\activate.bat - Mac/Linux:
source venv/bin/activate
- Windows:
-
Verify activation - you should see
(venv)in your prompt:(venv) C:\path\to\motm_app> -
Run your application:
python main.py
Ending Your Work Session
Deactivate when done:
deactivate
The (venv) indicator will disappear
📦 Managing Dependencies
Installing New Packages
-
Activate virtual environment first
-
Install the package:
pip install package-name -
Update requirements.txt:
pip freeze > requirements.txt -
Commit the updated requirements.txt to version control
Updating Existing Packages
pip install --upgrade package-name
Viewing Installed Packages
pip list
Removing Packages
pip uninstall package-name
🛠️ Troubleshooting
"Python not found" Error
Windows:
- Install Python from python.org
- During installation, check "Add Python to PATH"
- Try using
pycommand instead ofpython
Mac/Linux:
- Install Python 3:
brew install python3(Mac) or use package manager - Use
python3instead ofpython
Virtual Environment Won't Activate
Windows:
# Try running Command Prompt as Administrator
# Or use PowerShell with:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Permission Errors:
# Remove and recreate
rmdir /s venv # Windows
rm -rf venv # Mac/Linux
python -m venv venv # Recreate
Packages Not Found After Installation
- Make sure virtual environment is activated (look for
(venv)) - Check if packages are installed:
pip list - Reinstall requirements:
pip install -r requirements.txt
Application Won't Start
- Activate virtual environment
- Check all dependencies:
pip install -r requirements.txt - Test imports:
python -c "import flask" - Check error messages in the console
🔄 Working with Teams
Sharing Your Environment
- Commit
requirements.txtto version control - Don't commit the
venvfolder (add to.gitignore)
Setting Up on New Machine
- Clone the repository
- Create virtual environment:
python -m venv venv - Activate it:
venv\Scripts\activate.bat(Windows) orsource venv/bin/activate(Unix) - Install dependencies:
pip install -r requirements.txt
🚀 Deployment Considerations
Production Environment
- Use the same Python version as development
- Install exact dependencies:
pip install -r requirements.txt - Set up environment variables for database connections
- Use a proper web server (nginx, Apache)
- Consider using Docker for containerization
Docker Example
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "main.py"]
💡 Best Practices
- Always use virtual environments for Python projects
- Activate before working on the project
- Keep requirements.txt updated when adding packages
- Use descriptive commit messages when updating dependencies
- Test your application after installing new packages
- Document any special setup requirements
🆘 Getting Help
If you're still having issues:
- Check Python version: Should be 3.7 or higher
- Verify virtual environment: Should be activated (see
(venv)in prompt) - Review error messages: Look for specific error details
- Test step by step: Create a simple test script to verify Python works
- Check internet connection: Required for installing packages
Remember: The virtual environment isolates your project dependencies from your system Python installation, making your project more reliable and portable! 🎉