Resolved

Python virtualenv module import fails despite successful pip installation

Python
Virtualenv
Dependency Management
Debugging
Module Resolution
Package Installation
Backend
Dev

ModuleNotFoundError occurs when importing installed packages in virtualenv due to Python version mismatch between system and virtual environment | System Python 3.13.7 vs virtualenv Python 3.12.12 causing path resolution issues

Building an automated video generation system using Python with PyTorch, WhisperX, MoviePy, and custom TTS pipelines, where the system combines text-to-speech with mechanical keyboard sound effects synchronized to video output.

Issue

The system failed with ModuleNotFoundError: No module named 'librosa' despite successful installation via uv pip install. The error occurred when running python main.py in a virtual environment where uv pip list confirmed librosa 0.11.0 was installed. The issue manifested as an import failure during execution while the package existed in the virtual environment’s site-packages.

Culprit

A Python version mismatch between the system Python (3.13.7) and the virtual environment (3.12.12) caused module resolution failures. The virtual environment was activated but:

  1. The which python command pointed to /usr/sbin/python (system Python 3.13.7)
  2. The virtualenv’s Python binary (./venv/bin/python) was correctly linked to Python 3.12.12
  3. System Python’s sys.path didn’t include the virtualenv’s site-packages directory
  4. The source .venv/bin/activate command wasn’t properly updating the shell’s PATH to prioritize the virtualenv’s Python

Solution

Correct Virtualenv Activation

Explicitly used the virtualenv’s Python binary (./venv/bin/python) instead of relying on system Python, ensuring proper path resolution:

./venv/bin/python main.py

Path Verification

Confirmed the virtualenv’s Python had correct paths:

./venv/bin/python -c "import sys; print('\n'.join(sys.path))"

This showed the virtualenv’s site-packages (/home/user/.local/lib/python3.12/site-packages) was properly included.

Import Correction

Fixed MoviePy import syntax from:

from moviepy.editor import VideoClip, AudioFileClip

to:

from moviepy import VideoClip, AudioFileClip

TTS Pipeline Implementation

Implemented proper Kokoro pipeline initialization:

from kokoro.pipeline import KPipeline
pipeline = KPipeline(lang_code='a')  # American English
generator = pipeline(text, voice='am_echo')

The solution required explicit virtualenv Python usage and careful import path management to resolve the version mismatch issues while maintaining all package dependencies.