Linux application deployment

J

Jarek Zgoda

What you consider a "best way to deploy linux python app"? I don't want
to install library modules into user's site-packages, so distutils is no
help. Currently, I change sys.path (adding directory where library
modules are stored) and I ask users to install application into /opt
hierarchy, but I don't think it's optimal.
 
R

Robert M. Emmons

What you consider a "best way to deploy linux python app"? I don't want
to install library modules into user's site-packages, so distutils is no
help.
Yes, I agree. I've noticed that. It seems to me that the only
appropriate thing to install in site-packages are libraries to be used
to basically extend pythong -- not applications.
> Currently, I change sys.path (adding directory where library
modules are stored) and I ask users to install application into /opt
hierarchy, but I don't think it's optimal.

My opinion. You can install it where you want. Typically either /opt
or /usr/local depending if you want to do a merged or separate install.
I would layout the package according the the GNU Standard Directory
structure, and consistent with the Linux Standard Base (or HFS). That I
think would mean that you'd put the modules into
${exec_prefix}/lib/mypackage-xx.yy.zz and then symlink any launch code
to the appropriate bin/ directory.

I would then reset sys.path to be the right thing in the startup module
- essentially the launcher. There are couple of way to do this -- the
installer can modify the python or sh launch code to hard wire the
install location or you can do like Gnome does -- have a script that is
in the path named mypackage-config where you can lookup the setup.

One thing I've always wonders was -- Is there a way for a python module
to tell where it's file is located??? If you could do this it might
also be possible to have a python script reset sys.path based on it's
own location.

Rob
 
J

Jarek Zgoda

Robert M. Emmons said:
One thing I've always wonders was -- Is there a way for a python module
to tell where it's file is located??? If you could do this it might
also be possible to have a python script reset sys.path based on it's
own location.

import sys, os

me = os.path.abspath(sys.argv[0])
 
J

Jeffrey Froman

Robert said:
One thing I've always wonders was -- Is there a way for a python module
to tell where it's file is located???  If you could do this it might
also be possible to have a python script reset sys.path based on it's
own location.

One way of doing this is:

import os, sys
directory, filename = os.path.split(__file__)
sys.path.append(directory)
 
G

Grant Edwards

Robert M. Emmons said:
One thing I've always wonders was -- Is there a way for a python module
to tell where it's file is located??? If you could do this it might
also be possible to have a python script reset sys.path based on it's
own location.

import sys, os

me = os.path.abspath(sys.argv[0])

That's only mostly reliable. Nothing in Linux/Unix actually requires that
argv[0] be the program's path. It is the convention to pass that as
argv[0], but there may be corner cases where it doesn't work.
 
J

Jarek Zgoda

Grant Edwards said:
import sys, os

me = os.path.abspath(sys.argv[0])

That's only mostly reliable. Nothing in Linux/Unix actually requires that
argv[0] be the program's path. It is the convention to pass that as
argv[0], but there may be corner cases where it doesn't work.

Based on Python docs:

"""
argv

The list of command line arguments passed to a Python script.
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not). If the command was executed using
the -c command line option to the interpreter, argv[0] is set to the
string '-c'. If no script name was passed to the Python interpreter,
argv has zero length.
"""

http://docs.python.org/lib/module-sys.html

In my opinion, this would be enough to get full path of currently
running program, if run from script. Are there any caveats (except this
"-c" option, which I don't count, as is not relevant in most cases)?
 
K

klappnase

Jarek Zgoda said:
Grant Edwards said:
import sys, os

me = os.path.abspath(sys.argv[0])

That's only mostly reliable. Nothing in Linux/Unix actually requires that
argv[0] be the program's path. It is the convention to pass that as
argv[0], but there may be corner cases where it doesn't work.

Based on Python docs:

"""
argv

The list of command line arguments passed to a Python script.
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not). If the command was executed using
the -c command line option to the interpreter, argv[0] is set to the
string '-c'. If no script name was passed to the Python interpreter,
argv has zero length.
"""

http://docs.python.org/lib/module-sys.html

In my opinion, this would be enough to get full path of currently
running program, if run from script. Are there any caveats (except this
"-c" option, which I don't count, as is not relevant in most cases)?

If you use a symbolic link to start the program sys.argv[0] will
return the location of the link instead of the path to your
application file I think. Anyway you can get the directory of the
application file with sys.path[0] if you need it.
However, why not just put all application files into one directory
(like /usr/local/share/yourApp or /opt/lib/myApp or something) and
start it from a link from /usr/local/bin (or /opt/bin) to your app's
main program file instead of messing around with sys.path , at least
it's the easiest solution and I don't really see the benefit of
putting the files into different directories unless you have modules
other people might want to use, in which case python's site-packages
directory would probably be the preferred location.
Just a personal opinion of course.

Regards

Michael
 
A

Alex Martelli

Robert M. Emmons said:
One thing I've always wonders was -- Is there a way for a python module
to tell where it's file is located??? If you could do this it might

Piece of cake: the key part of this recipe is
def whereami(): return sys.modules[__name__].__file__
then you can os.path.abspath this filename string as you wish.

Here's a more complete example foo.py:

#!/usr/bin/env python

import sys, os

def whereami(): return os.path.abspath(sys.modules[__name__].__file__)

if __name__ == '__main__': print whereami()


Put this anywhere on your $PATH, chmod +x it, and try out what you see
by executing it as the main script from various locations... should work
pretty reliably.
also be possible to have a python script reset sys.path based on it's
own location.

Sure, that shouldn't be impossible.


Alex
 
P

P

Alex said:
One thing I've always wonders was -- Is there a way for a python module
to tell where it's file is located??? If you could do this it might


Piece of cake: the key part of this recipe is
def whereami(): return sys.modules[__name__].__file__
then you can os.path.abspath this filename string as you wish.

That only seems to work for python 2.3 or later

One thing to consider when wondering where to put system wide
modules is that you have to worry about the python version
changing underneath you. This is because *.py[oc]
are not compatibile across minor version changes.

I.E. if one has pyc files in /usr/lib/site-python
and python 2.3 is installed subsequently on the system
then the modules probably will cause problems.

That is why py[co] files are installed in
version specific directories like:
/usr/lib/python2.2/site-packages/app/

Pádraig.
 

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
474,206
Messages
2,571,069
Members
47,674
Latest member
scazeho

Latest Threads

Top