Identify the platform running Python (Kaggle / Colab / Windows)

Purpose

In competitions, etc., the file reading process and import module will change between local and cloud, so it will be possible to switch automatically. Kaggle, Google Colaboratory, Local (Windows 10)

Execution environment (as of March 15, 2020)

Overview / Method

Discrimination method,

  1. Local (Windows) and cloud judgment are judged from the OS with os.name
  2. Kaggle / Colab is judged by the presence or absence of Kaggle's unique environment variable'KAGGLE_DATA_PROXY_TOKEN'

Please let me know if there is a smarter judgment method.

Processing content of each platform

Discrimination

import os

# windows
if os.name == 'nt':
    path = '../input/data/'

    import japanize_matplotlib
    sns.set(font="IPAexGothic")

elif os.name == 'posix':
# Kaggle
    if 'KAGGLE_DATA_PROXY_TOKEN' in os.environ.keys():
        path = '/kaggle/input/'

# Google Colab
    else:
        from google.colab import drive
        drive.mount('/content/drive')
        !ls drive/My\ Drive/'Colab Notebooks'/xxx #xxx rewrite
        path = "./drive/My Drive/Colab Notebooks/xxx/input/data/" #xxx rewrite
        #Check the remaining time of the session
        !cat /proc/uptime | awk '{print $1 /60 /60 /24 "days (" $1 / 60 / 60 "h)"}'

print(os.name)
print(path)

Recommended Posts

Identify the platform running Python (Kaggle / Colab / Windows)
[Beginner] Installing Python and running programs (Windows)
The basics of running NoxPlayer in Python
Until you use the Kaggle API with Colab
Trap trapped when running a Python Windows executable
Python on Windows
python windows environment
Python installation (Windows)
Try running the toio core cube on Windows 10 / macOS / Linux with the Python library bleak
Touching the latest physics-based renderer Mitsuba2 (2) Running from Python
I tried running the app on the IoT platform "Rimotte"
The process of installing Atom and getting Python running
From python to running instance on google cloud platform