diff --git a/README.md b/README.md index c07a6ad..900e72c 100644 --- a/README.md +++ b/README.md @@ -176,7 +176,7 @@ IR codes are mapped in `/etc/video_player/ir_mapping.json`: 1. **Start IR learning mode**: ```bash - sudo python3 /opt/video_player/ir_remote.py + sudo /opt/video_player/venv/bin/python /opt/video_player/ir_remote.py ``` 2. **Press buttons** on your remote control diff --git a/install.sh b/install.sh index 0c27d9d..4b759ea 100755 --- a/install.sh +++ b/install.sh @@ -117,13 +117,20 @@ install_packages() { install_python_dependencies() { print_status "Installing Python dependencies..." - # Install pip packages - pip3 install --upgrade pip - pip3 install \ + # Create virtual environment + python3 -m venv "$INSTALL_DIR/venv" + + # Activate virtual environment and install packages + source "$INSTALL_DIR/venv/bin/activate" + + # Upgrade pip in virtual environment + pip install --upgrade pip + + # Install packages in virtual environment + pip install \ python-vlc \ python-dotenv \ psutil \ - pathlib \ PyYAML \ RPi.GPIO \ pigpio \ @@ -133,7 +140,10 @@ install_python_dependencies() { numpy \ opencv-python-headless - print_success "Python dependencies installed" + # Deactivate virtual environment + deactivate + + print_success "Python dependencies installed in virtual environment" } # Function to create directories @@ -166,6 +176,7 @@ copy_files() { cp video_player.py "$INSTALL_DIR/" cp ir_remote.py "$INSTALL_DIR/" cp config_manager.py "$INSTALL_DIR/" + cp test_venv_setup.py "$INSTALL_DIR/" # Copy USB auto-mount script cp usb_automount.sh "$INSTALL_DIR/" @@ -472,6 +483,12 @@ EOF cat > /usr/local/bin/video-player-usb << 'EOF' #!/bin/bash /opt/video_player/usb_automount.sh "$@" +EOF + + # Create test script + cat > /usr/local/bin/video-player-test << 'EOF' +#!/bin/bash +/opt/video_player/venv/bin/python /opt/video_player/test_venv_setup.py EOF # Set permissions @@ -498,15 +515,17 @@ display_summary() { echo "Restart: video-player-restart" echo "Status: video-player-status" echo "Logs: video-player-logs" + echo "Test: video-player-test" echo "USB: video-player-usb {scan|mount-all}" echo echo "Next Steps:" echo "===========" - echo "1. Add video files to: $VIDEO_FOLDER" - echo "2. Insert USB drives - they will auto-mount to: /media/usb/" - echo "3. Configure IR remote codes in: $CONFIG_DIR/ir_mapping.json" - echo "4. Start the service: video-player-start" - echo "5. Check status: video-player-status" + echo "1. Test the installation: video-player-test" + echo "2. Add video files to: $VIDEO_FOLDER" + echo "3. Insert USB drives - they will auto-mount to: /media/usb/" + echo "4. Configure IR remote codes in: $CONFIG_DIR/ir_mapping.json" + echo "5. Start the service: video-player-start" + echo "6. Check status: video-player-status" echo echo "For more information, see the documentation in $INSTALL_DIR" } diff --git a/test_venv_setup.py b/test_venv_setup.py new file mode 100644 index 0000000..2d0df21 --- /dev/null +++ b/test_venv_setup.py @@ -0,0 +1,141 @@ +#!/usr/bin/env python3 +""" +Test script to verify virtual environment setup +This script tests if the virtual environment is properly configured +""" + +import sys +import os +import subprocess +from pathlib import Path + +def test_virtual_environment(): + """Test if virtual environment is properly set up""" + print("Testing Virtual Environment Setup") + print("=" * 40) + + # Check if we're in a virtual environment + if hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix): + print("✅ Running in virtual environment") + print(f" Python executable: {sys.executable}") + print(f" Virtual env path: {sys.prefix}") + else: + print("❌ Not running in virtual environment") + print(f" Python executable: {sys.executable}") + return False + + # Test required packages + required_packages = [ + 'vlc', + 'dotenv', + 'psutil', + 'yaml', + 'RPi.GPIO', + 'pigpio', + 'requests', + 'PIL', + 'numpy', + 'cv2' + ] + + print("\nTesting Required Packages:") + print("-" * 30) + + missing_packages = [] + for package in required_packages: + try: + if package == 'yaml': + import yaml + elif package == 'PIL': + from PIL import Image + elif package == 'cv2': + import cv2 + else: + __import__(package) + print(f"✅ {package}") + except ImportError: + print(f"❌ {package} - Missing") + missing_packages.append(package) + + if missing_packages: + print(f"\n❌ Missing packages: {', '.join(missing_packages)}") + return False + else: + print("\n✅ All required packages are available") + + # Test VLC + print("\nTesting VLC:") + print("-" * 15) + try: + import vlc + instance = vlc.Instance() + print("✅ VLC instance created successfully") + except Exception as e: + print(f"❌ VLC test failed: {e}") + return False + + # Test GPIO (if on Raspberry Pi) + print("\nTesting GPIO:") + print("-" * 15) + try: + import RPi.GPIO as GPIO + print("✅ RPi.GPIO module available") + except ImportError: + print("⚠️ RPi.GPIO not available (may not be on Raspberry Pi)") + except Exception as e: + print(f"❌ GPIO test failed: {e}") + return False + + print("\n✅ Virtual environment setup test passed!") + return True + +def test_installation_paths(): + """Test if installation paths are correct""" + print("\nTesting Installation Paths") + print("=" * 30) + + install_dir = Path("/opt/video_player") + venv_dir = install_dir / "venv" + config_dir = Path("/etc/video_player") + + paths_to_check = [ + (install_dir, "Installation directory"), + (venv_dir, "Virtual environment directory"), + (venv_dir / "bin" / "python", "Virtual environment Python"), + (config_dir, "Configuration directory"), + (install_dir / "video_player.py", "Main video player script"), + (install_dir / "ir_remote.py", "IR remote script"), + (install_dir / "config_manager.py", "Config manager script") + ] + + all_good = True + for path, description in paths_to_check: + if path.exists(): + print(f"✅ {description}: {path}") + else: + print(f"❌ {description}: {path} - Missing") + all_good = False + + return all_good + +def main(): + """Main test function""" + print("Video Player Virtual Environment Test") + print("=" * 50) + + # Test installation paths + paths_ok = test_installation_paths() + + # Test virtual environment + venv_ok = test_virtual_environment() + + print("\n" + "=" * 50) + if paths_ok and venv_ok: + print("🎉 All tests passed! Virtual environment is properly configured.") + return 0 + else: + print("❌ Some tests failed. Please check the installation.") + return 1 + +if __name__ == "__main__": + sys.exit(main()) diff --git a/video-player.service b/video-player.service index b79af80..cf26b2b 100644 --- a/video-player.service +++ b/video-player.service @@ -9,7 +9,7 @@ Type=simple User=root Group=root WorkingDirectory=/opt/video_player -ExecStart=/usr/bin/python3 /opt/video_player/video_player.py +ExecStart=/opt/video_player/venv/bin/python /opt/video_player/video_player.py ExecReload=/bin/kill -HUP $MAINPID Restart=always RestartSec=10