0

situation

  • I created a new Python project with pixi venv.
    I have this isolated venv, including Python.
    I installed pandas etc.
    I didn't install plotly etc.

  • I have Python installed globally.
    The global Python environment has plotly installed.

problem

  • Now I open the project in VS Code.

  • If I try to import plotly.
    It won't 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.

  • enter image description here

desire

  • I want VS Code 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.

  • Note: I need an visible error in the IDE, not just when running in terminal.

attempts

  • I tried with PYTHONNOUSERSITE=1,
    • won't work for VS Code IDE
    • only works in terminal (this is not enough)
  • include-system-site-packages = false doesn't exist for pixi.

compare

  • if I create a project with .venv, no such problem, it's truly isolated, and VS Code will show an error when importing a package not in venv (regardless of its in global Python env site-packages or not).
  • 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 <[email protected]>"]
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"

2
  • I think it has something to do with the python.exe binary too. I tried to switch the .exe file, seems getting different results. Not sure. Commented Jun 23 at 15:34
  • My current workaround is to NUKE the global python env site-packages folder... It will probably go bad... Commented Jun 23 at 15:37

2 Answers 2

0

You need to create the configure the launch.json to run the project.

Sample project structure:

pro1/
├── .vscode/
│   └── launch.json
├── venv/                   # virtual environment
├── app.py

And launch.json file:


{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Python: Launch Main",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/app.py",
        "console": "integratedTerminal",
        "env": {
          "PYTHONPATH": "${workspaceFolder}/src"
        },
        "python": "${workspaceFolder}/venv/bin/python"
      }
    ]
  }

Output:
enter image description here

Note: I installed the torch and plotly manually in venv.

Sign up to request clarification or add additional context in comments.

4 Comments

Did you try it and it worked? Note: Im talking about the ide.
Yes, I added the output for your reference.
But I need it to work for the IDE, show an linting error or something, just like pip venv does. Only working in terminal is not enough for me.
Also you are using pip venv. But Im asking for pixi.
0

I'm not sure if this also works with pixi.

If you are using the new version of VSCode (and the corresponding Python extension), you have to install the Python Environment (yes, it is a pre-release extension) and enable it by:

"python.useEnvironmentsExtension": true

Refer to this post: Reddit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.