microsoft / vscode-python-tools-extension-template

Template for creating VS Code extensions for python tools.
https://code.visualstudio.com/api/advanced-topics/python-extension-template
Other
132 stars 42 forks source link

Numpy error when using bundled libs from different python version #209

Open inimaz opened 2 months ago

inimaz commented 2 months ago

Hello team,

thanks for this template! I am building an extension using this template, see the repo. And it works, but only if the python version is the same as the python version used when it was built.

If it is not the case, when the user's python (python 3.12) has not the same version as the one that built it (python 3.11). I get the following error from numpy:

 File "/home/inigo/.vscode-server/extensions/codecarbon.codecarbon-0.1.0/bundled/libs/codecarbon/core/cpu.py", line 13, in <module>
    import pandas as pd
  File "/home/inigo/.vscode-server/extensions/codecarbon.codecarbon-0.1.0/bundled/libs/pandas/__init__.py", line 19, in <module>
    raise ImportError(
ImportError: Unable to import required dependencies:
numpy: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.

What do you think I should do? Build an extension per python version and then detect which one to load?

Thanks!

inimaz commented 2 months ago

Exact line in my repo that triggers this error https://github.com/inimaz/vscode-extension-codecarbon/blob/main/bundled/tool/lsp_server.py#L210 . This line imports codecarbon, codecarbon imports pandas and pandas imports numpy ==> the error appears

karthiknadig commented 2 months ago

@inimaz You will need to use platform specific builds. The template was designed with universal builds in mind. But, for scenarios like this where you need platform specific binaries like with numpy, you will need to change the build steps to install numpy with the binaries. You will also need to change packaging the vsix to use platform target (see vsce command line for more details)

Here is an 3rd party extension that builds and ships native binaries based on this template. https://github.com/astral-sh/ruff-vscode

inimaz commented 2 months ago

Thanks for the response and the link! @karthiknadig

I agree I will need to do this platform specific builds. Thanks for the tip! It does not solve this problem though, in my case it is the same platform (Windows laptop) just changing the python version. Do I need to do a full matrix of builds per platform per python version?

For example if I support windows and mac, and python 3.7 and python 3.8... Do I need to do:

karthiknadig commented 2 months ago

You will have to pull the native builds for each python ABI (i.e, py37, py38, py39, etc) at build time if you want to package it.

We do this for debugpy, in the python debugger extension. We pull each of the compiled variants for the debugger.