473,473 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with Distutils

Hi!

I just started playing around with Distutils and there is one thing I
could use some help with. I'm wondering if there is some convenient way
to access the directories where the data_files was installed?

--
Johan Svedberg, jo***@svedberg.pp.se, http://johan.svedberg.pp.se/
Jul 18 '05 #1
8 1719
On Qui 13 Mai 2004 13:14, Johan Svedberg wrote:
I just started playing around with Distutils and there is one thing I
could use some help with. I'm wondering if there is some convenient way
to access the directories where the data_files was installed?


Could you explain when and why you are trying to do that?

--
Godoy. <go***@ieee.org>
Jul 18 '05 #2
* Jorge Godoy <go***@ieee.org>:
On Qui 13 Mai 2004 13:14, Johan Svedberg wrote:
I just started playing around with Distutils and there is one thing I
could use some help with. I'm wondering if there is some convenient
way to access the directories where the data_files was installed?


Could you explain when and why you are trying to do that?


Well, the reason is that I've told setup.py to install a bunch of
templates in <prefix>/share/myapp/templates/ and I want to make sure
that myapp can find these no matter what <prefix> is.

--
Johan Svedberg, jo***@svedberg.pp.se, http://johan.svedberg.pp.se/
Jul 18 '05 #3
At some point, Johan Svedberg <jo***@svedberg.pp.se> wrote:
Hi!

I just started playing around with Distutils and there is one thing I
could use some help with. I'm wondering if there is some convenient way
to access the directories where the data_files was installed?


If you're using the data_files argument to setup(), it's behaviour is
described in the manual. Basically, if the specified directory is
absolute, the files go there. Otherwise, the directory is relative to
sys.prefix (or sys.exec_prefix if extension modules are installed).

For example (from the documentation):

### setup.py
setup(...
py_modules = ['mod'],
data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
('config', ['cfg/data.cfg']),
('/etc/init.d', ['init-script'])]
)
###

I'll assume sys.prefix is '/usr'.

b1.gif and b2.gif will installed by default /usr/bitmaps',
and data.cfg in /usr/config. init-script will be installed in /etc/init.d.

Now, there's a difficulty. If I run the setup.py script like this:

$ python setup.py install --prefix=/usr/local

then b1.gif, for instance, gets installed in /usr/local/bitmaps. So
you can't hard-code /usr/bitmaps as the path to look at in your
application. One thing you can do is use the __file__ variable that's
defined in a module to find out where that module is installed, and
work your way up.

With the above call to setup.py, the module 'mod.py' would be
installed in /usr/local/lib/python2.3/site-packages, and within
mod.py, __file__ would be
'/usr/local/lib/python2.3/site-packages/mod.pyc', so you could walk
the tree up.

However, there's more problems. I install somethings in my home
directory like this:

$ python setup.py install --home=~/usr

Now mod.__file__ is '/home/cookedm/usr/lib/python/mod.pyc'. Things can
get more complicated if the --install-lib option is used.

Ugh.

One solution that I've seen used several times is to install data
files in the package directory:

### setup.py
from distutils.core import setup
from distutils.command.install_data import install_data

class my_install_data(install_data):
def finalize_options(self):
self.set_undefined_options('install',
('install_lib', 'install_dir'))
install_data.finalize_options(self)

setup(...
packages = ['mypackage', 'mypackage.data'],
data_files=[('mypackage/data', ['bm/b1.gif', 'bm/b2.gif'])],
cmdclass = { 'install_data' : my_install_data }
)
###

b1.gif and b2.gif will now be installed in the same directory as the
modules in mypackage.data. A file mypackage.data.__init.py can look
like this:

### mypackage.data.__init__
import os.path
def data_dir():
mod_loc = os.path.dirname(__file__)
return os.path.abspath(mod_loc)
###

Then in your main code,

### mypackage.do_stuff_module
import mypackage.data
....
data_dir = mypackage.data.data_dir()
b1gif = open(os.path.join(data_dir, 'b1.gif'))
###

Hope this helps.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #4
* David M. Cooke <co**********@physics.mcmaster.ca>:

[...]
Hope this helps.


It does! This was the exact problems I thought of myself! A huuuge
thanks for taking the time to break this down for me!

Regards,

--
Johan Svedberg, jo***@svedberg.pp.se, http://johan.svedberg.pp.se/
Jul 18 '05 #5
On Qui 13 Mai 2004 15:39, Johan Svedberg wrote:
Well, the reason is that I've told setup.py to install a bunch of
templates in <prefix>/share/myapp/templates/ and I want to make sure
that myapp can find these no matter what <prefix> is.


I see somebody suggested that you use __file__, from the module.

