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


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

The original command python line

Started byDamjan Georgievski <gdamjan@gmail.com>
First post2012-03-04 06:38 +0100
Last post2012-03-04 02:19 -0500
Articles 7 — 4 participants

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


Contents

  The original command python line Damjan Georgievski <gdamjan@gmail.com> - 2012-03-04 06:38 +0100
    Re: The original python command line Damjan Georgievski <gdamjan@gmail.com> - 2012-03-04 06:52 +0100
    Re: The original command python line Chris Rebert <clp2@rebertia.com> - 2012-03-03 21:48 -0800
      Re: The original command python line Grant Edwards <invalid@invalid.invalid> - 2012-03-04 14:34 +0000
    Re: The original command python line Chris Rebert <clp2@rebertia.com> - 2012-03-03 21:57 -0800
      Re: The original command python line Damjan Georgievski <gdamjan@gmail.com> - 2012-03-04 07:20 +0100
        Re: The original command python line Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-03-04 02:19 -0500

#21178 — The original command python line

FromDamjan Georgievski <gdamjan@gmail.com>
Date2012-03-04 06:38 +0100
SubjectThe original command python line
Message-ID<4obb29-os3.ln1@archaeopteryx.softver.org.mk>
How can I get the *really* original command line that started my python
interpreter?

Werkzeug has a WSGI server which reloads itself when files are changed
on disk. It uses `args = [sys.executable] + sys.argv` to kind of
recreate the command line, and the uses subprocess.call to run that
command line.

BUT that's problematic as, when you run::

	python -m mypackage --config

sys.argv printed in mypackage/__main__.py will be::

	['/full/path/to/mypackage/__main__.py', '--config']

so you get::

	python /full/path/to/mypackage/__main__.py --config

instead of::

	python -m mypackage --config


the difference in the 2 cases is what the current package is, and
whether you can use relative imports.


-- 
damjan

[toc] | [next] | [standalone]


#21179 — Re: The original python command line

FromDamjan Georgievski <gdamjan@gmail.com>
Date2012-03-04 06:52 +0100
SubjectRe: The original python command line
Message-ID<qhcb29-hh4.ln1@archaeopteryx.softver.org.mk>
In reply to#21178
> How can I get the *really* original command line that started my python
> interpreter?
> 
> Werkzeug has a WSGI server which reloads itself when files are changed
> on disk. It uses `args = [sys.executable] + sys.argv` to kind of
> recreate the command line, and the uses subprocess.call to run that
> command line.
> 
> BUT that's problematic as, when you run::
> 
> 	python -m mypackage --config
> 
> sys.argv printed in mypackage/__main__.py will be::
> 
> 	['/full/path/to/mypackage/__main__.py', '--config']
> 
> so you get::
> 
> 	python /full/path/to/mypackage/__main__.py --config
> 
> instead of::
> 
> 	python -m mypackage --config
> 
> 
> the difference in the 2 cases is what the current package is, and
> whether you can use relative imports.

BTW, the same thing happens in Python 3.2.2. To reproduce::

mkdir /tmp/X
cd /tmp/X

mkdir mypackage
touch  mypackage/__init__.py mypackage/dummy.py

cat <<EOF > mypackage/__main__.py
from __future__ import print_function, absolute_import
import os, sys, subprocess

def rerun():
    new_environ = os.environ.copy()
    new_environ['TEST_CHILD'] = 'true'
    args = [sys.executable] + sys.argv
    subprocess.call(args, env=new_environ)

if os.environ.get('TEST_CHILD') != 'true':
    Role = 'Parent'
    rerun()
else:
    Role = 'Child'

try:
    from . import dummy
except:
    print('Exception in %s' % Role)
else:
    print('Success in %s' % Role)
EOF



Both::
	
	python2 -m mypackage

or::

	python3 -m mypackage


will print::

	Exception in Child
	Success in Parent

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


#21180

