[Python] Python packages installed from .whl files not importable in other scripts or notebooks

Discussão em 'Python' iniciado por Stack, Julho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working in a restricted environment where pip install from the internet is not allowed. To work around this, I created a script that installs all required Python packages from .whl files stored in a synced SharePoint folder.

    Here’s the script I’m using:

    import os
    import subprocess
    import sys

    def install_whl_packages(directory):
    whl_files = [f for f in os.listdir(directory) if f.endswith('.whl')]

    if not whl_files:
    print("No .whl files found in the directory.")
    return

    for whl in whl_files:
    whl_path = os.path.join(directory, whl)
    print(f"Installing {whl}...")
    try:
    subprocess.check_call([sys.executable, "-m", "pip", "install", "--user", "--no-index", "--no-deps", whl_path])
    print(f"Successfully installed {whl}")
    except subprocess.CalledProcessError as e:
    print(f"Failed to install {whl}: {e}")

    if __name__ == "__main__":
    sharepoint_folder = r"C:..."
    install_whl_packages(sharepoint_folder)


    The script runs successfully and installs the packages. However, when I try to use these packages in other scripts or Jupyter notebooks (e.g., import pandas as pd), I get ModuleNotFoundError and it prompted me to install that packages.

    I suspect this is a sys.path issue where Python isn't looking in the user site-packages directory by default.

    My Questions:

    1. How can I ensure that packages installed with --user are always available in any Python script or notebook?
    2. Is there a way to make this path persistent across all environments (e.g., Jupyter, VS Code, CLI)?
    3. Would creating a .pth file help, and if so, where should it be placed and what should it contain?

    Any help or best practices would be appreciated!

    Continue reading...

Compartilhe esta Página