Skip to content
Sign upLog in
← Back to Community

Can a Python script detect it is running in repl.it?

Profile icon
boukeas

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

Answered by Vandesm14 [earned 5 cycles]
View Answer
Voters
Profile icon
boukeas
Comments
hotnewtop
Profile icon
Vandesm14

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

Profile icon
boukeas

@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 repl.it server?

I asked this question in the context of a specific issue I am having: https://repl.it/talk/ask/Is-it-possible-to-use-PIL-in-replit-to-show-images/24944. Essentially, this is an example of a program that would need to differentiate its behaviour depending on whether or not it is running in repl.it

Profile icon
Vandesm14

@boukeas
You can set .env variables manually and pull from that, but it's far from automatic.
https://pypi.org/project/python-dotenv/

Profile icon
eco27

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

Profile icon
boukeas

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

Profile icon
qaiik

@Vandesm14
it is, check if REPL_ID env exists

Profile icon
[deleted]
from os import environ if "REPL_OWNER" in environ: print(f"Hello, {environ['REPL_OWNER']}.") else: print("Not using replit.")
Profile icon
coreyseliger

It seems that repl.it sets some environment variables:

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

...returns:

['REPL_SLUG', 'REPL_IMAGE', 'REPL_ID', 'REPL_OWNER', 'REPL_LANGUAGE', 'REPL_PUBKEYS']

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]

...returns:

['REPLIT_DB_URL']

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

https://docs.replit.com/misc/repl-environment-variables

Profile icon
Andy_4sberg

detect if directory /home/runner exists, it isn't likely for it to exist on some other device, but it does on repl.it. 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 else: # Some code here