{"id":7738,"library":"speedtest-cli","title":"Speedtest CLI for Python","description":"speedtest-cli is a Python library that provides a command-line interface for testing internet bandwidth using speedtest.net. The current version is 2.1.3, and the library maintains an active release cadence with frequent bug fixes and minor improvements. It can also be used programmatically within Python applications.","status":"active","version":"2.1.3","language":"en","source_language":"en","source_url":"https://github.com/sivel/speedtest-cli","tags":["cli","network","speedtest","bandwidth","internet-speed"],"install":[{"cmd":"pip install speedtest-cli","lang":"bash","label":"Install via pip"}],"dependencies":[],"imports":[{"note":"The primary programmatic interface is the `speedtest` module and its `Speedtest` class. Direct import or execution of `speedtest_cli.py` was deprecated and removed in version 2.0.0.","wrong":"import speedtest_cli","symbol":"Speedtest","correct":"import speedtest\nst = speedtest.Speedtest()"}],"quickstart":{"code":"import speedtest\nimport os\n\ndef human_readable_bytes(nbytes):\n    suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']\n    i = 0\n    while nbytes >= 1024 and i < len(suffixes)-1:\n        nbytes /= 1024\n        i += 1\n    f = ('%.2f' % nbytes).rstrip('0').rstrip('.')\n    return '%s %s' % (f, suffixes[i])\n\nprint('Starting Speedtest...')\nst = speedtest.Speedtest()\n\n# Find best server based on ping\nprint('Finding best server...')\nst.get_best_server()\nprint(f'Hosted by {st.results.server['sponsor']} ({st.results.server['name']}) [Ping: {st.results.ping:.2f} ms]')\n\n# Test download speed\nprint('Testing download speed...')\ndownload_speed_bps = st.download()\ndownload_speed_mbps = download_speed_bps / 1_000_000 # Convert to Mbps\nprint(f'Download: {human_readable_bytes(download_speed_bps)} ({download_speed_mbps:.2f} Mbps)')\n\n# Test upload speed\nprint('Testing upload speed...')\nupload_speed_bps = st.upload()\nupload_speed_mbps = upload_speed_bps / 1_000_000 # Convert to Mbps\nprint(f'Upload: {human_readable_bytes(upload_speed_bps)} ({upload_speed_mbps:.2f} Mbps)')\n\n# Get full results (optional)\n# results = st.results.dict()\n# print(results)\n","lang":"python","description":"This quickstart demonstrates how to import the `speedtest` module, initialize the `Speedtest` object, find the best server, and then measure download and upload speeds programmatically. The speeds are converted to a human-readable format for clarity."},"warnings":[{"fix":"Update your code to use the `speedtest` module and its `Speedtest` class (e.g., `import speedtest; st = speedtest.Speedtest()`).","message":"The Python API was redesigned in version 2.0.0, and the `speedtest_cli.py` script was removed. Code directly importing or executing `speedtest_cli.py` will no longer work.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Understand that `speedtest-cli` is an open-source implementation and inherent inconsistencies are acknowledged by the developers. It's best used for relative measurements or when a programmatic solution is required, rather than for absolute official benchmarks.","message":"Inconsistent results may occur compared to the official Speedtest.net website or Ookla's native CLI. This is often due to differences in testing protocols (HTTP vs. raw sockets) and Python's performance characteristics on various systems.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Update `certifi` (`pip install --upgrade certifi`). Ensure your system's root certificates are up-to-date. If behind a corporate network, consult IT regarding SSL inspection. Using `--secure` with the CLI might help in some cases but is not a universal fix.","message":"SSL Certificate Verification Failures (`CERTIFICATE_VERIFY_FAILED`) can block tests. This is typically due to outdated system certificates, Python's certificate store issues, or network intermediaries like corporate proxies performing SSL inspection.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `http_proxy` and `https_proxy` environment variables are correctly set. Test thoroughly if proxying is critical for all operations. Version 2.1.2 included proxy support fixes.","message":"Proxy support can be inconsistent. While `speedtest-cli` generally respects `http_proxy` and `https_proxy` environment variables, some internal functionalities (like sharing results via `--share`) might not fully route through the configured proxy in all environments or versions.","severity":"gotcha","affected_versions":"<2.1.2"},{"fix":"Ensure you are running `speedtest-cli` version 2.1.2 or newer to prevent warnings on Python 3.8 and later.","message":"Some Python 3.8+ users might experience warnings or minor issues due to older compatibility patterns. Version 2.1.2 specifically addressed warnings on Python 3.8.","severity":"gotcha","affected_versions":"<2.1.2"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Verify your internet connection and firewall settings. Try running the test again later or at a different time. If persistent, check for broader network issues or server availability.","cause":"This usually indicates a temporary network connectivity issue, a firewall blocking outgoing connections, or occasionally, a server-side issue. Some users report this error more frequently when running automated tests at specific times (e.g., on the half-hour), possibly due to perceived rate-limiting or a bug in server selection.","error":"ERROR: Unable to connect to servers to test latency."},{"fix":"Rename your Python script to something other than `speedtest.py` (e.g., `my_speedtest.py`) to avoid shadowing the installed package.","cause":"You likely named your Python script `speedtest.py`. When your script tries to `import speedtest`, Python loads your script instead of the installed `speedtest-cli` library, causing a circular import and this error.","error":"AttributeError: partially initialized module 'speedtest' has no attribute 'Speedtest' (most likely due to a circular import). Did you mean: 'speedtest'?"},{"fix":"Ensure `certifi` is up-to-date (`pip install --upgrade certifi`). On macOS, run `/Applications/Python \\ 3.x/Install \\ Certificates.command`. Verify your system's certificate store is current. If behind a corporate proxy, check if it's interfering with SSL.","cause":"This error points to an issue with SSL certificate validation. It can be caused by outdated root certificates on your system, an issue with Python's access to the certificate store, or network proxies performing SSL interception.","error":"[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:XXXX)"},{"fix":"Add the directory where `pip` installs user scripts (e.g., `~/.local/bin` on Linux/macOS, or the `Scripts` subdirectory of your Python installation on Windows) to your system's `PATH`. Remember to restart your terminal or command prompt.","cause":"After installing `speedtest-cli` with `pip`, the executable script might be placed in a directory not included in your system's `PATH` environment variable. Common on Linux/macOS for `~/.local/bin` not being in PATH.","error":"command not found: speedtest"}]}