I want to create a script for just Terminal and IDLE, but I don't know how. Using if 'idlelib' in sys.modules:
works for seeing if it is running in IDLE, but is there a way to use the same code to find if it is in Terminal by replacing 'idlelib'
?
Asked
Active
Viewed 56 times
2

EnderCodesLol
- 79
- 1
- 1
- 10
-
What do you mean by "for just Terminal and IDLE"? What would be *not* Terminal or IDLE? – mkrieger1 Jan 10 '21 at 16:20
-
@mkrieger1 Solid point, but I wanted to make sure, because I plan on turning it into an executable for Terminal with arguments but still use inputs if it is in IDLE. – EnderCodesLol Jan 10 '21 at 16:21
-
Are you asking how you can determine whether a Python file is run as a script, or imported as a module? – mkrieger1 Jan 10 '21 at 16:25
-
@mkrieger1 No, I want to get the name of the process running the script and then check if it is what I want it to be. – EnderCodesLol Jan 10 '21 at 16:26
-
And what do you intend to do if is not what you want it to be? Or what would be the problem if you didn't do this check at all? – mkrieger1 Jan 10 '21 at 16:26
-
@mkrieger1 Return something that says to the user that they should use IDLE or Terminal. Because I don't have a windows device, I don't want people running it on Command Line in case of issues. – EnderCodesLol Jan 10 '21 at 16:28
1 Answers
2
You can try using psutil
and os
import psutil
import os
if psutil.Process(os.getpid()).parent().name() in ["cmd.exe","bash"]:
print("in cmd")
Using idle it returned 'pythonw.exe'
which shows this works.

Axisnix
- 2,822
- 5
- 19
- 41
-
What would I change "cmd.exe" to because I'm using Terminal, not windows Command Line? – EnderCodesLol Jan 10 '21 at 16:24
-
Running on Centos you would get `bash` as a result but you can also try printing using `print(psutil.Process(os.getpid()).parent().name())` to get what the terminal is in your case – Axisnix Jan 10 '21 at 16:28
-
ModuleNotFoundError: No module named 'psutil' Is it something I have to download first? – EnderCodesLol Jan 10 '21 at 16:31
-
-
@ncmathsadist I'm trying to download it using pip3 now, I guess it just has to be downloaded. – EnderCodesLol Jan 10 '21 at 16:34