Skip to content

Instantly share code, notes, and snippets.

@KirillY
Last active June 2, 2024 20:27
Show Gist options
  • Save KirillY/6a39310b1fea1a8cc7d0d81632426c99 to your computer and use it in GitHub Desktop.
Save KirillY/6a39310b1fea1a8cc7d0d81632426c99 to your computer and use it in GitHub Desktop.
#enviroment-setup #python #pip #pdm #vscode #pyenv

#enviroment-setup #python #pip #pdm #vscode #pyenv

Local Machine Installation & Configuration for a Python Testing Automation project

It's recommended to use Ubuntu Linux distribution, either WSL or VM or Desktop Linux. Some instructions are specific to WSL, but most are applied to any Debian Linux distribution.

Setting Up Windows Subsystem for Linux (WSL) on Windows 10 and Windows 11

Windows Subsystem for Linux (WSL) allows you to run a Linux distribution alongside your Windows installation. Follow these steps to set up WSL on both Windows 10 and Windows 11:

Follow the latest instructions here

Pip

pip is still required to download and configure pdm.

Verify pip version has been installed:

pip --version

If an older version is installed, upgrade by running:

pip install --upgrade pip

Or install from the scratch

sudo apt update
sudo apt install python3-pip

Pyenv

Auto installation

curl https://pyenv.run | bash

Update packages before install any python version

sudo apt install -y make build-essential \
                         libssl-dev \
                         zlib1g-dev \
                         libbz2-dev \
                         libreadline-dev \
                         libsqlite3-dev \
                         wget \
                         curl \
                         llvm \
                         libncurses5-dev \
                         libncursesw5-dev \
                         xz-utils \
                         tk-dev \
                         libffi-dev \
                         liblzma-dev \
                         python-openssl \
                         git
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

Ensure to restart your terminal after performing the above. To check this has been installed correctly, simply run:

pyenv --help

Please ensure to install the pyenv-update plugin so that it can be updated from the command line. To do this:

git clone https://github.com/pyenv/pyenv-update.git $(pyenv root)/plugins/pyenv-update

This will allow pyenv to be updated by running the following. It is important to do this as pyenv caches the versions of Python available and so major and/or minor patches may not appear available to use without doing this.

pyenv update

Install the latest stable version of Python via pyenv:

pyenv install -l 
# 3.10.12
pyenv install 3.10.12

This will take a few minutes as it builds Python from source. To then use this version of Python going forward:

pyenv global 3.10.12

To use specific version inside the project

cd /your/project
pyenv local 3.10.12

To confirm the correct python version is being used, run the following command:

python -V
# Python 3.10.12

PDM installation

PDM is a modern Python package manager and environment manager that aims to provide a better project management experience. It combines the features of package management and virtual environments into a single tool. Here's how you can get started with PDM:

curl -sSL https://pdm.fming.dev/install-pdm.py | python -
# add suggested line to ~/.bashrc

PDM initialize pyproject.toml file

cd /your/project
pdm init
# yes to create virtualenv, others default

PDM add package, execute command with pdm run

pdm add requests
pdm run playwright install

Installing VS Code on Windows

  1. Go to the VS Code website.
  2. Click on the "Download for Windows" button.
  3. Run the downloaded installer.
  4. Follow the installation instructions.

VS Code: install Python Plugin

To install the Python plugin in VS Code, follow these steps:

  1. Open VS Code.
  2. Click on the "Extensions" icon on the left-hand side of the screen.
  3. Search for "Python" in the search bar.
  4. Click on the "Install" button next to the "Python" extension.

VS Code: selecting the Python Interpreter in WSL

To select the Python interpreter in WSL, follow these steps:

  1. Install the Remote WSL extension in VS Code.
  2. Open a WSL window in VS Code.
  3. Check if the Python interpreter is automatically detected. If not, run which python in the WSL terminal to get the path.
  4. Use \\wsl.localhost\Ubuntu-22.04\home\username\your\project to navigate in Windows Explorer
  5. Select the Python interpreter by clicking on the Python version in the bottom left corner of the VS Code window.

By following these steps, you can install VS Code on Windows, install the Python plugin, and select the Python interpreter in WSL. This will allow you to work with Python in VS Code and take advantage of its many features for Python development.

Playwright

Playwright is a powerful automation library that allows you to automate browser tasks and perform end-to-end testing. Here's how you can install Playwright for Python on Windows Subsystem for Linux (WSL):

Install required dependencies:

sudo apt update
sudo apt install -y libnss3-dev libx11-xcb-dev libxcb-dri3-0 libdrm2 libgbm1

