Those of you who use Windows know how painful it is to develop Python on Windows. Install all kinds of errors with PIP, and then spend a lot of time to find a solution, every time the mood is like this

My previous solution was to install VMWare on Windows, run Ubuntu on VMWare, and do development in Ubuntu. However, this is not very convenient. Every time you enter the development environment, you have to start VMWare first, then Ubuntu, and then open the command line window to start development. And sometimes you need to switch back and forth between the host machine and virtual machine, which is also very troublesome.

Recently I learned that Docker also has a Version of Windows, so I thought of using Docker to run an Ubuntu image on Windows and build a Python development environment in the container, which not only solves the problem of installation library, but also solves the inconvenience of VMWare virtual machine.

For the basic use of containers, you can refer to my previous article Docker first experience.

Note that Docker installation on Windows has the following requirements for the system:

  • You need a Version of Windows that supports Hyper-V, which is currently only supported after Windows 10
  • Virtualization needs to be enabled in BIOS.

If your system meets the above requirements, let’s set up the environment step by step.

Install Docker for Windows

The installer can download Docker for Windows from here

Once installed, run Docker for Windows.

After Docker runs, you can see a small whale icon in the status bar, as shown below

Open a command line window and run the following command to view the Docker version

docker --version

Copy the code

The output

Docker version 18.09.0, build 4d60db4

Copy the code

Download mirror

Docker Hub provides many Docker images. We build our Python development environment based on Ubuntu :18.04. run

Docker pull ubuntu: 18.04Copy the code

After downloading, let’s take a look at the local image and run it

docker images

Copy the code

You can see the following output

REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
ubuntu                      18.04               93fd78260bd1        6 days ago          86.2MB

Copy the code

Install common tools and Python

We start a container, go to bash inside the container, and run it

Docker Run -it Ubuntu :18.04 bashCopy the code

Our image is now just a basic Ubuntu system, without many tools like Ping, ifconfig, wget, vim, etc., and without Python. And then we’re going to put them all in one by one.

Since the default image uses the official ubuntu source, it is very slow to connect to the official source from home, so let’s change the source to 163 first. Change the contents of /etc/apt/sources.list to the following

deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse

Copy the code

run

apt update

Copy the code

Next, install ping, wget, ifconfig, vim, etc

apt install iputils-ping wget net-tools vim

Copy the code

Install python3.6

Apt install python3.6 ln-sThe/usr/bin/python3.6 / usr/bin/pythonCopy the code

Install the PIP

apt install python3-pip

Copy the code

Configuration of VIM

After installing the tools, let’s configure vim. Open the ~/. Vimrc file and enter the vim configuration. Let’s say my configuration looks like this

set ru
syntax on
set background=dark
set sw=4
set ts=4
set tabstop=4
set shiftwidth=4
set expandtab
filetype plugin on
set autoindent
set smartindent
set number
set viminfo='10,\"100,:20,%,n~/.viminfo
function! ResCur()
    if line("'\"") <= line("$")
        normal! g`"
        return 1
    endif
endfunction

augroup resCur
    autocmd!
    autocmd BufWinEnter * call ResCur()
augroup END

highlight WhiteSpaces ctermbg=green guibg=#55aa55
match WhiteSpaces /\s\+$/

Copy the code

Ok, so now our development environment is configured.

Submit mirror

For future use, the configured container is made into a new image. By executing exit in the container, we are now in the Windows command line window. Let’s take a look at the container we just configured and run

docker ps -a

Copy the code

The output

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 39CA895C725e Ubuntu :18.04 "bash" 3 hours ago Exited (0) 19 seconds  ago relaxed_wilesCopy the code

Run the following command to commit the changes and package the container into a new image

docker commit 39ca895c725e shenzhongqiang/python-dev:version1

Copy the code

Let’s look at the local mirror again

docker images

Copy the code

The output

REPOSITORY TAG IMAGE ID CREATED SIZE Shenzhongqiang /python-dev version1 fbf0ce58d00d 2 minutes ago 542MB Ubuntu 18.04 REPOSITORY TAG IMAGE ID CREATED SIZE Shenzhongqiang /python-dev version1 fbf0ce58d00d 2 minutes ago 542MB Ubuntu 18.04 93FD78260bd1 6 days ago 86.2MBCopy the code

As you can see, we now have two local mirrors. Below is the original Ubuntu image on docker Hub, and above is our own custom image.

Then we can start the container development based on our custom image. Starting the container is as simple as running it

docker run -it fbf0ce58d00d bash

Copy the code

Note that if the container is deleted, the changes in it will also be lost. To save the changes in the container, commit the changes to the image as above.

Ok, so that’s all the customization steps. Over the last couple of days I’ve been moving projects to containers, and developing in containers feels much smoother than developing in virtual machines.

For convenience, the image above is on my Docker Hub and can be obtained by using the following command

docker pull shenzhongqiang/python-dev:version1

Copy the code

If you have any suggestions for a better Python environment for Windows, let me know in the comments

This article has been updated to the public account “Python and data analysis”, welcome to follow ~