How do I get the path of the current executed file in python?

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.

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.

I mention that on OS 10.6 I get NameError: global name '__file__' is not defined using file and this is not inside the IDLE. Think that __file__ is defined only inside modules. – sorin Apr 13 '10 at 18:59 1 @Sorin Sbarnea: I updated my answer with how I get around that.

– Daniel Stutzbach Apr 13 '10 at 19:37 Thanks, but in fact the problem with missing __file__ had nothing to do with Unicode. I don't know why __file__ is not defined but I'm looking for a generic solution this will work an all cases. – sorin Apr 13 '10 at 21:29 @Sorin Sbarnea, unicode isn't the part that I added.

The key, I believe, is to put the file in a module, not in the main file. In other words, you should be calling my_module. Module_path().

– Daniel Stutzbach Apr 13 '10 at 23:25 Sorry this is not possible in all cases. For example I try to do this in waf.googlecode. Com from inside a wscript file (python).

These files are executed but they are not modules and you cannot made them modules (they can be any any subdirectory from the source tree). – sorin Apr 13 '107 at 10:01.

The short answer is that there is no guaranteed way to get the information you want, however there are heuristics that work almost always in practice. You might look at stackoverflow.com/questions/933850/how-t.... It discusses the problem from a C point of view, but the proposed solutions are easily transcribed into python.

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 stackoverflow.com/questions/729583/getti... 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!

Edit: 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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions