#enviroment-setup #python #pip #pdm #vscode #pyenv
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.
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:
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
curl https://pyenv.run | bash
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
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 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
- Go to the VS Code website.
- Click on the "Download for Windows" button.
- Run the downloaded installer.
- Follow the installation instructions.
To install the Python plugin in VS Code, follow these steps:
- Open VS Code.
- Click on the "Extensions" icon on the left-hand side of the screen.
- Search for "Python" in the search bar.
- Click on the "Install" button next to the "Python" extension.
To select the Python interpreter in WSL, follow these steps:
- Install the Remote WSL extension in VS Code.
- Open a WSL window in VS Code.
- Check if the Python interpreter is automatically detected. If not, run
which python
in the WSL terminal to get the path. - Use
\\wsl.localhost\Ubuntu-22.04\home\username\your\project
to navigate in Windows Explorer - 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 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()
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:
- If you don't have it already, consider installing Windows Terminal from the Microsoft Store. It provides a modern and feature-rich terminal experience.
- Download and install an X Server for Windows. A popular choice is VcXsrv. Follow the installation instructions provided by the X Server you choose.
-
Launch the installed X Server.
-
During the setup, choose the options to start the server with multiple windows and to disable access control.
-
Open your WSL terminal.
-
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
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.
- https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps Run in the admin powershell:
wsl --update
wsl --shutdown
sudo apt update
sudo apt install x11-apps -y
xcalc
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.
Install Git using your package manager. On Debian-based systems (e.g., Ubuntu):
sudo apt-get update
sudo apt-get install git
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
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.
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
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
- 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
API_URL=<>
API_USERNAME=example@email.com
API_PASSWORD=<>
export $(cat .env/<envname>.env | xargs)
cd /path/to/project/root
pdm run python -m pytest -k test_name