How do I get the path and name of the file that is currently executing?
__file__
as others have said. You may also want to use os.path.realpath to eliminate symlinks:
import osos.path.realpath(__file__)
Update 2018-11-28:
Here is a summary of experiments with Python 2 and 3. With
main.py - runs foo.py
foo.py - runs lib/bar.py
lib/bar.py - prints filepath expressions
| Python | Run statement | Filepath expression ||--------+---------------------+----------------------------------------|| 2 | execfile | os.path.abspath(inspect.stack()[0][1]) || 2 | from lib import bar | __file__ || 3 | exec | (wasn't able to obtain it) || 3 | import lib.bar | __file__ |
For Python 2, it might be clearer to switch to packages so can use from lib import bar
- just add empty __init__.py
files to the two folders.
For Python 3, execfile
doesn't exist - the nearest alternative is exec(open(<filename>).read())
, though this affects the stack frames. It's simplest to just use import foo
and import lib.bar
- no __init__.py
files needed.
See also Difference between import and execfile
Original Answer:
Here is an experiment based on the answers in this thread - with Python 2.7.10 on Windows.
The stack-based ones are the only ones that seem to give reliable results. The last two have the shortest syntax, i.e. -
print os.path.abspath(inspect.stack()[0][1]) # C:\filepaths\lib\bar.pyprint os.path.dirname(os.path.abspath(inspect.stack()[0][1])) # C:\filepaths\lib
Here's to these being added to sys as functions! Credit to @Usagi and @pablog
Based on the following three files, and running main.py from its folder with python main.py
(also tried execfiles with absolute paths and calling from a separate folder).
C:\filepaths\main.py: execfile('foo.py')
C:\filepaths\foo.py: execfile('lib/bar.py')
C:\filepaths\lib\bar.py:
import sysimport osimport inspectprint "Python " + sys.versionprintprint __file__ # main.pyprint sys.argv[0] # main.pyprint inspect.stack()[0][1] # lib/bar.pyprint sys.path[0] # C:\filepathsprintprint os.path.realpath(__file__) # C:\filepaths\main.pyprint os.path.abspath(__file__) # C:\filepaths\main.pyprint os.path.basename(__file__) # main.pyprint os.path.basename(os.path.realpath(sys.argv[0])) # main.pyprintprint sys.path[0] # C:\filepathsprint os.path.abspath(os.path.split(sys.argv[0])[0]) # C:\filepathsprint os.path.dirname(os.path.abspath(__file__)) # C:\filepathsprint os.path.dirname(os.path.realpath(sys.argv[0])) # C:\filepathsprint os.path.dirname(__file__) # (empty string)printprint inspect.getfile(inspect.currentframe()) # lib/bar.pyprint os.path.abspath(inspect.getfile(inspect.currentframe())) # C:\filepaths\lib\bar.pyprint os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # C:\filepaths\libprintprint os.path.abspath(inspect.stack()[0][1]) # C:\filepaths\lib\bar.pyprint os.path.dirname(os.path.abspath(inspect.stack()[0][1])) # C:\filepaths\libprint