473,701 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PEP 338: Executing modules inside packages with '-m'

Python 2.4's -m command line switch only works for modules directly on sys.path.
Trying to use it with modules inside packages will fail with a "Module not
found" error. This PEP aims to fix that for Python 2.5.

Previously, posting of a draft version of the PEP to python-dev and python-list
didn't actually generate any responses. I'm not sure if that's an indication
that people don't see the restriction to top-level modules as a problem (and
hence think the PEP is unecessary), or think the extension to handle packages is
obvious (and hence see no need to comment).

Or, it could just be a sign that Python 2.4 hasn't been out long enough for
anyone to care what I'm yabbering on about :)

Anyway, all comments are appreciated (even a simple "Sounds good to me").

Cheers,
Nick.

*************** *************** *************** *************** ***********
PEP: 338
Title: Executing modules inside packages with '-m'
Version: $Revision: 1.2 $
Last-Modified: $Date: 2004/12/11 20:31:10 $
Author: Nick Coghlan <nc******@email .com>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 16-Oct-2004
Python-Version: 2.5
Post-History: 8-Nov-2004
Abstract
========

This PEP defines semantics for executing modules inside packages as
scripts with the ``-m`` command line switch.

The proposed semantics are that the containing package be imported
prior to execution of the script.
Rationale
=========

Python 2.4 adds the command line switch ``-m`` to allow modules to be
located using the Python module namespace for execution as scripts.
The motivating examples were standard library modules such as ``pdb``
and ``profile``.

A number of users and developers have requested extension of the
feature to also support running modules located inside packages. One
example provided is pychecker's ``pychecker.che cker`` module. This
capability was left out of the Python 2.4 implementation because the
appropriate semantics were not entirely clear.

The opinion on python-dev was that it was better to postpone the
extension to Python 2.5, and go through the PEP process to help make
sure we got it right.
Scope of this proposal
=============== ===========

In Python 2.4, a module located using ``-m`` is executed just as if
its filename had been provided on the command line. The goal of this
PEP is to get as close as possible to making that statement also hold
true for modules inside packages.

Prior discussions suggest it should be noted that this PEP is **not**
about any of the following:

- changing the idiom for making Python modules also useful as scripts
(see PEP 299 [1]_).

- lifting the restriction of ``-m`` to modules of type PY_SOURCE or
PY_COMPILED (i.e. ``.py``, ``.pyc``, ``.pyo``, ``.pyw``).

- addressing the problem of ``-m`` not understanding zip imports or
Python's sys.metapath.

The issues listed above are considered orthogonal to the specific
feature addressed by this PEP.
Current Behaviour
=============== ==

Before describing the new semantics, it's worth covering the existing
semantics for Python 2.4 (as they are currently defined only by the
source code).

When ``-m`` is used on the command line, it immediately terminates the
option list (like ``-c``). The argument is interpreted as the name of
a top-level Python module (i.e. one which can be found on
``sys.path``).

If the module is found, and is of type ``PY_SOURCE`` or
``PY_COMPILED`` , then the command line is effectively reinterpreted
from ``python <options> -m <module> <args>`` to ``python <options>
<filename> <args>``. This includes setting ``sys.argv[0]`` correctly
(some scripts rely on this - Python's own ``regrtest.py`` is one
example).

If the module is not found, or is not of the correct type, an error
is printed.
Proposed Semantics
=============== ===

The semantics proposed are fairly simple: if ``-m`` is used to execute
a module inside a package as a script, then the containing package is
imported before executing the module in accordance with the semantics
for a top-level module.

This is necessary due to the way Python's import machinery locates
modules inside packages. A package may modify its own __path__
variable during initialisation. In addition, paths may affected by
``*.pth`` files. Accordingly, the only way for Python to reliably
locate the module is by importing the containing package and
inspecting its __path__ variable.

Note that the package is *not* imported into the ``__main__`` module's
namespace. The effects of these semantics that will be visible to the
executed module are:

- the containing package will be in sys.modules

- any external effects of the package initialisation (e.g. installed
import hooks, loggers, atexit handlers, etc.)
Reference Implementation
=============== =========

A reference implementation is available on SourceForge [2]_. In this
implementation, if the ``-m`` switch fails to locate the requested
module at the top level, it effectively reinterprets the command from
``python -m <script>`` to ``python -m execmodule <script>``. (There
is one caveat: when reinterpreted in this way, ``sys.argv[0]`` may not
actually contain the filename of ``execmodule``. This only affects
``execmodule`` itself, not the requested module).

``execmodule`` is a proposed standard library module that contains a
single function (also called ``execmodule``) . When invoked as a
script, this module finds and executes the module supplied as the
first argument. It adjusts ``sys.argv`` by deleting ``sys.argv[0]``
and replacing the new ``sys.argv[0]`` with the module's filename
instead of its Python name.