Install Playwright

cd /your/project
pdm run add playwright

Install Browser Binaries

pdm run playwright install

Verify installation

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://example.com")
    print(page.title())
    browser.close()

Using GUI Applications in WSL with Windows 10

Windows Subsystem for Linux (WSL) now supports running Linux graphical applications with the help of a third-party tool called X Server. Here's how you can set up and use GUI applications in the latest version of WSL:

Step 1: Install Windows Terminal (Optional)

  1. If you don't have it already, consider installing Windows Terminal from the Microsoft Store. It provides a modern and feature-rich terminal experience.

Step 2: Install an X Server

  1. Download and install an X Server for Windows. A popular choice is VcXsrv. Follow the installation instructions provided by the X Server you choose.

Step 3: Configure X Server

  1. Launch the installed X Server.

  2. During the setup, choose the options to start the server with multiple windows and to disable access control.

Step 4: Configure WSL for GUI

  1. Open your WSL terminal.

  2. Add the following environment variables to your shell profile file (e.g., ~/.bashrc):

    export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
    export LIBGL_ALWAYS_INDIRECT=1

Using GUI Applications in WSL with Windows 11

With the latest major release, WSL 2, Windows 11 supports running not just command-line tools but also GUI applications (X11 and Wayland). The Windows operating system renders GUI applications running in the WSL virtual machine using the native mstsc.exe RDP client, which communicates with FreeRDP in WSLg.

Installation:

wsl --update
wsl --shutdown

Verify

sudo apt update
sudo apt install x11-apps -y
xcalc

Git Installation and Basic Usage Guide for Linux

Git is a powerful version control system that allows you to track changes in your codebase. Follow these steps to install Git and get started with basic usage on a Linux system.

Step 1: Install Git

Install Git using your package manager. On Debian-based systems (e.g., Ubuntu):

sudo apt-get update
sudo apt-get install git

Step 2: Configure Git

git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"

Step 3: Generate SSH Key for GitHub

Check for existing SSH keys:

ls ~/.ssh

Generate a new SSH key if needed:

ssh-keygen -t ed25519 -C "youremail@example.com"
# all default

Copy the public SSH key to your clipboard:

cat ~/.ssh/id_ed25519.pub

Add the copied SSH key to your GitHub account:

  • Go to GitHub > Settings > SSH and GPG keys.
  • Click on "New SSH key" and paste the copied key.

Step 4: Basic Git Usage

Create a new Git repository:

cd /your/project
git init

Set origin

git remote add origin <repository_ssh_url>

Commit your changes

git add <file_name>
git commit -m "Your commit message"

Set remote branch (needed only once)

git push -u origin <branch_name>

Sync with remote after remote branch is set

git pull
git push

Pre-commit checks

The pre-commit module is used to ensure the code in this repo adheres to a set of coding standards on each and every commit.

Install hooks

pdm add pre-commit

Update hooks

pdm run pre-commit autoupdate

Create pre-commmit-config.yaml

repos:
  - repo: https://github.com/charliermarsh/ruff-pre-commit
    rev: v0.0.289
    hooks:
      - id: ruff
        args: [--fix]

  - repo: https://github.com/psf/black
    rev: 23.9.1
    hooks:
      - id: black

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.5.1
    hooks:
      - id: mypy
        args: [--install-types, --non-interactive, --no-strict-optional, --ignore-missing-imports]

  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks:
      - id: trailing-whitespace

Setup the pre-commit git hooks (this only needs doing once):

pdm run pre-commit install --install-hooks

Create .mypy_cache cache folder

cd project_root/
mkdir .mypy_cache

Before committing code, it is recommended that you run the following command to see if any changes have caused the static code analysis tools to highlight an issue:

pdm run pre-commit run --all-files

Readme.md file best practices

  • use backticks for code inline and code block formatting
  • user should be able to setup from zero by only reading the Install section
  • if any environment variables need to be added, include them into readme
  • user should be able to run any application/tests by only reading Run section
  • composing good and reliable readme is super important for the application success

Suggested Readme.md sections

### Environment setup, git workflow

Local Machine Installation & Configuration for a Python Testing Automation project

- https://gist.github.com/KirillY/6a39310b1fea1a8cc7d0d81632426c99

### Install packages and venv

```shell
cd /path/to/project/root
pdm install

Create <envname>.env file

API_URL=<>
API_USERNAME=example@email.com
API_PASSWORD=<>

Export environment variables

export $(cat .env/<envname>.env | xargs)

Run tests

cd /path/to/project/root
pdm run python -m pytest -k test_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment