Python3 + OpenCV3 + VS Code Installation Tutorial


1 Install Python3

1.1 In Windows

Scientific programming in Python requires multiple third-party packages (like numpy), whose installing procedure needs complicated compiling tools. Those compiling tools are tricky in Windows. Anaconda is the most popular Python data science platform, which includes essential pre-compiled third-package packages. Thus, in Windows platform, we suggest installing Python by Anaconda.

Download and install Anaconda Python 3.7 from official website. Plase check adding to system path while installing.

In order to test the Anaconda. Open command line PowerShell. If the installed Anaconda is the only Python version in your operating system. Type python. In If it shows:

PS C:\Users\User> python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32           
Type "help", "copyright", "credits" or "license" for more information.

Then type exit() and enter. Type pip -V. If the screen shows:

PS C:\Users\User> pip -V
pip 18.1 from C:\Users\User\Anaconda3\lib\site-packages\pip (python 3.7) 

Congratulations! Your Anaconda environment is successfully installed in your Windows.

If you have another version of Python, try python3 / py3 and pip3 instead.

1.2 In Linux (like Ubuntu)

In general, Python is pre-installed in Ubuntu. For Ubuntu 18.04, in Terminal, you can type python3 to enter python console mode.

If your Ubuntu does not have Python3, please try:

$ sudo apt install python3

to install Python3.

We also need pip to maintain Python third-party packages. Use

$ sudo apt install python3-pip

to install pip3.

You can also install Anaconda in Linux if you want.

2 Install OpenCV3

OpenCV is the most popular open source computer vision libraries. We will use pip to install OpenCV for Python. Please type pip -V in command line to check the current pip is matched to your target Python version. You may use pip3 instead (In Ubuntu 18.04).

In command line, type pip install opencv-python.

To test OpenCV, in python consle mode, type the following codes in order.

>>> import cv2
>>> import numpy
>>> img = numpy.zeros([320, 320], numpy.uint8)
>>> cv2.imshow("test", img)
>>> cv2.destroyAllWindows()            

You will see an empty window shon then disappeared.

3 Install Visual Studio Code

Download and install Visual Studio Code from official website.

Now, you can write your code in VS Code and run the code in command line. You can also open command line in VS Code by Ctrl + ` .

If you want to set up a debug environment for Python, please follow this official tutorial. There are two tips:

  1. Check the Python intepretor in the left bottom of VS Code is correct or not.
  2. Add attribute "args": ["--arg1", "--arg2"] in launch.json file to set up program arguments.
© Yongqing Liang (root [at] lyq [dot] me).RSS