Another option --- I'm using it --- is to have a config file and have
distutils to call a postinstall script (requires Python 2.3+) to update the
directory in such a config file.

You can parse it with ConfigParser in your application. It also adds an
interesting way of putting things in different places and still having them
to work as expected :-)

--
Godoy. <go***@ieee.org>
Jul 18 '05 #6
At some point, Jorge Godoy <go***@ieee.org> wrote:
On Qui 13 Mai 2004 15:39, Johan Svedberg wrote:
Well, the reason is that I've told setup.py to install a bunch of
templates in <prefix>/share/myapp/templates/ and I want to make sure
that myapp can find these no matter what <prefix> is.


I see somebody suggested that you use __file__, from the module.

Another option --- I'm using it --- is to have a config file and have
distutils to call a postinstall script (requires Python 2.3+) to update the
directory in such a config file.


Of course, you have to able to find the config file, but that's a
smaller kettle of fish. I suppose you could put the config file in
your modules directory (like I suggested), and have your other data
files sitting outside (in /usr/share/whatever).

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #7
On Qui 13 Mai 2004 17:51, David M. Cooke wrote:
Of course, you have to able to find the config file, but that's a
smaller kettle of fish. I suppose you could put the config file in
your modules directory (like I suggested), and have your other data
files sitting outside (in /usr/share/whatever).


If all your concerns are with unix machines, taht would be great. I don't
know if his software should also run on other platforms and OSs. Anyway,
you have basically two points, for Unix and Windows: /etc/something/config
and c:\Windows\something\config. You can check the OS and go to the correct
directory... It makes it harder to find only one file: the config file. The
other way you'd have to check all the other files...

--
Godoy. <go***@ieee.org>
Jul 18 '05 #8
* Johan Svedberg <jo***@svedberg.pp.se>:
* Jorge Godoy <go***@ieee.org>:
On Qui 13 Mai 2004 13:14, Johan Svedberg wrote:
I just started playing around with Distutils and there is one thing
I could use some help with. I'm wondering if there is some
convenient way to access the directories where the data_files was
installed?


Could you explain when and why you are trying to do that?


Well, the reason is that I've told setup.py to install a bunch of
templates in <prefix>/share/myapp/templates/ and I want to make sure
that myapp can find these no matter what <prefix> is.


When I think more about it, the cleanest way would probably be if I
could in some way patch the relevant files so that they point to the
right location. This would ofcourse have to be done after the user has
decided where he wants to install things. Very much like the way
configure and make works together.

I'm going to look around more to see if I can find any good solution by
looking at other peoples projects. I find it really strange that there
isn't some generic way of doing this since application installment is a
pretty central part.

--
Johan Svedberg, jo***@svedberg.pp.se, http://johan.svedberg.pp.se/
Jul 18 '05 #9

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

Similar topics

3
by: Isaac Jones | last post by:
Hello Python Community. I bring greetings from the Haskell community (http://www.haskell.org) ;) There has been a lot of discussion of late about creating a grand-unified build & distribution...
0
by: Jorge Godoy | last post by:
Hi! For the following setup.py, I get this error: byte-compiling /var/tmp/bhsys-buildroot/usr/lib/python2.3/site-packages/bhsys/Impressao.py to Impressao.pyc byte-compiling...
1
by: Mathieu Malaterre | last post by:
Hello, I thought this would be easy but I guess I didn't get the distutil feeling. I am trying to write a setup for install my package but I don't understand how to do that. organisation: ...
15
by: Colin J. Williams | last post by:
The distutils download page has: -------------------------------------------------------- Current stable release The current stable release is Distutils 1.0.2; you can download it as: *...
1
by: Terry Hancock | last post by:
Some time ago, I got the idea that I wanted to build image resources from vector graphic originals, instead of marshalling hundreds of tiny little icon images by hand. I wrote "BuildImage" to do...
7
by: Scott | last post by:
I've installed Python 2.4 under WinXP and am attempting to create an extension module using the steps outlined here: http://python.org/doc/2.4/ext/win-cookbook.html I'm specifically trying to...
0
by: Maarten Sneep | last post by:
I'm trying to build PyBison on Mac OS X, and I'm running into some problems with the distutils. Just for starters: PyBison requires Pyrex. This is not a problem, and Pyrex seems to work without...
6
by: ajikoe | last post by:
Hello I tried to combine c++ and python together. So I follow from this website: http://kortis.to/radix/python_ext/ I have this code: # prmodule.c static PyObject *pr_isprime(PyObject *self,...
3
by: Rick Muller | last post by:
I need some distutils help. I currently run a python library (PyQuante) that, until recently, had all of its modules in a single directory, called "PyQuante". The setup command in my setup.py...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.