Skip to content

Python⚓︎

Resources

Basics⚓︎

Package management⚓︎

About package management

  • py -m pip install --upgrade pip : it installs and upgrades pip
  • py -m pip install MODULE: it installs the required module

Creating and using virtual environments⚓︎

About virtual environments

  • py -m venv .VIRTUALENVIRONMENTNAME: it creates/uses the virtual environment specified in the path, then selectable in the terminal
  • py -m deactivate: it destroys the current virtual environment

Requirements⚓︎

  • py -m pip freeze > requirements.txt: it prints a file called requirements with all the package necessary to the current project (they can be retrieved by the command py -m pip install -r requirements.txt)

Inheritance⚓︎

About inheritance

Testing⚓︎

Using Selenium and ChromeDriver⚓︎

  • Download and install Chrome
# Setup key
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -

# Setup repository
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
sudo apt-get update 
sudo apt-get install google-chrome-stable
  • Install Selenium and webdriver_manager (this last one will take care of installing and updating the right version on the chromedriver, regardless of the local version of Google Chrome)

    pip install selenium
    pip install webdriver_manager
    

  • In your Python script, add

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.chrome.options import Options
    

  • To silence the webdriver, use

    # Shut webdriver manager logs up
    os.environ['WDM_LOG_LEVEL'] = '0'
    

  • To set up a browser session:

    chrome_options = Options()
    chrome_options.add_argument("--headless") # Means with no interface
    browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
    
    To close the session after the tests:
    browser.quit()