How do I get the path of the current executed file in Python?
--
Track title: CC E Schuberts Piano Sonata D 784 in A
--
Chapters
00:00 Question
02:17 Accepted answer (Score 88)
03:49 Answer 2 (Score 96)
04:08 Answer 3 (Score 27)
04:25 Answer 4 (Score 16)
05:09 Thank you
--
Full question
https://stackoverflow.com/questions/2632...
Question links:
[a workaround]: http://www.py2exe.org/index.cgi/WhereAmI
[IDLE]: https://en.wikipedia.org/wiki/IDLE
[Mac OS X v10.6]: https://en.wikipedia.org/wiki/Mac_OS_X_S...
[Find path to currently running file]: https://stackoverflow.com/questions/1296...
[Path to current file depends on how I execute the program]: https://stackoverflow.com/questions/1483...
[How can I know the path of the running script in Python?]: https://stackoverflow.com/questions/2259...
[Change directory to the directory of a Python script]: https://stackoverflow.com/questions/5097...
Answer 3 links:
[IDLE]: https://en.wikipedia.org/wiki/IDLE
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #path #directory
#avk47
ANSWER 1
Score 103
First, you need to import from inspect and os
from inspect import getsourcefile
from os.path import abspath
Next, wherever you want to find the source file from you just use
abspath(getsourcefile(lambda:0))
ACCEPTED ANSWER
Score 89
You can't directly determine the location of the main script being executed. After all, sometimes the script didn't come from a file at all. For example, it could come from the interactive interpreter or dynamically generated code stored only in memory.
However, you can reliably determine the location of a module, since modules are always loaded from a file. If you create a module with the following code and put it in the same directory as your main script, then the main script can import the module and use that to locate itself.
some_path/module_locator.py:
def we_are_frozen():
# All of the modules are built-in to the interpreter, e.g., by py2exe
return hasattr(sys, "frozen")
def module_path():
encoding = sys.getfilesystemencoding()
if we_are_frozen():
return os.path.dirname(unicode(sys.executable, encoding))
return os.path.dirname(unicode(__file__, encoding))
some_path/main.py:
import module_locator
my_path = module_locator.module_path()
If you have several main scripts in different directories, you may need more than one copy of module_locator.
Of course, if your main script is loaded by some other tool that doesn't let you import modules that are co-located with your script, then you're out of luck. In cases like that, the information you're after simply doesn't exist anywhere in your program. Your best bet would be to file a bug with the authors of the tool.
ANSWER 3
Score 31
This solution is robust even in executables:
import inspect, os.path
filename = inspect.getframeinfo(inspect.currentframe()).filename
path = os.path.dirname(os.path.abspath(filename))
ANSWER 4
Score 16
I was running into a similar problem, and I think this might solve the problem:
def module_path(local_function):
''' returns the module path without the use of __file__. Requires a function defined
locally in the module.
from http://stackoverflow.com/questions/729583/getting-file-path-of-imported-module'''
return os.path.abspath(inspect.getsourcefile(local_function))
It works for regular scripts and in IDLE. All I can say is try it out for others!
My typical usage:
from toolbox import module_path
def main():
pass # Do stuff
global __modpath__
__modpath__ = module_path(main)
Now I use _modpath_ instead of _file_.