Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

maybe a stupid question

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


python at p0w

Aug 8, 2008, 3:33 PM

Post #1 of 2 (188 views)
Permalink
maybe a stupid question

Hi,

I suppose this has already been asked in the list, but I ask anyway:

I want to determine from where my python app is executed, but I want to
determine the path of the real script file, not the path of the command
being executed (in case of symlink in a *bin dir in the system).

I explain:

I have an app installed in /usr/lib/python2.5/site-package/MyApp

I have a symlink in /usr/local/bin that points to
/usr/lib/python2.5/site-package/MyApp/myscript.py

Then, when I launch my script from anywhere using the symlink, how to
determine that the script is located in
/usr/lib/python2.5/site-package/MyApp ?

Regards,
Strato


--
http://mail.python.org/mailman/listinfo/python-list


lists at cheimes

Aug 8, 2008, 5:19 PM

Post #2 of 2 (181 views)
Permalink
Re: maybe a stupid question [In reply to]

Strato wrote:
> I have an app installed in /usr/lib/python2.5/site-package/MyApp
>
> I have a symlink in /usr/local/bin that points to
> /usr/lib/python2.5/site-package/MyApp/myscript.py
>
> Then, when I launch my script from anywhere using the symlink, how to
> determine that the script is located in
> /usr/lib/python2.5/site-package/MyApp ?


Put this in myscript.py:

import os.path
HERE = os.path.dirname(os.path.abspath(__file__))

You may also need

HERE = os.path.realpath(HERE)

Explanation:

Almost every module has a global variable __file__. It points to the
path from which the module was loaded. os.path.abspath() gives you the
absolute path to the module. os.path.dirname() strips away the file part
and gives you the path to the directory. os.path.realpath() migt be
required to eliminate all symbolic links.

Christian

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.