finding dir of main .py file

R

ron.longo

Is there any way that I can find the path of the main .py file of my
application?

For example, I have an application with some resources which are in a
subdirectory:


myPythonApp.py
/resources
image1
image2
etc.

If i just do a call to os.getcwd() I get back the directory I was in when I
typed 'python myPythonApp.py' which could be any directory. What I want is
the directory that contains the file myPythonApp.py. Then I can use this
directory to construct the path to the resources directory.

(Actually, the app I'm writing has several subdirectories with stuff that it
needs such as plugins, configuration files, workspaces, etc.

Thanks for the help.

Ron
 
R

Rick Dooling

Is there any way that I can find the path of the main .py file of my
application?

For example, I have an application with some resources which are in a
subdirectory:

myPythonApp.py
/resources
image1
image2
etc.

I just put the reference in my module. Don't hard code an absolute
path, use the environment tools.

app_path = os.getenv('HOME') + "/your_sub_dir"

resources_path = os.getenv('HOME') + "/your_sub_dir/resources"

If there's another way, someone else will jump in.

rd
 
S

Shane Geiger

Some usage of __file__ will always get what you want in various situations:

print __file__

print modulename.__file__

print os.getcwd() + "/" + __file__





Rick said:
I just put the reference in my module. Don't hard code an absolute
path, use the environment tools.

app_path = os.getenv('HOME') + "/your_sub_dir"

resources_path = os.getenv('HOME') + "/your_sub_dir/resources"

If there's another way, someone else will jump in.

rd


--
Shane Geiger
IT Director
National Council on Economic Education
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
R

ron.longo

Nope, maybe I'm not explaining myself well.

When I do os.getenv('HOME') I get back None.

According to the docs, 'HOME' is the user's home directory on some
platforms. Which is not what I want.

What I want is the directory in which an application's main .py file
resides. That is, when I type: python MyApp.py, I want to know in which
directory does MyApp.py reside?


Thanks,
Ron
 
M

Matt Nordhoff

ron.longo said:
Nope, maybe I'm not explaining myself well.

When I do os.getenv('HOME') I get back None.

According to the docs, 'HOME' is the user's home directory on some
platforms. Which is not what I want.

What I want is the directory in which an application's main .py file
resides. That is, when I type: python MyApp.py, I want to know in which
directory does MyApp.py reside?

Shane is right.

Just call os.path.dirname() on __file__ to get the directory.
--
 
J

John Machin

Shane is right.

Shane is half-right by accident.

__file__ is the path to the file currently being executed.
The OP wanted to know "the directory in which an application's
main .py file resides". Using __file__ will give the correct result
*only* when used in that "main .py" file. If you have multiple
applications and you want a common "where are my resources" function/
method in an imported module, __file__ is of no use.

sys.argv[0] is your friend.

C:\junk>type calledmodule.py
import sys
def print_filenames():
print "calledmodule: sys.argv[0] =", sys.argv[0]
print "calledmodule: __file__ =", __file__

C:\junk>type whereami.py
import sys, calledmodule
print "script: sys.argv[0] =", sys.argv[0]
print "script: __file__ =", __file__
calledmodule.print_filenames()

C:\junk>whereami.py
script: sys.argv[0] = C:\junk\whereami.py
script: __file__ = C:\junk\whereami.py
calledmodule: sys.argv[0] = C:\junk\whereami.py
calledmodule: __file__ = C:\junk\calledmodule.pyc

Note that "calledmodule" could quite easily have been in some other
directory e.g. C:\Python25\Lib\site-packages\calledmodule
\calledmodule.pyc

If you later want to use py2exe, you'll have to work a little bit
harder, but it still doesn't involve __file__.

Cheers,
John
 
S

Stefan Reichör

Shane Geiger said:
Some usage of __file__ will always get what you want in various situations:

print __file__

print modulename.__file__

print os.getcwd() + "/" + __file__


The following two versions are working for me:

import os, sys

print os.path.abspath(os.path.dirname(sys.argv[0]))
print os.path.abspath(os.path.dirname(__file__))



Stefan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top