Re: Developing a Package with Sub Packages

2008-02-18 Thread Gabriel Genellina
En Sun, 17 Feb 2008 22:34:27 -0200, Josh English  
[EMAIL PROTECTED] escribi�:

 Here's what I think is happening: IMS/__init__.py uses os.getcwd() to
 establish the path to the data folder and the files inside of it. When
 I run StoryCreator, os.getcwd() returns the story folder.
 If I'm right, how can I get the IMS/__init__.py module to use relative
 paths to its own module, and not the current working directory the os
 module provides?

Use the module's own __file__ attribute:
my_path = os.path.abspath(os.path.dirname(__file__))

-- 
Gabriel Genellina

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

Re: Developing a Package with Sub Packages

2008-02-18 Thread Josh English
When testing the package in idle, this results in
C:\Python25\Lib\idlelib
instead of the file.
The Data folder is created in this folder now.

On 2/18/08, Gabriel Genellina [EMAIL PROTECTED] wrote:
 En Sun, 17 Feb 2008 22:34:27 -0200, Josh English
 [EMAIL PROTECTED] escribi�:

  Here's what I think is happening: IMS/__init__.py uses os.getcwd() to
  establish the path to the data folder and the files inside of it. When
  I run StoryCreator, os.getcwd() returns the story folder.
  If I'm right, how can I get the IMS/__init__.py module to use relative
  paths to its own module, and not the current working directory the os
  module provides?

 Use the module's own __file__ attribute:
 my_path = os.path.abspath(os.path.dirname(__file__))

 --
 Gabriel Genellina

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


-- 
Josh English
[EMAIL PROTECTED]
http://joshenglish.livejournal.com
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Developing a Package with Sub Packages

2008-02-18 Thread Gabriel Genellina
En Mon, 18 Feb 2008 16:23:28 -0200, Josh English  
[EMAIL PROTECTED] escribi�:

 When testing the package in idle, this results in
 C:\Python25\Lib\idlelib
 instead of the file.
 The Data folder is created in this folder now.

Works for me:

main.py:
 from testpkg import a

testpkg directory (a subdirectory somewhere in sys.path):
testpkg\__init__.py:
(empty file)

testpkg\a.py:
import os
print __name__, __file__, os.path.abspath(os.path.dirname(__file__))

In IDLE: File - Open, main.py. F5 (Run Module). Output:

testpkg.a C:\APPS\PYTHON25\lib\site-packages\testpkg\a.pyc
C:\APPS\PYTHON25\lib\site-packages\testpkg

-- 
Gabriel Genellina

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

Developing a Package with Sub Packages

2008-02-17 Thread Josh English
I have created a group of scripts to manage an XML-based database. I'd
like to make it into a proper package that will let me keep track of
the code. I have a lot of files that are similar in name and they just
get crowded in one folder.

Here's a sample of the file structure:

IMS/
IMS/__init__.py
IMS/Config.py
IMS/imsdefaults.cfg
IMS/local.txt
IMS/Data/
IMS/Data/stories.xml
IMS/Story/
IMS/Story/__init__.py
IMS/Story/StoryCreator.py

When the IMS/__init__.py file is loaded, it creates the IMS/Data/
folder and if there are no xml files, it creates them and fills them
in with some default values.
The IMS/Config.py has a subclass of the ConfigParser, and reads the
imsdefaults.cfg and local.txt files.
When I run StoryCreator, buried in it's own package (that imports IMS)
the data folder is created inside the Story folder, and I get an error
message stating the ConfigParser object could not find the
imsdefaults.cfg file.
Here's what I think is happening: IMS/__init__.py uses os.getcwd() to
establish the path to the data folder and the files inside of it. When
I run StoryCreator, os.getcwd() returns the story folder.
If I'm right, how can I get the IMS/__init__.py module to use relative
paths to its own module, and not the current working directory the os
module provides?
This could also solve the problem with Config.py, I think.

Thanks

Josh English
http://joshenglish.livejournal.com
-- 
http://mail.python.org/mailman/listinfo/python-list