473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Self-awareness of imported modules? Do they know where they live?

Hi,

I have the following question: How can an imported module see/find the
path to itself?

Background: From my main script I import a module which needs a file
(AppleScript) located in the same directory as the imported module.
What I do not want is to tell the module the location of the
AppleScript file by passing this information from the main script,
because I want my imported module to be independent from the main
script.

So how can I do this? In AppleScript I would try something like "path
to me", but this does not work for imported scripts in AppleScript, as
you then only get the path to the main script. But can this be done in
Python? Can modules know where they are currently located, where they
live? Can they be self-aware?

I hope you understand my problem and I appreciate any helpful answers!

Happy scripting!

Martin

P.S.: Background-background: I try to build myself a library of Python
scripts, which are getting a lot of infro from AppleScripts which
reside in the same directoty (e.g. usrdrs.py & usrdrs.scpt, usrdrs.py
is reading/executing usrdrs.scpt and returning the gathered info).

Dec 15 '05 #1
4 1667
Martin M. wrote:
So how can I do this? In AppleScript I would try something like "path
to me", but this does not work for imported scripts in AppleScript, as
you then only get the path to the main script. But can this be done in
Python? Can modules know where they are currently located, where they
live? Can they be self-aware?


Yes, they can be:

modelnine@phoenix ~ $ cat test.py
import os

print os.path.abspath(__file__)
modelnine@phoenix ~ $ python test.py
/home/modelnine/test.py
modelnine@phoenix ~ $

HTH!

--- Heiko.
Dec 15 '05 #2
import os
print os.__file__

--
dsw
Dec 15 '05 #3
Hi Heiko,

Thanks so much for your help! I just tested it and it works like a
charm!

****Test File 1****
-- main.py
#!/usr/bin python

from lib import being

being.location()

****Test File 2****
-- being.py
#!/usr/bin/python

import os

def location():
print os.path.abspath(__file__)
****
results in:
martin% python main.py
/Volumes/CodeIsland/Projects/glashaus/glashaus0.1a/lib/being.pyc

Now I can use os.path.split() to get the parent directory and
os.path.join() to target my AppleScript file.

Thanks again!

Martin M.

Dec 15 '05 #4
The only self aware Python scripts that I'm aware are the timbot and
the effbot. Their sources are available from the PSU website at
Dec 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Grzegorz Staniak | last post by:
Hello, I'm a newbie Python user, a systems administrator - I've been trying to switch from Perl to Python for administrative tasks - and one thing I cannot understand so far is why I need the...
2
by: Jim Jewett | last post by:
Normally, I expect a subclass to act in a manner consistent with its Base classes. In particular, I don't expect to *lose* any functionality, unless that was the whole point of the subclass. ...
2
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e =...
4
by: David Coffin | last post by:
I'd like to subclass int to support list access, treating the integer as if it were a list of bits. Assigning bits to particular indices involves changing the value of the integer itself, but...
7
by: Andrew Robert | last post by:
Hi Everyone, I am having a problem with a class and hope you can help. When I try to use the class listed below, I get the statement that self is not defined. test=TriggerMessage(data) var...
24
by: Peter Maas | last post by:
The Python FAQ 1.4.5 gives 3 reasons for explicit self (condensed version): 1. Instance variables can be easily distinguished from local variables. 2. A method from a particular class can be...
84
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to...
6
by: Bart Kastermans | last post by:
I am playing with some trees. In one of the procedures I wrote for this I am trying to change self to a different tree. A tree here has four members (val/type/left/right). I found that self = SS...
1
by: lpyth | last post by:
class abc: def __init__(self,x,y,z): self.x = x self.y = y self.z = z def add(self): c = self.x + self.y + self.z
3
by: Aaron Gray | last post by:
Regarding 'self' :- if (self) document.write( "self<br>"); if (self === window) document.write( "self === window<br>"); else if (self == window) document.write( "self == window<br>");
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.