├── .devcontainer ├── Dockerfile ├── devcontainer.json ├── requirements.txt └── set_python_env.sh ├── 01_basic_example └── .devcontainer │ └── devcontainer.json ├── 02_build_example ├── .devcontainer │ ├── Dockerfile │ ├── devcontainer.json │ ├── requirements.txt │ └── set_python_env.sh └── test1.py ├── 03_final_example ├── .devcontainer │ ├── Dockerfile │ ├── devcontainer.json │ ├── requirements.txt │ └── set_python_env.sh ├── test1.py └── test2.ipynb ├── README.md ├── images └── vscode final.gif └── tests ├── test1.py └── test2.ipynb /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | ARG PYTHON_VER="3.10" 2 | FROM python:$PYTHON_VER 3 | 4 | ARG PYTHON_ENV=my_env 5 | ENV PYTHON_ENV=$PYTHON_ENV 6 | 7 | RUN mkdir requirements 8 | 9 | COPY requirements.txt set_python_env.sh /requirements/ 10 | 11 | RUN bash ./requirements/set_python_env.sh $PYTHON_ENV 12 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Python Development Environment", 3 | "build": { 4 | "dockerfile": "Dockerfile", 5 | "context": ".", 6 | "args": { 7 | "PYTHON_ENV": "my_python_dev" 8 | } 9 | }, 10 | "customizations": { 11 | "vscode": { 12 | "settings": { 13 | "python.defaultInterpreterPath": "/opt/my_python_dev/bin/python3", 14 | "python.selectInterpreter": "/opt/my_python_dev/bin/python3" 15 | }, 16 | "extensions": [ 17 | "ms-python.python", 18 | "ms-toolsai.jupyter" 19 | ] 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /.devcontainer/requirements.txt: -------------------------------------------------------------------------------- 1 | wheel==0.40.0 2 | pandas==2.0.3 -------------------------------------------------------------------------------- /.devcontainer/set_python_env.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PYTHON_ENV=$1 4 | 5 | python3 -m venv /opt/$PYTHON_ENV \ 6 | && export PATH=/opt/$PYTHON_ENV/bin:$PATH \ 7 | && echo "source /opt/$PYTHON_ENV/bin/activate" >> ~/.bashrc 8 | 9 | source /opt/$PYTHON_ENV/bin/activate 10 | 11 | pip3 install -r ./requirements/requirements.txt -------------------------------------------------------------------------------- /01_basic_example/.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Python Development Environment", 3 | "image": "python:3.10" 4 | } -------------------------------------------------------------------------------- /02_build_example/.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.10 2 | 3 | ARG PYTHON_ENV=my_env 4 | 5 | ENV PYTHON_ENV=$PYTHON_ENV 6 | 7 | RUN mkdir requirements 8 | 9 | COPY requirements.txt set_python_env.sh /requirements/ 10 | 11 | RUN bash ./requirements/set_python_env.sh $PYTHON_ENV -------------------------------------------------------------------------------- /02_build_example/.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Python Development Environment", 3 | "build": { 4 | "dockerfile": "Dockerfile", 5 | "context": ".", 6 | "args": { 7 | "PYTHON_ENV": "my_python_dev" 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /02_build_example/.devcontainer/requirements.txt: -------------------------------------------------------------------------------- 1 | wheel==0.40.0 2 | pandas==2.0.3 3 | ipykernel 4 | ipywidgets 5 | jupyter -------------------------------------------------------------------------------- /02_build_example/.devcontainer/set_python_env.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PYTHON_ENV=$1 4 | 5 | python3 -m venv /opt/$PYTHON_ENV \ 6 | && export PATH=/opt/$PYTHON_ENV/bin:$PATH \ 7 | && echo "source /opt/$PYTHON_ENV/bin/activate" >> ~/.bashrc 8 | 9 | source /opt/$PYTHON_ENV/bin/activate 10 | 11 | pip3 install -r ./requirements/requirements.txt -------------------------------------------------------------------------------- /02_build_example/test1.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | print("Hello World!") -------------------------------------------------------------------------------- /03_final_example/.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.10 2 | 3 | ARG PYTHON_ENV=my_env 4 | 5 | ENV PYTHON_ENV=$PYTHON_ENV 6 | 7 | RUN mkdir requirements 8 | 9 | COPY requirements.txt set_python_env.sh /requirements/ 10 | 11 | RUN bash ./requirements/set_python_env.sh $PYTHON_ENV -------------------------------------------------------------------------------- /03_final_example/.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Python Development Environment", 3 | "build": { 4 | "dockerfile": "Dockerfile", 5 | "context": ".", 6 | "args": { 7 | "PYTHON_ENV": "my_python_dev" 8 | } 9 | }, 10 | "customizations": { 11 | "vscode": { 12 | "settings": { 13 | "python.defaultInterpreterPath": "/opt/my_python_dev/bin/python3", 14 | "python.selectInterpreter": "/opt/my_python_dev/bin/python3" 15 | }, 16 | "extensions": [ 17 | "ms-python.python", 18 | "ms-toolsai.jupyter" 19 | ] 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /03_final_example/.devcontainer/requirements.txt: -------------------------------------------------------------------------------- 1 | wheel==0.40.0 2 | pandas==2.0.3 3 | ipykernel 4 | ipywidgets 5 | jupyter -------------------------------------------------------------------------------- /03_final_example/.devcontainer/set_python_env.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PYTHON_ENV=$1 4 | 5 | python3 -m venv /opt/$PYTHON_ENV \ 6 | && export PATH=/opt/$PYTHON_ENV/bin:$PATH \ 7 | && echo "source /opt/$PYTHON_ENV/bin/activate" >> ~/.bashrc 8 | 9 | source /opt/$PYTHON_ENV/bin/activate 10 | 11 | pip3 install -r ./requirements/requirements.txt -------------------------------------------------------------------------------- /03_final_example/test1.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | print("Hello World!") -------------------------------------------------------------------------------- /03_final_example/test2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Hello World!" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "This is example for running Python inside a container." 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "import pandas as pd" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Hello World!\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "print(\"Hello World!\")" 41 | ] 42 | } 43 | ], 44 | "metadata": { 45 | "kernelspec": { 46 | "display_name": "my_python_dev", 47 | "language": "python", 48 | "name": "python3" 49 | }, 50 | "language_info": { 51 | "codemirror_mode": { 52 | "name": "ipython", 53 | "version": 3 54 | }, 55 | "file_extension": ".py", 56 | "mimetype": "text/x-python", 57 | "name": "python", 58 | "nbconvert_exporter": "python", 59 | "pygments_lexer": "ipython3", 60 | "version": "3.10.14" 61 | } 62 | }, 63 | "nbformat": 4, 64 | "nbformat_minor": 2 65 | } 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Setting A Dockerized Python Environment - The Elegant Way 2 | 3 | This repo provides the code examples for this [medium article](https://medium.com/p/f716ef85571d). This includes the following three examples: 4 | - [Basic settings](https://github.com/RamiKrispin/vscode-python-medium/tree/main/01_basic_example) - a simple example of the `devcontainers.json` settings using the `image` argument to launch a dockerized session with the `python:3.10` image 5 | - [Build settings](https://github.com/RamiKrispin/vscode-python-medium/tree/main/02_build_example) - extending the first example by using the `build` argument to build the image from a Dockerfile 6 | - [Customization settings](https://github.com/RamiKrispin/vscode-python-medium/tree/main/03_final_example) - adding to the previous example the `customizations` argument to set the default Python interpreter and define the extension settings 7 | 8 |
9 | 10 |
11 | 12 | ## Resources 13 | 14 | Additional resources for setting a Python development environment with VScode and the Dev Containers extension: 15 | - Setting Python Development Environment with VScode and Docker - https://github.com/RamiKrispin/vscode-python 16 | - A Dockerized Python Development Environment Template - https://github.com/RamiKrispin/vscode-python-template 17 | - VScode - https://code.visualstudio.com/ 18 | - Dev Containers - https://code.visualstudio.com/docs/devcontainers/containers 19 | 20 | 21 | ## License 22 | 23 | This tutorial is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-nc-sa/4.0/) License. 24 | -------------------------------------------------------------------------------- /images/vscode final.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RamiKrispin/vscode-python-medium/a74839320e52ee997f52eed12c8fe663e0f5ae34/images/vscode final.gif -------------------------------------------------------------------------------- /tests/test1.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | print("Hello World!") -------------------------------------------------------------------------------- /tests/test2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Hello World!" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "This is example for running Python inside a container." 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "import pandas as pd" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Hello World!\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "print(\"Hello World!\")" 41 | ] 42 | } 43 | ], 44 | "metadata": { 45 | "kernelspec": { 46 | "display_name": "my_python_dev", 47 | "language": "python", 48 | "name": "python3" 49 | }, 50 | "language_info": { 51 | "codemirror_mode": { 52 | "name": "ipython", 53 | "version": 3 54 | }, 55 | "file_extension": ".py", 56 | "mimetype": "text/x-python", 57 | "name": "python", 58 | "nbconvert_exporter": "python", 59 | "pygments_lexer": "ipython3", 60 | "version": "3.10.14" 61 | } 62 | }, 63 | "nbformat": 4, 64 | "nbformat_minor": 2 65 | } 66 | --------------------------------------------------------------------------------