Instructions for Installing Python and OpenCV

0. Outline

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 a popular Python data science platform, which includes many useful pre-compiled third-package packages. Thus, in Windows platform, you can simply install Python through Anaconda.

Download and install Anaconda Python 3.7 from official website. Plase check “adding to system path” while installing. (Anaconda now also comes with Python 3.8, you can also choose to install it.)

After installation, you can test the installation of Anaconda: Open command line PowerShell. If the only Python in your operating system is linked with this Anaconda, when you type python, you shall see

PS C:\Users\User> python
Python 3.7.x (default, Date and Time) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32           
Type "help", "copyright", "credits" or "license" for more information.
>>>                                                                         

Then type exit() and enter to exit.

Then, type pip -V. If the screen shows:

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

Then your Anaconda environment is successfully installed.

If your system already had other versions of Python installed, try python3 / py3 and pip3 instead.
And later in your IDE (e.g. PyCharm), make sure that the Python of the Anaconda version is selected.

1.2 In Linux (e.g., Ubuntu)

Python is usually pre-installed. For example, in 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 IDE (PyCharm)

Install a Python Programming IDE to make your coding and debugging easier. Here we show the installation of a popular Python IDE: PyCharm.

Download and install the installation from its official website.
Next, you need to select your python interepreter in the setting.

Open PyCharm, create a new project, click the following items

File -> Settings -> Project -> Project Interpreter -> [wrench icon in top right] ->
Add -> System Interpreter -> Select Anaconda.

Then, you can right click a python file *.py from the Project explorer on the left side to run it.

3. Install third-party package (OpenCV)

OpenCV is the most popular open source computer vision libraries. We can 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 (i.e., with Anaconda). If not, you may need to use pip3 instead.

In command line, type pip install opencv-python.

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

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

You will see an empty window shown then disappeared. Test this in your IDE too.