Forum Archive

Telling If I'm Pythonista Or Editorial Or Sublime Text Or Whatever

MartinPacker

Something I'm going to prototype might well run in Pythonista, or Editorial, or Sublime Text.

If Editorial it probably wants the current document. Likewise Sublime Text (but the code would be different). With Pythonista I read the file. And so on.

The point is I'd like to know which environment I'm running in so my code can branch and do the right thing.

I expect 95% of the code to be common across all the environments.

So how do I detect Pythonista vs Editorial etc? (Even just these two would be great.)

ccc

https://github.com/cclauss/Ten-lines-or-less/blob/master/omz_env.py

dgelessus

Instead of trying to find out what your environment is, go through all known methods of getting the current file and use the first one that works.

```python
try:
import editor
filename = editor.get_current_file()
except (ImportError, AttributeError):
try:
# Not Pythonista or Editorial
import whatever_sublime_text_uses
filename = whatever_sublime_text_uses.what_is_my_filename()
except (ImportError, AttributeError):
print("Could not determine currently open file!")
````