The function ``execmodule`` is like ``execfile``, but uses the Python
module namespace to locate the script instead of the filesystem. It
has an additional optional argument ``set_argv0`` which causes the
filename of the located module to be written to ``sys.argv[0]`` before
the module is executed.

A hybrid C/Python implementation is used as the Python module is much
more flexible and extensible than the equivalent C code would be. It
also allows the ``execmodule`` function to be made available. Scripts
which execute other scripts (e.g. ``profile``, ``pdb``) have the
option to use this function to provide ``-m`` style support for
identifying the script to be executed.

The Python code for ``execmodule`` has also been posted as a
cookbook recipe for Python 2.4 [3]_.
Open Issues
===========

- choosing a name for the standard library module containing
``execmodule``. The reference implementation uses ``execmodule``.
An alternative name proposed on python-dev is ``runpy``.
Alternatives
============

The main alternative implementation considered ignored packages'
__path__ variables, and looked only in the main package directory. A
Python script with this behaviour can be found in the discussion of
the ``execmodule`` cookbook recipe [3]_.

This approach was not used as it does not meet the main goal of the
``-m`` switch -- to allow the full Python namespace to be used to
locate modules for execution.
References
==========

... [1] Special __main__() function in modules
(http://www.python.org/peps/pep-0299.html)

... [2] Native ``-m`` execmodule support

(http://sourceforge.net/tracker/?func...70&atid=305470 )

... [3] execmodule Python Cookbook Recipe
(http://aspn.activestate.com/ASPN/Coo.../Recipe/307772)
Copyright
=========

This document has been placed in the public domain.

...
Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
End:
--
Nick Coghlan | nc******@email. com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #1
15 2603
A useful feature that is a logical extension of current '-m' behaviour.
(I'm actually surprised it was left out in the first place)

This will definitely allow me and other python programmers to package
our scripts better

Sounds Good to me. (-;

Thank you for the PEP

AK

Jul 18 '05 #2
al*********@yah oo.com wrote:
A useful feature that is a logical extension of current '-m' behaviour.
(I'm actually surprised it was left out in the first place)


That seems to be a common reaction :)

It was dropped for 2.4 because I wasn't sure exactly how it should work, and 2.4
was already in beta at the time. So I went with the cookbook recipe instead.

Cheers,
Nick.

--
Nick Coghlan | nc******@email. com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #3
al*********@yah oo.com wrote:
A useful feature that is a logical extension of current '-m' behaviour.
(I'm actually surprised it was left out in the first place)

This will definitely allow me and other python programmers to package
our scripts better

Sounds Good to me. (-;


/me too :)
Richard

Jul 18 '05 #4
al*********@yah oo.com wrote:
This will definitely allow me and other python programmers to package
our scripts better


puzzling.

I'd say that for a typical user,

$ python -m foo.bar arg

is a marginal improvement over

$ python -c "import foo.bar" arg

compared to

$ bar arg

</F>

Jul 18 '05 #5
Fredrik Lundh wrote:
I'd say that for a typical user,

$ python -m foo.bar arg

is a marginal improvement over

$ python -c "import foo.bar" arg
This doesn't work. Any code protected by "if __name__ == '__main__':" won't run
in this context (since 'foo.bar' is being imported as a module, not run as a
script).

Even 'python -c "from foo.bar import _main; _main()" arg' isn't quite right,
since sys.argv[0] will be wrong (it will say '-c', instead of giving the
module's filename). There's also the problem that there is no standard idiom for
_main() functions.
compared to

$ bar arg


This is true, but it has its own problems, mainly in the area of namespace
conflicts on the packaging side:

1. Namespace conflicts between different Python versions
2. Namespace conflicts between different Python packages
3. Namespace conflicts between Python packages and other programs
4. Additional overhead to create an installed module that is usable as a script
a. Add a shebang line for *nix style systems
b. Think about how to deal with the previous 3 points
c. Update the installer to copy the file to the right place with a good name
d. Realise you're screwed on Windows, since you can't control the file
associations and the script will always run with the default interpreter.

An extended -m, on the other hand deals with all those problems automatically:

python -m foo.bar arg # Default interpreter, foo's bar
python -m bar.bar arg # Default interpreter, bar's bar
python24 -m foo.bar arg # Force Python 2.4, foo's bar
python24 -m bar.bar arg # Force Python 2.4, bar's bar
bar arg # Unrelated application called bar

Points 1, 3 & 4 were the justification for adding the current version of -m to
Python 2.4 (obviously, point 2 didn't apply, since the current version doesn't
work for modules insides packages). Specifically, it makes it trivial to get
hold of the right version of pdb and profile for the interpreter you're working
with.

For usability, you can hide all of the above behind a menu item or desktop
shortcut. However, the major target of the feature is Python developers rather
than the end-users of applications built using Python.

Cheers,
Nick.

--
Nick Coghlan | nc******@email. com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #6
Nick Coghlan wrote:
$ python -c "import foo.bar" arg


This doesn't work. Any code protected by "if __name__ == '__main__':" won't run in this context
(since 'foo.bar' is being imported as a module, not run as a script).


I appreciate that you're taking the time to teach me about Python, but I can
assure you that it's not really needed.

as for the rest of your arguments, I have to assume that you were joking. (or
that you have no experience whatsoever of distribution of Python programs in
Unix and Windows environments).

</F>

Jul 18 '05 #7
In article <ma************ *************** ***********@pyt hon.org>,
"Fredrik Lundh" <fr*****@python ware.com> wrote:
Nick Coghlan wrote:
$ python -c "import foo.bar" arg
This doesn't work. Any code protected by "if __name__ == '__main__':" won't
run in this context
(since 'foo.bar' is being imported as a module, not run as a script).


I appreciate that you're taking the time to teach me about Python, but I can
assure you that it's not really needed.


Neither is the sarcasm.
as for the rest of your arguments, I have to assume that you were joking.
(or
that you have no experience whatsoever of distribution of Python programs in
Unix and Windows environments).


Whatever. You suggestion does not work in many cases. How about a
program that starts threads? Can't do that as a side effect of import.

Just
Jul 18 '05 #8
Just wrote:

as for the rest of your arguments, I have to assume that you were joking.
(or
that you have no experience whatsoever of distribution of Python programs in
Unix and Windows environments).


Whatever. You suggestion does not work in many cases. How about a
program that starts threads? Can't do that as a side effect of import.


my suggestion was to make sure that the user can type "bar arg" to start a
Python program called "bar" with the argument "arg". that's trivial, on all
major platforms, despite what Nick says -- and yes, you can start threads
from a program named "bar". try it.

</F>

Jul 18 '05 #9
In article <ma************ *************** ***********@pyt hon.org>,
"Fredrik Lundh" <fr*****@python ware.com> wrote:
Just wrote:

as for the rest of your arguments, I have to assume that you were joking.
(or
that you have no experience whatsoever of distribution of Python programs
in
Unix and Windows environments).


Whatever. You suggestion does not work in many cases. How about a
program that starts threads? Can't do that as a side effect of import.


my suggestion was to make sure that the user can type "bar arg" to start a
Python program called "bar" with the argument "arg". that's trivial, on all
major platforms, despite what Nick says -- and yes, you can start threads
from a program named "bar". try it.


This subthread was specifically about your

python -c "import foo.bar" arg

suggestion.

Just
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1956
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is inside a package, the invocation will fail with a "Module not found" error. This PEP is aimed at fixing that :) Cheers, Nick.
4
1753
by: Misto . | last post by:
Hi folks! Short: There is a way to dumplicate a module ? I tried copy.deepcopy(module) but hangs with an error (also with standard modules ).. The only solution that I have by now is creating two files and importing them.
11
3856
by: fortepianissimo | last post by:
Say I have the following package organization in a system I'm developing: A |----B |----C |----D I have a module, say 'foo', that both package D and B require. What is the best practice in terms of creating a 'common' package that hosts
4
4323
by: Fabian Braennstroem | last post by:
Hi, I am pretty new to python and will use it mainly in combination with scientific packages. I am running ubuntu breezy right now and see that some packages are out of date. Do you have any suggestion, how I can get/keep the latest python modules (e.g. scipy, numpy,...) on my ubuntu system? I.e. does there exist any script/program, which downloads and installs automatically the latest stable releases of selected modules? It would be...
1
2112
by: tyler | last post by:
I've written a small python extension but I'm having difficulty loading it at runtime. The source for my extension is a module which is a member of a package is organized as follows. test/setup.py test/myutils/__init__.py test/myutils/netmodule.c my setup.py file for building / installing looks like this
2
1101
by: dhable | last post by:
I think I'm still missing something in how python is handling packages and it's mixing me up. I have a package with three files (modules?) like so: OPS:\ __init__.py model.py search.py To hide more details of the package structure, I import model and
17
5406
by: mohan | last post by:
Hi Guys, I've been using the following IDE, "Pythonwin - Python IDE and GUI Framework for Windows. Copyright 1994-2001 Mark Hammond " With respect to my work, I had created my own modules (.py files) in drives and folders other than the python root. I know that if I need to
16
7241
by: ssecorp | last post by:
Is there a way to view all the modules I have available for import from within Python? Like writing in the interpreter: import.modules Also, is there anything like Cpan for Python?
1
1889
by: Juan | last post by:
Hi I am programming a little script that makes use of a module I developed before. The utils are inside the directory src of the directory utils, and the package is nutum.utils. The script is in the directory src inside the directory sysinfo, and the package is nutum.sysinfo. Well, if not clear, this is the list of files: ls -lR ~/workspace (imaginary output):
0
8733
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8649
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6569
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5904
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.