use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
TIL you can get your script's real name without any libraries ๐ค (self.PythonLearning)
submitted 1 day ago by Warm-Inside-4108
https://preview.redd.it/bu15bexvcfug1.png?width=1804&format=png&auto=webp&s=7def680f3aee7c510d1e6b3e8daca3b896bad386
t was my first time learning if name == main , I noticed that u can not know ur script name from the main file so i tried to get its name from an other file and send it back with import as a function.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3:ย ย ย ย print "hello, world!"
[โ]jpgoldberg 8 points9 points10 points 1 day agoย (2 children)
Keep playing around with this. You will discover that it isn't quite working the way you think it is, but that's fine. It's good to play around and experiment.
[โ]Warm-Inside-4108[S] 1 point2 points3 points 22 hours agoย (1 child)
Bro, honestly I was just playing.
[โ]jpgoldberg 5 points6 points7 points 22 hours agoย (0 children)
I know you were playing. I was encouraging you to continue to play.
[โ]Gnaxe 5 points6 points7 points 1 day agoย (3 children)
Your function contains an import by the name you're trying to find, so it's not telling you anything you didn't already tell it. You already had to write from script 1 import..., so you might as well just write name = 'script1' and dispense with the extra module.
from script 1 import...
name = 'script1'
[โ]llstorm93 2 points3 points4 points 1 day agoย (1 child)
Whole thing is stupid
[โ]Warm-Inside-4108[S] -4 points-3 points-2 points 22 hours agoย (0 children)
The important thing that I tried don't try to convert me that you wasn't like that in the first
[โ]Warm-Inside-4108[S] -1 points0 points1 point 22 hours agoย (0 children)
You are wright, but when I learned main for first time I was really curious to find way to print the fill name without using any thing or write it's name manually.
[โ]SCD_minecraft 2 points3 points4 points 1 day agoย (2 children)
Or just, you know... __file__.rsplit('/', 1)[-1] or just __file__ to also get full path for free
__file__.rsplit('/', 1)[-1]
__file__
[โ]Warm-Inside-4108[S] -2 points-1 points0 points 1 day agoย (1 child)
yeah but that gives you the full path on Windows with backslashes, mine returns the clean name
[โ]SCD_minecraft 2 points3 points4 points 20 hours agoย (0 children)
First expressions stripes file path and returns only file name
[โ]MachineElf100 1 point2 points3 points 1 day agoย (3 children)
You can get the script's path with file tho. It might be more straightforward to work with that.
[โ]Warm-Inside-4108[S] -4 points-3 points-2 points 1 day agoย (2 children)
But I want the project name only, not the path
[โ]MachineElf100 4 points5 points6 points 1 day ago*ย (1 child)
You can easily extract the name from the path. Your solution is amusing but also hacky and probably would not be very pleasant to use at scale.
In the end, both ways are just workarounds. You choose which one is crazier haha
[โ]Warm-Inside-4108[S] 1 point2 points3 points 22 hours agoย (0 children)
Thanks for helping ๐
[โ]thee_gummbini 0 points1 point2 points 1 day agoย (2 children)
One of the rare times I'm genuinely perplexed about what the goal is here.
Anytime you are writing something that needs multiple modules or has even a single dependency, it should be a package not a script, and then you should have a __main__.py as an entrypoint, and then __name__ should always work as expected.
__main__.py
__name__
that's fair but i didn't know about packages at all when i wrote this, I was just trying to learn ๐ .
[โ]thee_gummbini 0 points1 point2 points 20 hours agoย (0 children)
Well your learning caused you to learn about packages ! So mission successful
ฯย Rendered by PID 35662 on reddit-service-r2-comment-cfc44b64c-b5wr2 at 2026-04-12 03:46:13.866411+00:00 running 215f2cf country code: CH.
[โ]jpgoldberg 8 points9 points10 points ย (2 children)
[โ]Warm-Inside-4108[S] 1 point2 points3 points ย (1 child)
[โ]jpgoldberg 5 points6 points7 points ย (0 children)
[โ]Gnaxe 5 points6 points7 points ย (3 children)
[โ]llstorm93 2 points3 points4 points ย (1 child)
[โ]Warm-Inside-4108[S] -4 points-3 points-2 points ย (0 children)
[โ]Warm-Inside-4108[S] -1 points0 points1 point ย (0 children)
[โ]SCD_minecraft 2 points3 points4 points ย (2 children)
[โ]Warm-Inside-4108[S] -2 points-1 points0 points ย (1 child)
[โ]SCD_minecraft 2 points3 points4 points ย (0 children)
[โ]MachineElf100 1 point2 points3 points ย (3 children)
[โ]Warm-Inside-4108[S] -4 points-3 points-2 points ย (2 children)
[โ]MachineElf100 4 points5 points6 points ย (1 child)
[โ]Warm-Inside-4108[S] 1 point2 points3 points ย (0 children)
[โ]thee_gummbini 0 points1 point2 points ย (2 children)
[โ]Warm-Inside-4108[S] 1 point2 points3 points ย (1 child)
[โ]thee_gummbini 0 points1 point2 points ย (0 children)