[Python] How to prevent vscode from importing global python env site-packages when using pixi...

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    situation


    • I created a new python project with pixi venv.
      I have this isolated venv, including python.
      I installed pandas etc.
      I didnt install plotly etc.


    • I have python installed globally.
      The global python environment has plotly installed.
    problem


    • Now I open the project in vscode.


    • If I try to import plotly.
      It wont show any error.
      If I click into it, it goes to the global python env site-packages.


    • Even If I try to import pandas.
      Same thing happens.


    • [​IMG]
    desire

    • I want vscode to truly isolate the venv.
      Stop it from importing from the global python site-packages.
      Just show an error if that happens in my code.
      I cant.
    attempts

    • I tried with PYTHONNOUSERSITE=1 wont work.
      • wont work for vscode ide
      • but seems working in terminal (but this is not enough)
    • include-system-site-packages = false doesnt exist for pixi.
    compare

    • if i create a project with .venv, no such problem, its truly isolated.
    • if i create a project with .conda, some project has no such problem, (the base env), but others seems have the same problem
    • if i create a project with .pixi, have the problem
    diagnostics


    To show where they are imported:

    import sys
    import os

    # Print the executable python thinks it's running
    print("--- Python Executable ---")
    print(sys.executable)
    print("-" * 25)

    # Print all the paths python will search for modules
    print("\n--- Module Search Paths (sys.path) ---")
    for path in sys.path:
    print(path)
    print("-" * 25)

    # Check if the PYTHONPATH variable is set
    print("\n--- PYTHONPATH Environment Variable ---")
    py_path = os.environ.get('PYTHONPATH')
    if py_path:
    print(py_path.replace(':', '\n')) # Use ';' for Windows if needed
    else:
    print("PYTHONPATH is not set.")
    print("-" * 25)

    # Try the import and see where it comes from

    try:
    import pandas
    print("\n--- 'pandas' was found here: ---")
    print(pandas.__file__)
    except ImportError:
    print("\n--- 'pandas' could not be imported. ---")

    try:
    import plotly
    print("\n--- 'plotly' was found here: ---")
    print(plotly.__file__)
    except ImportError:
    print("\n--- 'plotly' could not be imported. ---")

    try:
    import torch
    print("\n--- 'torch' was found here: ---")
    print(torch.__file__)
    except ImportError:
    print("\n--- 'torch' could not be imported. ---")



    Output:

    D:\usp\uhpsj\study\build_llm_wsp\build_llm>D:/usp/uhpsj/study/build_llm_wsp/build_llm/.pixi/envs/default/python.exe d:/usp/uhpsj/study/build_llm_wsp/build_llm/src/build_llm_demo/_demo/d2.py
    --- Python Executable ---
    D:\usp\uhpsj\study\build_llm_wsp\build_llm\.pixi\envs\default\python.exe
    -------------------------

    --- Module Search Paths (sys.path) ---
    d:\usp\uhpsj\study\build_llm_wsp\build_llm\src\build_llm_demo\_demo
    D:\usp\uhpsj\study\build_llm_wsp\build_llm\.pixi\envs\default\python311.zip
    D:\usp\uhpsj\study\build_llm_wsp\build_llm\.pixi\envs\default\DLLs
    D:\usp\uhpsj\study\build_llm_wsp\build_llm\.pixi\envs\default\Lib
    D:\usp\uhpsj\study\build_llm_wsp\build_llm\.pixi\envs\default
    C:\Users\nshln\AppData\Roaming\Python\Python311\site-packages
    D:\usp\uhpsj\study\build_llm_wsp\build_llm\.pixi\envs\default\Lib\site-packages
    D:\usp\uhpsj\study\build_llm_wsp\build_llm\.pixi\envs\default\Lib\site-packages\win32
    D:\usp\uhpsj\study\build_llm_wsp\build_llm\.pixi\envs\default\Lib\site-packages\win32\lib
    D:\usp\uhpsj\study\build_llm_wsp\build_llm\.pixi\envs\default\Lib\site-packages\Pythonwin
    -------------------------

    --- PYTHONPATH Environment Variable ---
    PYTHONPATH is not set.
    -------------------------

    --- 'pandas' was found here: ---
    C:\Users\nshln\AppData\Roaming\Python\Python311\site-packages\pandas\__init__.py

    --- 'plotly' was found here: ---
    C:\Users\nshln\AppData\Roaming\Python\Python311\site-packages\plotly\__init__.py

    --- 'torch' was found here: ---
    C:\Users\nshln\AppData\Roaming\Python\Python311\site-packages\torch\__init__.py


    pixi.toml packages

    [workspace]
    authors = ["norlz <norlz@gitkra.com>"]
    channels = ["conda-forge"]
    name = "build-llm-demo"
    platforms = ["win-64"]
    version = "0.1.0"

    [tasks]

    [system-requirements]
    cuda = "12.0"

    [dependencies]
    python = "3.11.*"
    pandas = "<=2.1.4"
    pydantic-settings = ">=2.9.1,<3"
    ruff = ">=0.12.0,<0.13"
    pyright = ">=1.1.402,<2"
    pytest = ">=8.4.1,<9"
    pytorch-gpu = "*"
    cuda-version = "12.6.*"

    [pypi-dependencies]
    fastapi = { version = ">=0.115.13, <0.116", extras = ["standard"] }
    uvicorn = { version = ">=0.34.3, <0.35", extras = ["standard"] }
    gunicorn = ">=23.0.0, <24"

    Continue reading...

Compartilhe esta Página