Ask coding questions

← Back to all posts
Can a Python script detect it is running in
boukeas (2)

Is it possible for a Python script to check if it is running within

Answered by Vandesm14 (2753) [earned 5 cycles]
View Answer
Vandesm14 (2753)

I'm afraid that this is not possible. is running a Linux VM which does not provide any info on if it's a normal Linux machine, or a server.

boukeas (2)

@Vandesm14 Thank you for your reply. Would it not be possible to set an environment variable in the VM, that would suggest that this is a server?

I asked this question in the context of a specific issue I am having: Essentially, this is an example of a program that would need to differentiate its behaviour depending on whether or not it is running in

Vandesm14 (2753)

@boukeas You can set .env variables manually and pull from that, but it's far from automatic.

MrEconomical (2304)

@boukeas if @Vandesm14 answered your question make sure to mark t as correct

boukeas (2)

@Vandesm14 What I am saying is it would be possible for a program to detect if it's running in if a specific environment variable was automatically set by in the Linux VM.

qaiik (1)

@Vandesm14 it is, check if REPL_ID env exists

coreyseliger (0)

It seems that sets some environment variables:

import os
[k for k in os.environ.keys() if 'REPL_' in k]



I also notice that they embed a DB URL environment variable as well:

import os
[k for k in os.environ.keys() if 'REPLIT_' in k]



The documentation seems to reference some of these variables directly and encourages their use. See here:

Andy_4sberg (54)

detect if directory /home/runner exists, it isn't likely for it to exist on some other device, but it does on For it to exist on another device would mean that device is linux/unix based with the username of a user as "runner". Example:

import os
if os.path.isdir("/home/runner") == True:
    # Some code here
    # Some code here