[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Linux/Win32 func. to get Python instdir (not exedir) + site-packages => extensions mgmt

pythonewbie

1/20/2008 11:04:00 AM

Hi all,

I am newbie in Python, my wish would be to create python applications
for both Linux/Win32.

I am stucked on creating a function to get the Python install
directory (and site-packages directory) with a 100% reliable method...

My goal is to verify if an/several extension(s) are installed and to
automatically install the missing ones on Linux or Win32.

I have tested sys.executable and sys.path, but I am not sure to be
able to get what I need on different versions of Python and different
platforms.

Google was not a good friend on this, so I am very interested on how
you implement such a function.

Cheers.
24 Answers

Christian Heimes

1/20/2008 11:21:00 AM

0

pythonewbie wrote:
> I am stucked on creating a function to get the Python install
> directory (and site-packages directory) with a 100% reliable method...

Only one method is 100% reliable:

try:
import yourextension
except ImportError:
available = False
else:
available = True

Christian

pythonewbie

1/20/2008 4:50:00 PM

0

On 20 jan, 12:20, Christian Heimes <li...@cheimes.de> wrote:
> pythonewbie wrote:
> > I am stucked on creating a function to get the Python install
> > directory (and site-packages directory) with a 100% reliable method...
>
> Only one method is 100% reliable:
>
> try:
> import yourextension
> except ImportError:
> available = False
> else:
> available = True
>
> Christian

Hi Christian,

OK thanks, interesting to detect if an extension is available or not.

But for different reasons I also want to get the absolute path of
Python install directory (not only the executable under Linux) and
site-packages directory.

How could I proceed ?

Diez B. Roggisch

1/20/2008 6:51:00 PM

0

pythonewbie schrieb:
> On 20 jan, 12:20, Christian Heimes <li...@cheimes.de> wrote:
>> pythonewbie wrote:
>>> I am stucked on creating a function to get the Python install
>>> directory (and site-packages directory) with a 100% reliable method...
>> Only one method is 100% reliable:
>>
>> try:
>> import yourextension
>> except ImportError:
>> available = False
>> else:
>> available = True
>>
>> Christian
>
> Hi Christian,
>
> OK thanks, interesting to detect if an extension is available or not.
>
> But for different reasons I also want to get the absolute path of
> Python install directory (not only the executable under Linux) and
> site-packages directory.
>
> How could I proceed ?

Maybe sys.path is a starter?

Diez

pythonewbie

1/20/2008 7:38:00 PM

0

On 20 jan, 19:50, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> pythonewbie schrieb:
>
>
>
> > On 20 jan, 12:20, Christian Heimes <li...@cheimes.de> wrote:
> >> pythonewbie wrote:
> >>> I am stucked on creating a function to get the Python install
> >>> directory (and site-packages directory) with a 100% reliable method...
> >> Only one method is 100% reliable:
>
> >> try:
> >> import yourextension
> >> except ImportError:
> >> available = False
> >> else:
> >> available = True
>
> >> Christian
>
> > Hi Christian,
>
> > OK thanks, interesting to detect if an extension is available or not.
>
> > But for different reasons I also want to get the absolute path of
> > Python install directory (not only the executable under Linux) and
> > site-packages directory.
>
> > How could I proceed ?
>
> Maybe sys.path is a starter?
>
> Diez

Yes, it is, but my problem is that I am not sure to find the
information I need at the same position of the list generated by
sys.path.

I explain, for Win32, I find install directory using sys.path[6] and
site-package directory using sys.path[7], for Linux I find install
directory using sys.path[2] and site-package directory using
sys.path[6].

For my tests, I have used XP Pro and Ubuntu Gutsy.

I am not sure to find these information at the same position in the
sys.path list using Win9x, Win2k, Ubuntu Dapper, Redhat FC6, FreeBSD
and using Python v2.1 2.2 2.3 etc ?

This why I'm asking experienced programmers of this usenet group for
advices.

Michael L Torrie

1/20/2008 7:50:00 PM

0

pythonewbie wrote:
> Hi all,
>
> I am newbie in Python, my wish would be to create python applications
> for both Linux/Win32.
>
> I am stucked on creating a function to get the Python install
> directory (and site-packages directory) with a 100% reliable method...
>
> My goal is to verify if an/several extension(s) are installed and to
> automatically install the missing ones on Linux or Win32.
>
> I have tested sys.executable and sys.path, but I am not sure to be
> able to get what I need on different versions of Python and different
> platforms.
>
> Google was not a good friend on this, so I am very interested on how
> you implement such a function.

On both windows and Linux, installing stuff into the python install dir
requires root or administrative privileges, something most linux users
won't have unless they sudo. So if the extensions you need are missing,
and you are distributing them yourself, why not just add them into the
path at runtime, rather than messing with the user's system?

As for obtaining the installation path, the setup.py that often comes
with python modules like ldaptor, seems to be able to figure it out.
I'd check there. I think setup.py is created with distutils.


>
> Cheers.

Diez B. Roggisch

1/20/2008 8:00:00 PM

0

pythonewbie schrieb:
> On 20 jan, 19:50, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
>> pythonewbie schrieb:
>>
>>
>>
>>> On 20 jan, 12:20, Christian Heimes <li...@cheimes.de> wrote:
>>>> pythonewbie wrote:
>>>>> I am stucked on creating a function to get the Python install
>>>>> directory (and site-packages directory) with a 100% reliable method...
>>>> Only one method is 100% reliable:
>>>> try:
>>>> import yourextension
>>>> except ImportError:
>>>> available = False
>>>> else:
>>>> available = True
>>>> Christian
>>> Hi Christian,
>>> OK thanks, interesting to detect if an extension is available or not.
>>> But for different reasons I also want to get the absolute path of
>>> Python install directory (not only the executable under Linux) and
>>> site-packages directory.
>>> How could I proceed ?
>> Maybe sys.path is a starter?
>>
>> Diez
>
> Yes, it is, but my problem is that I am not sure to find the
> information I need at the same position of the list generated by
> sys.path.
>
> I explain, for Win32, I find install directory using sys.path[6] and
> site-package directory using sys.path[7], for Linux I find install
> directory using sys.path[2] and site-package directory using
> sys.path[6].
>
> For my tests, I have used XP Pro and Ubuntu Gutsy.
>
> I am not sure to find these information at the same position in the
> sys.path list using Win9x, Win2k, Ubuntu Dapper, Redhat FC6, FreeBSD
> and using Python v2.1 2.2 2.3 etc ?
>
> This why I'm asking experienced programmers of this usenet group for
> advices.

Sorry, I missed your first post. However, I don't see what your problem
actually is. If you want to look for any extension, you need to consider
whatever can be seen in the sys.path. So what do you care about the
order of them?

Diez

pythonewbie

1/20/2008 9:58:00 PM

0

On 20 jan, 20:59, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> pythonewbie schrieb:
>
>
>
> > On 20 jan, 19:50, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> >> pythonewbie schrieb:
>
> >>> On 20 jan, 12:20, Christian Heimes <li...@cheimes.de> wrote:
> >>>> pythonewbie wrote:
> >>>>> I am stucked on creating a function to get the Python install
> >>>>> directory (and site-packages directory) with a 100% reliable method...
> >>>> Only one method is 100% reliable:
> >>>> try:
> >>>> import yourextension
> >>>> except ImportError:
> >>>> available = False
> >>>> else:
> >>>> available = True
> >>>> Christian
> >>> Hi Christian,
> >>> OK thanks, interesting to detect if an extension is available or not.
> >>> But for different reasons I also want to get the absolute path of
> >>> Python install directory (not only the executable under Linux) and
> >>> site-packages directory.
> >>> How could I proceed ?
> >> Maybe sys.path is a starter?
>
> >> Diez
>
> > Yes, it is, but my problem is that I am not sure to find the
> > information I need at the same position of the list generated by
> > sys.path.
>
> > I explain, for Win32, I find install directory using sys.path[6] and
> > site-package directory using sys.path[7], for Linux I find install
> > directory using sys.path[2] and site-package directory using
> > sys.path[6].
>
> > For my tests, I have used XP Pro and Ubuntu Gutsy.
>
> > I am not sure to find these information at the same position in the
> > sys.path list using Win9x, Win2k, Ubuntu Dapper, Redhat FC6, FreeBSD
> > and using Python v2.1 2.2 2.3 etc ?
>
> > This why I'm asking experienced programmers of this usenet group for
> > advices.
>
> Sorry, I missed your first post. However, I don't see what your problem
> actually is. If you want to look for any extension, you need to consider
> whatever can be seen in the sys.path. So what do you care about the
> order of them?
>
> Diez

I just would like to know if I would ALWAYS find the install directory
in sys.path[6] and site-packages directory in sys.path[7] on any Win32
platform and sys.path[2] and site-packages directory in sys.path[6] on
any Linux platform.

If the reply is : "YES you can be sure of it !"

All would be great for me and I would be ready to create a script to
detect with a reliable manner the installation dir. et site-packages
dir. for all my Linux/Win32 Python apps.

Thanks for your interest on this topic.


Martin v. Loewis

1/20/2008 10:20:00 PM

0

> But for different reasons I also want to get the absolute path of
> Python install directory (not only the executable under Linux) and
> site-packages directory.

The Python install directory is available as sys.prefix. The
site-packages directory is
sys.prefix+"lib/python"+x.y+"/site-packages (where x.y is from
sys.version_info).

