Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #57704 > unrolled thread

Function for the path of the script?

Started byPeter Cacioppi <peter.cacioppi@gmail.com>
First post2013-10-26 19:23 -0700
Last post2013-10-28 22:56 -0700
Articles 9 on this page of 29 — 13 participants

Back to article view | Back to comp.lang.python


Contents

  Function for the path of the script? Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-26 19:23 -0700
    Re: Function for the path of the script? Skip Montanaro <skip.montanaro@gmail.com> - 2013-10-26 21:42 -0500
    Re: Function for the path of the script? Gary Herron <gary.herron@islandtraining.com> - 2013-10-26 19:52 -0700
    Re: Function for the path of the script? Chris Angelico <rosuav@gmail.com> - 2013-10-27 14:01 +1100
      Re: Function for the path of the script? Grant Edwards <invalid@invalid.invalid> - 2013-10-28 14:49 +0000
        Re: Function for the path of the script? Chris Angelico <rosuav@gmail.com> - 2013-10-29 01:59 +1100
        Re: Function for the path of the script? rurpy@yahoo.com - 2013-10-28 21:00 -0700
          Re: Function for the path of the script? Steven D'Aprano <steve@pearwood.info> - 2013-10-29 05:27 +0000
            Re: Function for the path of the script? Grant Edwards <invalid@invalid.invalid> - 2013-10-29 14:31 +0000
    Re: Function for the path of the script? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-27 03:42 +0000
    Re: Function for the path of the script? Ben Finney <ben+python@benfinney.id.au> - 2013-10-27 15:10 +1100
      Re: Function for the path of the script? Roy Smith <roy@panix.com> - 2013-10-27 09:38 -0400
      Re: Function for the path of the script? Grant Edwards <invalid@invalid.invalid> - 2013-10-28 14:58 +0000
        Re: Function for the path of the script? Ben Finney <ben+python@benfinney.id.au> - 2013-10-29 09:50 +1100
          Re: Function for the path of the script? Grant Edwards <invalid@invalid.invalid> - 2013-10-29 14:26 +0000
    How to find where data files are installed for my Python program (was: Function for the path of the script?) Ben Finney <ben+python@benfinney.id.au> - 2013-10-27 15:28 +1100
      Re: How to find where data files are installed for my Python program (was: Function for the path of the script?) rurpy@yahoo.com - 2013-10-26 22:18 -0700
      Re: How to find where data files are installed for my Python program Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-10-27 20:58 +1300
        Re: How to find where data files are installed for my Python program Ian Kelly <ian.g.kelly@gmail.com> - 2013-10-27 09:50 -0600
        Re: How to find where data files are installed for my Python program Ben Finney <ben+python@benfinney.id.au> - 2013-10-28 10:13 +1100
        Re: How to find where data files are installed for my Python program Ben Finney <ben+python@benfinney.id.au> - 2013-10-28 10:31 +1100
        Re: How to find where data files are installed for my Python program Chris Angelico <rosuav@gmail.com> - 2013-10-28 10:58 +1100
        Re: How to find where data files are installed for my Python program Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-28 00:34 +0000
        Re: How to find where data files are installed for my Python program Chris Angelico <rosuav@gmail.com> - 2013-10-28 17:51 +1100
          Re: How to find where data files are installed for my Python program Roy Smith <roy@panix.com> - 2013-10-28 08:51 -0400
            Re: How to find where data files are installed for my Python program Chris Angelico <rosuav@gmail.com> - 2013-10-29 00:06 +1100
    Re: How to find where data files are installed for my Python program (was: Function for the path of the script?) Chris Angelico <rosuav@gmail.com> - 2013-10-27 16:49 +1100
    Re: Function for the path of the script? Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-28 15:57 -0700
    Re: Function for the path of the script? Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-28 22:56 -0700

Page 2 of 2 — ← Prev page 1 [2]


#57774 — Re: How to find where data files are installed for my Python program