FromChris Rebert <clp2@rebertia.com>
Date2012-03-03 21:48 -0800
Message-ID<mailman.369.1330840141.3037.python-list@python.org>
In reply to#21178
On Sat, Mar 3, 2012 at 9:38 PM, Damjan Georgievski <gdamjan@gmail.com> wrote:
> How can I get the *really* original command line that started my python
> interpreter?
>
> Werkzeug has a WSGI server which reloads itself when files are changed
> on disk. It uses `args = [sys.executable] + sys.argv` to kind of
> recreate the command line, and the uses subprocess.call to run that
> command line.
>
> BUT that's problematic as, when you run::
>
>        python -m mypackage --config
>
> sys.argv printed in mypackage/__main__.py will be::
>
>        ['/full/path/to/mypackage/__main__.py', '--config']
>
> so you get::
>
>        python /full/path/to/mypackage/__main__.py --config
>
> instead of::
>
>        python -m mypackage --config
>
>
> the difference in the 2 cases is what the current package is, and
> whether you can use relative imports.

On Linux, you can read from:
    /proc/<PID here>/cmdline
to get the null-delimited "command line".

Sidenote: Consensus generally seems to be that relative imports are a bad idea.

Cheers,
Chris

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


#21200

FromGrant Edwards <invalid@invalid.invalid>
Date2012-03-04 14:34 +0000
Message-ID<jivui3$j1r$1@reader1.panix.com>
In reply to#21180
On 2012-03-04, Chris Rebert <clp2@rebertia.com> wrote:
> On Sat, Mar 3, 2012 at 9:38 PM, Damjan Georgievski <gdamjan@gmail.com> wrote:
>> How can I get the *really* original command line that started my python
>> interpreter?

> On Linux, you can read from:
>     /proc/<PID here>/cmdline
> to get the null-delimited "command line".

And if what you want is your own command line, you can replace <PID
here> with self:

/proc/self/cmdline

-- 
Grant

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


#21181

FromChris Rebert <clp2@rebertia.com>
Date2012-03-03 21:57 -0800
Message-ID<mailman.370.1330840664.3037.python-list@python.org>
In reply to#21178
On Sat, Mar 3, 2012 at 9:48 PM, Chris Rebert <clp2@rebertia.com> wrote:
> On Sat, Mar 3, 2012 at 9:38 PM, Damjan Georgievski <gdamjan@gmail.com> wrote:
>> How can I get the *really* original command line that started my python
>> interpreter?
<snip>
> On Linux, you can read from:
>    /proc/<PID here>/cmdline
> to get the null-delimited "command line".

After some further searching:
psutil offers `Process.cmdline` cross-platform;
see http://code.google.com/p/psutil/wiki/Documentation

Cheers,
Chris

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


#21182

FromDamjan Georgievski <gdamjan@gmail.com>
Date2012-03-04 07:20 +0100
Message-ID<c6eb29-ko5.ln1@archaeopteryx.softver.org.mk>
In reply to#21181
>>> How can I get the *really* original command line that started my python
>>> interpreter?
> <snip>
>> On Linux, you can read from:
>>    /proc/<PID here>/cmdline
>> to get the null-delimited "command line".
> 
> After some further searching:
> psutil offers `Process.cmdline` cross-platform;
> see http://code.google.com/p/psutil/wiki/Documentation

Indeed, changing for

	args = psutil.Process(os.getpid()).cmdline

in the above example does solve the problem, at least in Linux.


> Sidenote: Consensus generally seems to be that relative imports are a
bad idea.

How come?
I'm using explicit relative imports, I thought they were the new thing?


-- 
damjan

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


#21183

FromDevin Jeanpierre <jeanpierreda@gmail.com>
Date2012-03-04 02:19 -0500
Message-ID<mailman.371.1330845608.3037.python-list@python.org>
In reply to#21182
On Sun, Mar 4, 2012 at 1:20 AM, Damjan Georgievski <gdamjan@gmail.com> wrote:
> How come?
> I'm using explicit relative imports, I thought they were the new thing?

Explicit relative imports are fine. Implicit relative imports can
create multiple module objects for the same source file, which breaks
things like exception handling (because exception types are unequal if
they're from different classes, even if the different classes come
from two executions of the same source code).

-- Devin

[toc] | [prev] | [standalone]


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


csiph-web