HTH,
Martin

pythonewbie

1/20/2008 10:49:00 PM

0

On 20 jan, 23:19, "Martin v. Löwis" <mar...@v.loewis.de> wrote:
> > But for different reasons I also want to get the absolute path of
> > Python install directory (not only the executable under Linux) and
> > site-packages directory.
>
> The Python install directory is available as sys.prefix. The
> site-packages directory is
> sys.prefix+"lib/python"+x.y+"/site-packages (where x.y is from
> sys.version_info).
>
> HTH,
> Martin

http://forum.ubuntu-fr.org/viewtopic.php...

>>> import distutils.sysconfig
>>> distutils.sysconfig.get_python_lib()
'/usr/lib/python2.5/site-packages'

get_python_lib(plat_specific=0, standard_lib=0, prefix=None)
Return the directory containing the Python library (standard
or
site additions).

If 'plat_specific' is true, return the directory containing
platform-specific modules, i.e. any module from a non-pure-
Python
module distribution; otherwise, return the platform-shared
library
directory. If 'standard_lib' is true, return the directory
containing standard Python library modules; otherwise, return
the
directory for site-specific modules.

If 'prefix' is supplied, use it instead of sys.prefix or
sys.exec_prefix -- i.e., ignore 'plat_specific'.

Diez B. Roggisch

1/20/2008 10:56:00 PM

0

pythonewbie schrieb:
> On 20 jan, 20:59, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
>> pythonewbie schrieb:
>>
>>
>>
>>> On 20 jan, 19:50, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
>>>> pythonewbie schrieb:
>>>>> On 20 jan, 12:20, Christian Heimes <li...@cheimes.de> wrote:
>>>>>> pythonewbie wrote:
>>>>>>> I am stucked on creating a function to get the Python install
>>>>>>> directory (and site-packages directory) with a 100% reliable method...
>>>>>> Only one method is 100% reliable:
>>>>>> try:
>>>>>> import yourextension
>>>>>> except ImportError:
>>>>>> available = False
>>>>>> else:
>>>>>> available = True
>>>>>> Christian
>>>>> Hi Christian,
>>>>> OK thanks, interesting to detect if an extension is available or not.
>>>>> But for different reasons I also want to get the absolute path of
>>>>> Python install directory (not only the executable under Linux) and
>>>>> site-packages directory.
>>>>> How could I proceed ?
>>>> Maybe sys.path is a starter?
>>>> Diez
>>> Yes, it is, but my problem is that I am not sure to find the
>>> information I need at the same position of the list generated by
>>> sys.path.
>>> I explain, for Win32, I find install directory using sys.path[6] and
>>> site-package directory using sys.path[7], for Linux I find install
>>> directory using sys.path[2] and site-package directory using
>>> sys.path[6].
>>> For my tests, I have used XP Pro and Ubuntu Gutsy.
>>> I am not sure to find these information at the same position in the
>>> sys.path list using Win9x, Win2k, Ubuntu Dapper, Redhat FC6, FreeBSD
>>> and using Python v2.1 2.2 2.3 etc ?
>>> This why I'm asking experienced programmers of this usenet group for
>>> advices.
>> Sorry, I missed your first post. However, I don't see what your problem
>> actually is. If you want to look for any extension, you need to consider
>> whatever can be seen in the sys.path. So what do you care about the
>> order of them?
>>
>> Diez
>
> I just would like to know if I would ALWAYS find the install directory
> in sys.path[6] and site-packages directory in sys.path[7] on any Win32
> platform and sys.path[2] and site-packages directory in sys.path[6] on
> any Linux platform.
>
> If the reply is : "YES you can be sure of it !"
>
> All would be great for me and I would be ready to create a script to
> detect with a reliable manner the installation dir. et site-packages
> dir. for all my Linux/Win32 Python apps.
>
> Thanks for your interest on this topic.


I doubt that you can say such things. You can even manipulate the path
at runtime.

And I still don't understand WHY you want that? If you want
site-packages, why don't you loop through the paths until you find it?

Diez