471,310 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

my parser.py isn't imported

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.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber.org
(See http://ime.webhop.org for ICQ, MSN, etc.)
Aug 26 '07 #1
1 1460
Hallöchen!

John Machin writes:
Torsten Bronger wrote:
>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.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber.org
(See http://ime.webhop.org for ICQ, MSN, etc.)
Aug 27 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Paulo Pinto | last post: by
3 posts views Thread by Himanshu Garg | last post: by
3 posts views Thread by Markus Dehmann | last post: by
5 posts views Thread by qqcq6s59 | last post: by
5 posts views Thread by Bill Cunningham | last post: by
2 posts views Thread by Alex | last post: by
28 posts views Thread by Marc Gravell | last post: by
reply views Thread by rosydwin | last post: by

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.