Cross-platform way to get default directory for binary files likeconsole scripts?

P

Piotr Dobrogost

Hi!

Is there cross-platform way to get default directory for binary files (console scripts for instance) the same way one can use sys.executable to get path to the Python's interpreter in cross-platform way?

Context:
There's Python script which runs various tools like pip using subprocess and we would like to make sure we run tools that accompany Python's interpreter used to run this script. Please note that the script may be run from within virtualenv which had not been activated - ./venv/bin/python our_script.py


Regards,
Piotr Dobrogost
 
N

Ned Batchelder

Hi!

Is there cross-platform way to get default directory for binary files (console scripts for instance) the same way one can use sys.executable to get path to the Python's interpreter in cross-platform way?

Context:
There's Python script which runs various tools like pip using subprocess and we would like to make sure we run tools that accompany Python's interpreter used to run this script. Please note that the script may be run from within virtualenv which had not been activated - ./venv/bin/python our_script.py


Regards,
Piotr Dobrogost

Hi Piotr, we talked about this briefly in #python this morning. I still
don't quite understand why you are averse to activating the virtualenv.
It is designed to solve precisely this problem: create an environment
that uses the natural OS tools (including PATH) to produce a consistent
environment that works the way tools expect.

If you don't activate the virtualenv, then you can look for your Python
executable using sys.executable, and see if the file you want to run is
in that same directory. I have no idea under what conditions that is
the right or wrong answer, and I don't know what to do if the file
you're looking for isn't in that directory.

Perhaps the shorter answer is, look in the Python executable directory,
then look in the directories on PATH.
 
O

Oscar Benjamin

Is there cross-platform way to get default directory for binary files (console scripts for instance) the same way one can use sys.executable to get path to the Python's interpreter in cross-platform way?

Context:
There's Python script which runs various tools like pip using subprocess and we would like to make sure we run tools that accompany Python's interpreter used to run this script. Please note that the script may be run from within virtualenv which had not been activated - ./venv/bin/python our_script.py

I'm not sure if I understand the question. Are you trying to find
where a script would go if it had been installed as a result of
'python setup.py install' or 'pip install ...'? If so there are
different places it could go depending not only on the system but also
how the packages were installed (e.g. --user).

You can find the default location in this roundabout way:

In [1]: from distutils.command.install import install

In [2]: from distutils.dist import Distribution

In [3]: c = install(Distribution())

In [4]: c.finalize_
c.finalize_options c.finalize_other c.finalize_unix

In [4]: c.finalize_options()

In [5]: c.insta
c.install_base c.install_headers c.install_lib
c.install_path_file c.install_platlib c.install_scripts
c.install_usersite
c.install_data c.install_layout c.install_libbase
c.install_platbase c.install_purelib c.install_userbase

In [5]: c.install_scripts
Out[5]: '/usr/local/bin'


Oscar
 
P

Piotr Dobrogost

I'm not sure if I understand the question. Are you trying to find
where a script would go if it had been installed as a result of
'python setup.py install' or 'pip install ...'?

If so there are
different places it could go depending not only on the system but also
how the packages were installed (e.g. --user).
Right.

You can find the default location in this roundabout way:

(...)

In [5]: c.install_scripts
Out[5]: '/usr/local/bin'

I think this is pretty much what I'm after, thanks.
I'm wondering if there's some API to get this info as what you showed is really roundabout way to achieve the goal...

Regards,
Piotr
 
N

Ned Batchelder

I'm not sure if I understand the question. Are you trying to find
where a script would go if it had been installed as a result of
'python setup.py install' or 'pip install ...'?

If so there are
different places it could go depending not only on the system but also
how the packages were installed (e.g. --user).
Right.

You can find the default location in this roundabout way:

(...)

In [5]: c.install_scripts
Out[5]: '/usr/local/bin'

I think this is pretty much what I'm after, thanks.
I'm wondering if there's some API to get this info as what you showed is really roundabout way to achieve the goal...

As roundabout and advanced as that code is, it doesn't give the right
answer for me. It returns None. On my Mac, after activating a virtualenv:

Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]
on darwin
Type "help", "copyright", "credits" or "license" for more information.['activate', 'activate.csh', 'activate.fish', 'activate_this.py',
'easy_install', 'easy_install-2.7', 'pip', 'pip-2.7', 'python',
'python2', 'python2.7']
 
O

Oscar Benjamin

I'm wondering if there's some API to get this info as what you showed is really roundabout way to achieve the goal...

There may be something that I don't know of. Distutils is generally
pretty clunky though if you're not trying to do one of the exact
things it was designed to do.


Oscar
 
P

Piotr Dobrogost

As roundabout and advanced as that code is, it doesn't give the right
answer for me. It returns None.

Indeed. I tried on Linux and got None both inside and outside virtualenv :(

Regards,
Piotr
 
O

Oscar Benjamin

As roundabout and advanced as that code is, it doesn't give the right answer
for me. It returns None. On my Mac, after activating a virtualenv:

Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on
darwin
Type "help", "copyright", "credits" or "license" for more information.

You forgot to call c.finalize_options() here which actually sets all
of these attributes.


Oscar
 
N

Ned Batchelder

As roundabout and advanced as that code is, it doesn't give the right answer
for me. It returns None. On my Mac, after activating a virtualenv:

Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on
darwin
Type "help", "copyright", "credits" or "license" for more information.
from distutils.command.install import install
from distutils.dist import Distribution
c = install(Distribution())

You forgot to call c.finalize_options() here which actually sets all
of these attributes.

Ah, good! Thanks!
 

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
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top