Skip to content

NLTK import security check blocks startup on Linux (Python 3.10) #587

Description

@q514168795

Bug Description

On Linux, VideoLingo fails to start with the following error when the current working directory contains any .py file (or when Streamlit's subprocess workers inherit the parent process environment):

ImportError: Blocked import of regex from current working directory for security reasons.

File "videolingo/.venv/lib/python3.10/site-packages/nltk/inisec.py", line 128, in find_spec
    raise ImportError(
        f"Blocked import of {fullname} from current working directory "
        "for security reasons. Use '-P' or set PYTHONSAFEPATH to prevent "
        "Python from searching the current working directory."
    )

Root Cause

NLTK v4.x added an import security hook (nltk/inisec.py) that blocks modules resolved from the current working directory when the import is initiated by NLTK or its dependencies (including g2p_ennltkregex).

The NLTK docs state:

-P and PYTHONSAFEPATH require Python 3.11+. On Python 3.10 they have no effect.

The NLTK hook itself exposes a workaround via environment variable (confirmed in nltk/inisec.py line 156):

if os.environ.get("NLTK_DISABLE_IMPORT_SECURITY") != "1":
    _install()

However, os.environ.setdefault("PYTHONSAFEPATH", "1") on line 149 runs after the hook is installed, so setdefault cannot undo the check in the current interpreter — it's purely for child process inheritance.

Reproduction Steps

  1. Install VideoLingo on Linux with uv + Python 3.10
  2. Run: streamlit run st.py
  3. Access the web UI — the page loads
  4. Click any button or refresh — the subprocess worker crashes with the error above

Workaround

Setting NLTK_DISABLE_IMPORT_SECURITY=1 in the environment before launching Streamlit works:

NLTK_DISABLE_IMPORT_SECURITY=1 streamlit run st.py

Suggested Fix

Add this at the very top of st.py (before any other imports):

import os
os.environ.setdefault("NLTK_DISABLE_IMPORT_SECURITY", "1")

Or document it prominently in the README for Linux/macOS users on Python 3.10.

Environment

  • OS: Ubuntu 24.04 (Linux)
  • Python: 3.10.19 (via uv)
  • NLTK: bundled via requirements
  • Streamlit: latest
  • VideoLingo: 3.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions