admin管理员组

文章数量:1435842

I have a Python project in Ubuntu 24.04.1 LTS and I have a DevContainer in VSC with Debian GNU/Linux 11, the problem is that Pylance is flagging import streamlit as st with Import "streamlit" could not be resolved. If I run the application from the terminal with streamlit run myfile.py, it runs perfectly, but launching the debugger raises this exception:

EDIT I reset the Docker image, and the exception changed to a simpler one:

/usr/bin/python3: No module named streamlit

This is my devcontainer.json:

{
    "build": {"dockerfile": "Dockerfile"},
    "customizations": {
        "vscode": {
            "settings": {},
            "extensions": [
                "ms-python.python",
                "ms-python.vscode-pylance"
            ]
        },
        "forwardPorts": [8501],
        "runArgs": ["--env-file",".devcontainer/devcontainer.env"]
    }
}

This is my Dockerfile:

FROM python:3.10-bullseye

COPY requirements.txt ./requirements.txt

RUN pip install oscrypto@git+.git@d5f3437ed24257895ae1edd9e503cfb352e635a8

# COPY src ./src
# WORKDIR /src
RUN pip install --no-cache-dir -r requirements.txt

ENV PYTHONPATH=/workspaces/my_project/src

EXPOSE 8001
CMD ["streamlit", "run", "view/frontend/main.py"]

And this is my launch.json:

{
    "version": "0.2.0",
    "configurations": [        
        {
            "name": "Python: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {"PYTHONPATH": "${workspaceFolder}/src"}
        },
        {
            "name": "Python:Streamlit",
            "type": "debugpy",
            "request": "launch",
            "module": "streamlit",
            "args": [
                "run",
                "${file}",
                "--server.port",
                "8501",
                "--server.fileWatcherType",
                "poll",
                "--server.address",
                "0.0.0.0"
            ],
            "cwd": "${workspaceFolder}/src",
            "env": {
                "PYTHONPATH": "${workspaceFolder}/src",
                "PYTHONHOME": "/usr/local/bin"
            }
        }
    ]
}

I have tried a lot of configurations, but I failed to find one that works.

EDIT I have pylance and streamlit workinng together in another computer with a VSC Dev Container over Windows, and they work perfectly.

This is what I get with python -m site:

sys.path = [
    '/workspaces/sacbeh',
    '/workspaces/sacbeh/src',
    '/usr/local/lib/python310.zip',
    '/usr/local/lib/python3.10',
    '/usr/local/lib/python3.10/lib-dynload',
    '/usr/local/lib/python3.10/site-packages',
]
USER_BASE: '/root/.local' (doesn't exist)
USER_SITE: '/root/.local/lib/python3.10/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

I have a Python project in Ubuntu 24.04.1 LTS and I have a DevContainer in VSC with Debian GNU/Linux 11, the problem is that Pylance is flagging import streamlit as st with Import "streamlit" could not be resolved. If I run the application from the terminal with streamlit run myfile.py, it runs perfectly, but launching the debugger raises this exception:

EDIT I reset the Docker image, and the exception changed to a simpler one:

/usr/bin/python3: No module named streamlit

This is my devcontainer.json:

{
    "build": {"dockerfile": "Dockerfile"},
    "customizations": {
        "vscode": {
            "settings": {},
            "extensions": [
                "ms-python.python",
                "ms-python.vscode-pylance"
            ]
        },
        "forwardPorts": [8501],
        "runArgs": ["--env-file",".devcontainer/devcontainer.env"]
    }
}

This is my Dockerfile:

FROM python:3.10-bullseye

COPY requirements.txt ./requirements.txt

RUN pip install oscrypto@git+https://github/wbond/oscrypto.git@d5f3437ed24257895ae1edd9e503cfb352e635a8

# COPY src ./src
# WORKDIR /src
RUN pip install --no-cache-dir -r requirements.txt

ENV PYTHONPATH=/workspaces/my_project/src

EXPOSE 8001
CMD ["streamlit", "run", "view/frontend/main.py"]

And this is my launch.json:

{
    "version": "0.2.0",
    "configurations": [        
        {
            "name": "Python: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {"PYTHONPATH": "${workspaceFolder}/src"}
        },
        {
            "name": "Python:Streamlit",
            "type": "debugpy",
            "request": "launch",
            "module": "streamlit",
            "args": [
                "run",
                "${file}",
                "--server.port",
                "8501",
                "--server.fileWatcherType",
                "poll",
                "--server.address",
                "0.0.0.0"
            ],
            "cwd": "${workspaceFolder}/src",
            "env": {
                "PYTHONPATH": "${workspaceFolder}/src",
                "PYTHONHOME": "/usr/local/bin"
            }
        }
    ]
}

I have tried a lot of configurations, but I failed to find one that works.

EDIT I have pylance and streamlit workinng together in another computer with a VSC Dev Container over Windows, and they work perfectly.

This is what I get with python -m site:

sys.path = [
    '/workspaces/sacbeh',
    '/workspaces/sacbeh/src',
    '/usr/local/lib/python310.zip',
    '/usr/local/lib/python3.10',
    '/usr/local/lib/python3.10/lib-dynload',
    '/usr/local/lib/python3.10/site-packages',
]
USER_BASE: '/root/.local' (doesn't exist)
USER_SITE: '/root/.local/lib/python3.10/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
Share Improve this question edited Nov 26, 2024 at 6:54 fgu 3081 silver badge13 bronze badges asked Nov 16, 2024 at 18:41 HuLu ViCaHuLu ViCa 5,46612 gold badges52 silver badges114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Yeah, it's unclear what's going on there. I can easily reproduce such behavior with python 3.13.0 under macOS.

$ cat st.py && python st.py && echo && pyright st.py
import streamlit
print('Hello, world!')

Hello, world!

st.py:1:8 - error: Import "streamlit" could not be resolved (reportMissingImports)
1 error, 0 warnings, 0 informations 

solution

I recommend you move on to another linter when working with streamlit:

$ mypy st.py 
Success: no issues found in 1 source file

It also accepts switches like --ignore-missing-imports if you continue to run into trouble. I usually find it most helpful when executed using --strict.


But, does this solve the debugging error, or just the linting

There's two questions posed. I answered the first one, by suggesting the use of a non-broken linter.

In a subsequent edit you explained

I reset the Docker image, and the exception changed to a simpler one:

/usr/bin/python3: No module named streamlit

Well, that seems pretty straightforward to fix. I don't know what the verb "reset" means here, but the symptom shows that "broke" is an appropriate verb. Your sys.path is not pointing at a venv with streamlit installed. (Or, IDK maybe you're shooting for system level library installs, and streamlit isn't installed. Yeah, RUN pip install ... -r requirements.txt looks like it uses a system-wide folder such as /usr/local. It's not obvious to me how the interpreter in /usr/bin is going to find /usr/local folders. I usually rely on python -m venv and then source activate, but you have gone down a different config path in your container.)

It's easy enough to fix up "missing deps". Start with $ which python to verify that the interpreter you think is running truely is the one that does run. Then $ python -m site offers a convenient way to see which directories import consults. Adjust the PYTHONPATH env var if your favorite folder is missing.

Sometimes $ which pip shows it is coming from a different place. Installing deps with $ python -m pip install streamlit foo bar baz is more robust, as the install is guaranteed to use the same interpreter and same venv as the execution environment.

Running

$ find /workspaces/sacbeh /usr/local/lib/python3.10/site-packages |
    grep streamlit

should reveal where pip installed the library. If it is absent, then streamlit imports won't succeed. (BTW, USER_BASE and USER_SITE not existing is definitely not a problem, don't worry about that.)


If code runs fine from ENTRYPOINT or a bash prompt, but not from an IDE, one can always use the stopgap of inserting a breakpoint() call in the code, until the IDE can be coaxed into working again. An if statement can even conditionally enter the standard debugger.

本文标签: pythonPylance failing to resolve import of libraries in a devcontainer in LinuxStack Overflow