FromBen Finney <ben+python@benfinney.id.au>
Date2013-10-28 10:31 +1100
SubjectRe: How to find where data files are installed for my Python program
Message-ID<mailman.1670.1382916905.18130.python-list@python.org>
In reply to#57729
Ian Kelly <ian.g.kelly@gmail.com> writes:

> I don't see why Python files should be treated any differently than
> other non-binary executables, e.g. shell scripts.

It is an unfortunate artefact of Unix history that “binary” has an
established connotation of “executable”, encompassing even executable
text files.

So the separation I'm drawing attention to in the FHS has nothing to do
with whether the files are text files, and everything to do with whether
they're executable programs and code libraries.

According to the FHS (with which, of course, not every operating system
is bound to conform), executable program libraries belong in an entirely
separate location from non-executable data files.

This thread is about how to best use Python's standard tools to support
that separation on systems following the FHS.

-- 
 \           “If [a technology company] has confidence in their future |
  `\      ability to innovate, the importance they place on protecting |
_o__)     their past innovations really should decline.” —Gary Barnett |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#57776 — Re: How to find where data files are installed for my Python program

FromChris Angelico <rosuav@gmail.com>
Date2013-10-28 10:58 +1100
SubjectRe: How to find where data files are installed for my Python program
Message-ID<mailman.1672.1382918324.18130.python-list@python.org>
In reply to#57729
On Mon, Oct 28, 2013 at 10:31 AM, Ben Finney <ben+python@benfinney.id.au> wrote:
> It is an unfortunate artefact of Unix history that “binary” has an
> established connotation of “executable”, encompassing even executable
> text files.

That's a lot broader than Unix - people talk about "binaries" meaning
executables in Windows and OS/2 too. Unix is, if anything, _less_
inclined that way - the executable segment is called "text", which
always struck me as a bit odd.

> So the separation I'm drawing attention to in the FHS has nothing to do
> with whether the files are text files, and everything to do with whether
> they're executable programs and code libraries.

Yup. Unix does a fairly good job of blurring the line between
"executables that can be loaded and jumped to" and "scripts that get
loaded by an interpreter". I actually have a few scripts that take
several levels of interpreter, something like:

foo.pike
#!/usr/local/bin/pike

bar.pike:
#!/.../foo.pike --parameter

fum.pike:
#!/.../bar.pike --otherparameter

Unix will happily execute ./fum.pike as "/usr/local/bin/pike
/.../foo.pike --parameter /.../bar.pike --otherparameter ./fum.pike".
There's a limit on the number of interpreters (to prevent loops), but
I haven't hit it :)

There is one important place, though, where scripts are called data
files, and that's licensing. The GPL, for instance, does NOT cover
your scripts, even if it covers the interpreter, because *to the
language interpreter*, your scripts are just data files. But that's
more of a legal distinction than a filesystem hierarchical one.

ChrisA

[toc] | [prev] | [next] | [standalone]


#57777 — Re: How to find where data files are installed for my Python program

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-10-28 00:34 +0000
SubjectRe: How to find where data files are installed for my Python program
Message-ID<mailman.1673.1382920476.18130.python-list@python.org>
In reply to#57729
On 27/10/2013 23:58, Chris Angelico wrote:
> On Mon, Oct 28, 2013 at 10:31 AM, Ben Finney <ben+python@benfinney.id.au> wrote:
>> It is an unfortunate artefact of Unix history that “binary” has an
>> established connotation of “executable”, encompassing even executable
>> text files.
>
> That's a lot broader than Unix - people talk about "binaries" meaning
> executables in Windows and OS/2 too. Unix is, if anything, _less_
> inclined that way - the executable segment is called "text", which
> always struck me as a bit odd.
>
>> So the separation I'm drawing attention to in the FHS has nothing to do
>> with whether the files are text files, and everything to do with whether
>> they're executable programs and code libraries.
>
> Yup. Unix does a fairly good job of blurring the line between
> "executables that can be loaded and jumped to" and "scripts that get
> loaded by an interpreter". I actually have a few scripts that take
> several levels of interpreter, something like:
>
> foo.pike
> #!/usr/local/bin/pike
>
> bar.pike:
> #!/.../foo.pike --parameter
>
> fum.pike:
> #!/.../bar.pike --otherparameter
>
> Unix will happily execute ./fum.pike as "/usr/local/bin/pike
> /.../foo.pike --parameter /.../bar.pike --otherparameter ./fum.pike".
> There's a limit on the number of interpreters (to prevent loops), but
> I haven't hit it :)
>
> There is one important place, though, where scripts are called data
> files, and that's licensing. The GPL, for instance, does NOT cover
> your scripts, even if it covers the interpreter, because *to the
> language interpreter*, your scripts are just data files. But that's
> more of a legal distinction than a filesystem hierarchical one.
>
> ChrisA
>

Quoting from another thread

"What is the difference between "script" code (like Javascript and 
visual) made for the screen (where such magic values are utilized) and 
compiled source (made for the machine)?"

This obviously impacts on the discussion above, so how does Unix, 
Windows and other operating systems distinguish these with respect to 
binary, executable, code library or whatever?

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#57792 — Re: How to find where data files are installed for my Python program

FromChris Angelico <rosuav@gmail.com>
Date2013-10-28 17:51 +1100
SubjectRe: How to find where data files are installed for my Python program
Message-ID<mailman.1678.1382943105.18130.python-list@python.org>
In reply to#57729
On Mon, Oct 28, 2013 at 11:34 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> "What is the difference between "script" code (like Javascript and visual)
> made for the screen (where such magic values are utilized) and compiled
> source (made for the machine)?"
>
> This obviously impacts on the discussion above, so how does Unix, Windows
> and other operating systems distinguish these with respect to binary,
> executable, code library or whatever?

Suppose you pull up a shell - for argument's sake, let's say it's
Debian GNU/Linux and you're running bash. You get a prompt that ends
with a dollar sign, and you type "ls". What's going to get executed?

* An alias? You might have an internal function defined in your
.bashrc, or maybe a wrapper that adds parameters to your command.

* A bash internal command? The shell might directly interpret what you
specified. (I don't think ls is like that, but time is, on my
systems.)

* An external binary? On my systems, /bin/ls is an executable binary,
compiled and ready to run.

* A script? Another alternative to the shell alias, you could have
/usr/local/bin/ls that does something different, then maybe drops
through to /bin/ls. If it starts with "#!/usr/bin/python", it'll get
dropped through to Python for execution.

Chances are you wouldn't know the difference, as a human executing the
commands. And you shouldn't need to care, except in really weird
circumstances (maybe you broke your Python install and need to type
"/bin/ls" to figure out what's going on).

Most programs, trying to execute code, won't care about the difference
between binaries and scripts, though of course exec*() won't parse
bash aliases or internals. But if you need to distinguish for whatever
reason, the easiest way is to look at the magic numbers, which can be
done with the 'file' command:

rosuav@sikorsky:~$ file `which ls`
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV),
dynamically linked (uses shared libs), for GNU/Linux 2.6.26,
BuildID[sha1]=0x55f1e005df252708d4c456dcc2c7dccea1006553, stripped

rosuav@sikorsky:~$ file `which zcat`
/bin/zcat: Bourne-Again shell script, ASCII text executable

Executables happily together, regardless of type.

ChrisA

[toc] | [prev] | [next] | [standalone]


#57817 — Re: How to find where data files are installed for my Python program

FromRoy Smith <roy@panix.com>
Date2013-10-28 08:51 -0400
SubjectRe: How to find where data files are installed for my Python program
Message-ID<roy-A1F547.08515328102013@news.panix.com>
In reply to#57792
In article <mailman.1678.1382943105.18130.python-list@python.org>,
 Chris Angelico <rosuav@gmail.com> wrote:

> If it starts with "#!/usr/bin/python", it'll get
> dropped through to Python for execution.

Even better (for most purposes), use "#!/usr/bin/env python".  What that 
does is (slight handwave here) search your PATH to find the same version 
of Python you would get if you typed "python" at a shell prompt.

If you've only got a single version of python installed, it doesn't 
matter.  But it'll matter a lot if you have multiple versions (or even 
multiple installations of the same version, but with different sets of 
installed modules).

[toc] | [prev] | [next] | [standalone]


#57819 — Re: How to find where data files are installed for my Python program

FromChris Angelico <rosuav@gmail.com>
Date2013-10-29 00:06 +1100
SubjectRe: How to find where data files are installed for my Python program
Message-ID<mailman.1699.1382965577.18130.python-list@python.org>
In reply to#57817
On Mon, Oct 28, 2013 at 11:51 PM, Roy Smith <roy@panix.com> wrote:
> In article <mailman.1678.1382943105.18130.python-list@python.org>,
>  Chris Angelico <rosuav@gmail.com> wrote:
>
>> If it starts with "#!/usr/bin/python", it'll get
>> dropped through to Python for execution.
>
> Even better (for most purposes), use "#!/usr/bin/env python".  What that
> does is (slight handwave here) search your PATH to find the same version
> of Python you would get if you typed "python" at a shell prompt.

Yeah, I'm aware of that... but I dodged a bit of complexity by
hard-coding the path :) The shebang you quote drops it through to env,
which waves its hands vigorously and says "Abracadabra", before
dropping it through to the Python that you forgot you ran 'make
install' instead of 'make altinstall' on. :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#57723 — Re: How to find where data files are installed for my Python program (was: Function for the path of the script?)

FromChris Angelico <rosuav@gmail.com>
Date2013-10-27 16:49 +1100
SubjectRe: How to find where data files are installed for my Python program (was: Function for the path of the script?)
Message-ID<mailman.1639.1382852964.18130.python-list@python.org>
In reply to#57704
On Sun, Oct 27, 2013 at 3:28 PM, Ben Finney <ben+python@benfinney.id.au> wrote:
> Chris Angelico <rosuav@gmail.com> writes:
>
>> It's very common to want to know what directory you're in - it's a
>> good way to find data files.
>
> That's a naive way to do it (though it's often good enough, for a
> program only used on one system).

I never said it was right for a properly-installed program, just that
it's common - which it definitely is, and this not a Python thing,
it's across all languages. As you say, often good enough for simple
things.

Your main questions definitely still stand though, and I don't know
any answer to them.

ChrisA

[toc] | [prev] | [next] | [standalone]


#57852

FromPeter Cacioppi <peter.cacioppi@gmail.com>
Date2013-10-28 15:57 -0700
Message-ID<afa61d74-8300-47a9-8582-6063c3aa3ec7@googlegroups.com>
In reply to#57704
Ben Finney asked

"What workflow requires you to know the filename of the module, within
the module? "

So what I have is a project utility script in my scripts directory. I have a distinct file structure that holds my .py source, my test.py unit tests, and the file based data associated with the unit tests. 

Each test.py is in a dedicated directory.

My utility script can be easily tweaked to do a variety of useful things, one of which is leave the interactive session with a variable that points to a testing directory whose unit test failed. It relies on each test.py having a same named function that knows its directory.

It sounds like there is a more pythonic way to do what I am doing, but also that I am not completely out to lunch. 

Sounds about par for my python code at this point. As my code used to be almost exclusively out to lunch, I think I am improving (thanks guys!).

[toc] | [prev] | [next] | [standalone]


#57885

FromPeter Cacioppi <peter.cacioppi@gmail.com>
Date2013-10-28 22:56 -0700
Message-ID<f8650797-ae5d-4d3a-9637-d1c8dd5a0e51@googlegroups.com>
In reply to#57704
Steven said

"Isn't freedom of choice wonderful?"

Didn't somebody once say "we're all adults here". I forget who said that. Eddard Stark? The guy always did keep his head in a crisis.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web