Installing Python libraries in the Visual Studio Code container

Viewed 2111

I can edit python code in a folder located in a Docker Volume. I use Visual Studio Code and in general lines it works fine.

The only problem that I have is that the libraries (such as pandas and numpy) are not installed in the container that Visual Studio creates to mount the volume, so I get warning errors.

How to install these libraries in Visual Studio Code container?

** UPDATE **

This is my application Dockerfile, see that the libraries are included in the image, not the volume:

FROM daskdev/dask

RUN /opt/conda/bin/conda create -p /pyenv -y
RUN /opt/conda/bin/conda install -p /pyenv scikit-learn flask waitress gunicorn \
    pytest apscheduler matplotlib pyodbc -y
RUN /opt/conda/bin/conda install -p /pyenv -c conda-forge dask-ml pyarrow -y
RUN /opt/conda/bin/conda install -p /pyenv pip -y
RUN /pyenv/bin/pip install pydrill 

And the application is started with docker compose:

version: '3'

services:   

  web:
    image: img-python
    container_name: cont_flask
    volumes:
      - vol_py_code:/code
    ports:
      - "5000:5000"
    working_dir: /code
    entrypoint:
      - /pyenv/bin/gunicorn
    command:
      - -b 0.0.0.0:5000
      - --reload
      - app.frontend.app:app
3 Answers

https://code.visualstudio.com/docs/python/python-tutorial
In the VScode documentation, there is a section called "Install and use packages" under the python tutorial. I think that will explain it to you.
Edit:
Have you added it to your dockerfile? Like:
RUN pip install pandas
Try to look at the answer to this question and see if it helps:
How to add Python libraries to Docker image
Edit2:
It seems like VSC are using the local environment, when it creates a container, and there isn't a devcontainer.json file.
I think you need to either install the libraries in your local environment or set up a development container in VSC:
https://code.visualstudio.com/docs/remote/create-dev-container
Microsoft have a GitHub repo with development container templates:
https://github.com/microsoft/vscode-dev-containers
If you already have set up a development container, can you please add your devcontainer.json file to your question, so it is possible to look at it?

The warnings originate from using a different python interpreter for your Docker volume and Visual Studio Code.

This can be solved by directing the python interpreter to the one in your Docker volume. Clicking one the python interpreter in the bottom left of VS Code ("Python 3.8.0" in the image below) gives you the option to change the path. Enter the path to the Python in your docker volume.

enter image description here

Edit: the path could be something like /pyenv/bin/python.

Related