Python3 virtual environment

After using awhile and finally discovery some limitation of Docker can't do and not suitable.

If you need to develop something related with GUI applications and input devices automation thing Docker is totally not suitable.

For Example using the PyAutoGUI to capture a screenshoot and control the input devices using a local script to control some application on host machine.

Error when run PyAutoGUI on Docker

Traceback (most recent call last):  
  File "app/control.py", line 3, in <module>
    import pyautogui
  File "/usr/local/lib/python3.8/site-packages/pyautogui/__init__.py", line 241, in <module>
    import mouseinfo
  File "/usr/local/lib/python3.8/site-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
  File "/usr/local/lib/python3.8/os.py", line 675, in __getitem__
    raise KeyError(key) from None
KeyError: 'DISPLAY'  

Is look like the Docker can't get the correct Display for the GUI part and I can't find any solution solve this issue.

Maybe we can using the Ubuntu docker images but even if working also is very difficult for development since their are no GUI for fast and accurate to find the position and images information.

I have try using VM (VirtualBox).

Yes is working without any error the script is able get the screenshoot and control the input devices but actually is quite slow performance is difficult keep coding and try and error.

I running the Ubuntu20.04 VM on iMac (Retina 5K, 27-inch, 2019) with 16 GB 2667 MHz DDR4 Memory and Radeon Pro 570X 4 GB Graphics machine.

No idea why running Ubuntu20.04 VM on macOS Catalina is really slow, compare with running VM on my Windows 10 laptop.

More faster and better solution is look like using Python3 virtual environment for our request.

Our Principal is we need a faster, easy, safe, allow remove module files after finish and not cause any issue with current system environment .

1. Create a Project Folder and build the virtual environment

mkdir /project-folder

cd /project-folder

2. Create virtual environment

python3 -m venv ./venv

Installing a new virtual environment and define the env folder for the virtual environment dependent files

2. activate virtual environment

source ./venv/bin/activate

After activate we should see the terminal will change and start with (venv)

3. Check the binary on the venv

(venv) $ which pip

(venv) $ which python3

we can found all the pip and python3 is using inside the virtual environment.

4. Install Python Module

(venv) $ pip install numpy

Is quite easy and same with we install module on main machine.

Now we have a Python3 virtual environment to development and test our script.

5. Install Python Module with requirements.txt

(venv) $ pip freeze > requirements.txt

we can use pip freeze to find what module is install on current virtual environment this is very useful for source control and we send our project to other.

(venv) $ pip install -r requirements.txt

install the Module dependencies with requirements.txt

6. Deactivate after finish work

(venv) $ deactivate