my parser.py isn't imported

T

Torsten Bronger

Hallöchen!

I have a module parser.py in the same directory as the main module.
In the main module, I import "parser". On Linux, this works as
expected, however on Windows, it imports the stdlib parser module.
sys.path[0] points to the directory of my parser.py in both cases.
What went wrong here?

Tschö,
Torsten.
 
J

John Machin

Hallöchen!

I have a module parser.py in the same directory as the main module.
In the main module, I import "parser". On Linux, this works as
expected, however on Windows, it imports the stdlib parser module.
sys.path[0] points to the directory of my parser.py in both cases.
What went wrong here?

Ask a literal question, get a literal answer :)

1. Failure to consider that problems can happen [and therefore will
happen [1]] when there are multiple entities with the same name.

2. Failure to RTFabulousM:
"""
Details of the module searching and loading process are implementation
and platform specific. It generally involves searching for a ``built-
in'' module with the given name and then searching a list of locations
given as sys.path.
"""

3. Failure to attempt enlightenment by using the -v option:
"""
import parser # builtin
"""
(on Windows)

Suggestion: Avoid the confusion. Rename your module. If it is actually
intended as a replacement for the builtin one, then you can do:
import replacementparser as parser

[1] Codex Murphius, circa 5000 BCE

HTH,
John
 
T

Torsten Bronger

Hallöchen!

John said:
I have a module parser.py in the same directory as the main
module. In the main module, I import "parser". On Linux, this
works as expected, however on Windows, it imports the stdlib
parser module. sys.path[0] points to the directory of my
parser.py in both cases. What went wrong here?

Ask a literal question, get a literal answer :)

1. Failure to consider that problems can happen [and therefore
will happen [1]] when there are multiple entities with the same
name.

So I'd like to know a means to tell *explicitly* what I want to
import. Maybe I could use the imp module but that's ugly. I mean,
there are hundreds of modules on my harddisk, so trying to avoid
nameclashs should not be the solution, and given ...
2. Failure to RTFabulousM:
"""
Details of the module searching and loading process are
implementation and platform specific. It generally involves
searching for a ``built- in'' module with the given name and then
searching a list of locations given as sys.path.
"""

.... the whole systems seems largely arbitrary, which I don't
believe. ;-)

"parser" is built-in on Windows but not on Linux. What's the list
of modules that *may* be built-in on some platform or
implementation? Or should I avoid the whole standard lib for names
of my modules? Or even everything which is shipped e.g. with
Enthought Python? I find this quite irritating.

Tschö,
Torsten.
 
L

Lawrence D'Oliveiro

So I'd like to know a means to tell *explicitly* what I want to
import. Maybe I could use the imp module but that's ugly.

That seems to be the standard Python-provided way to explicitly import the
file you want from the place you want.
I mean, there are hundreds of modules on my harddisk, so trying to avoid
nameclashs should not be the solution...

That is what namespace qualifiers are for, e.g.

import my_custom_stuff.parser

versus

import parser
... the whole systems seems largely arbitrary, which I don't
believe. ;-)

The documentation <http://docs.python.org/lib/module-sys.html> says quite
clearly that the search order is controlled by sys.path. So why not
manipulate that to get the effect you want?
"parser" is built-in on Windows but not on Linux.

It is standard on all platforms
<http://docs.python.org/lib/module-parser.html>.
 
T

Torsten Bronger

Hallöchen!

John said:
Torsten said:
I have a module parser.py in the same directory as the main
module. In the main module, I import "parser". On Linux, this
works as expected, however on Windows, it imports the stdlib
parser module. sys.path[0] points to the directory of my
parser.py in both cases. What went wrong here?

[...]

2. Failure to RTFabulousM:
"""
Details of the module searching and loading process are implementation
and platform specific. It generally involves searching for a ``built-
in'' module with the given name and then searching a list of locations
given as sys.path.
"""

Okay, I did the following to avoid this:

import sys, os.path
modulepath = os.path.abspath(os.path.dirname(sys.argv[0]))
def import_local_module(name):
"""Load a module from the local modules directory.

Loading e.g. a local "parser" module is difficult because on
Windows, the stdlib parser module is a built-in and thus loaded
with higher priority. With
http://www.python.org/dev/peps/pep-0328/ it may become simpler.

:parameters:
- `name`: name of the module, as it would be given to
``import``.

:Return:
the module object or ``None`` if none was found

:rtype:
module
"""
import imp
try:
return sys.modules[name]
except KeyError:
pass
fp, pathname, description = imp.find_module(name, [modulepath])
try:
return imp.load_module(name, fp, pathname, description)
finally:
# Since we may exit via an exception, close fp explicitly.
if fp:
fp.close()

Tschö,
Torsten.
 
J

John Machin

That seems to be the standard Python-provided way to explicitly import the
file you want from the place you want.


That is what namespace qualifiers are for, e.g.

import my_custom_stuff.parser

versus

import parser



The documentation <http://docs.python.org/lib/module-sys.html> says quite
clearly that the search order is controlled by sys.path. So why not
manipulate that to get the effect you want?

Short answer: it doesn't work. The documentation that I quoted
previously says that if a module is is built in, it will be selected,
*before* sys.path is examined. The OP's original message stated quite
clearly that the directory containing his own parser.py is at the
front of sys.path.

"standard" != "builtin"
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top