FAQ
I'm seeing highly surprising (and different!) behaviors of
PyImport_ImportModule on Linux and Windows when used in a program with
python embedding.

On Linux, when attempting to import a module xxx that's in the current
directory, I get

ImportError: No module named xxx

I can work around the problem by setting

PYTHONPATH=.

On Windows, I get:

'import site' failed; use -v for traceback

I can work around the problem by setting PYTHONPATH to point to the
python library directory:

set PYTHONPATH=c:\Python25\Lib

I was under the impression that both the current directory *and* the
python library directory were already, automatically, in sys.path, so
I'm really surprised to see this. Am I doing something wrong, or is
this simply the expected behavior (and if so, where is it documented)?

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Search Discussions

  • Aahz at Mar 24, 2007 at 3:44 pm
    In article <mailman.5359.1174435375.32031.python-list at python.org>,
    David Abrahams wrote:
    I was under the impression that both the current directory *and* the
    python library directory were already, automatically, in sys.path, so
    I'm really surprised to see this. Am I doing something wrong, or is
    this simply the expected behavior (and if so, where is it documented)?
    IIRC (without bother to test), there has been some change in the
    definition of "current directory" -- it used to be the actual current
    directory of os.getcwd(), but since changed to the startup directory.
    --
    Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/

    "Typing is cheap. Thinking is expensive." --Roy Smith
  • Alex Martelli at Mar 24, 2007 at 4:36 pm

    Aahz wrote:

    In article <mailman.5359.1174435375.32031.python-list at python.org>,
    David Abrahams wrote:
    I was under the impression that both the current directory *and* the
    python library directory were already, automatically, in sys.path, so
    I'm really surprised to see this. Am I doing something wrong, or is
    this simply the expected behavior (and if so, where is it documented)?
    IIRC (without bother to test), there has been some change in the
    definition of "current directory" -- it used to be the actual current
    directory of os.getcwd(), but since changed to the startup directory.
    In 2.3 and later, at least (sorry, no 2.2 and earlier around to check),
    site.py makes every directory along sys.path an absolute path at Python
    startup. This _should_ probably be documented at
    <http://docs.python.org/lib/module-site.html>, but it doesn't appear to
    be clearly stated there (the page only speaks of site's job of
    "appending site specific paths", and not of the other jobs it also
    performs, such as normalizing sys.path by turning all paths into
    absolute ones and removing duplicates).


    Alex
  • Ziga Seilnacht at Mar 24, 2007 at 8:20 pm

    David Abrahams wrote:
    I'm seeing highly surprising (and different!) behaviors of
    PyImport_ImportModule on Linux and Windows when used in a program with
    python embedding.

    On Linux, when attempting to import a module xxx that's in the current
    directory, I get

    ImportError: No module named xxx

    I can work around the problem by setting

    PYTHONPATH=.
    Python puts the current directory in sys.path only if it can't
    determine the directory of the main script. There was a bug on
    Windows that always added current directory to sys.path, but it
    was fixed in Python 2.5. This is documented in the library
    reference:

    http://docs.python.org/lib/module-sys.html#l2h-5149
    On Windows, I get:

    'import site' failed; use -v for traceback

    I can work around the problem by setting PYTHONPATH to point to the
    python library directory:

    set PYTHONPATH=c:\Python25\Lib
    This happens because Python calculates the initial import path by
    looking for an executable file named "python" along PATH. You can
    change this by calling Py_SetProgramName(filename) before calling
    Py_Initialize(). This is documented in API reference manual:

    http://docs.python.org/api/embedding.html

    That page also describes a few hooks that you can overwrite to
    modify the initial search path. They are described in more detail
    on this page:

    http://docs.python.org/api/initialization.html

    HTH,
    Ziga
  • David Abrahams at Oct 25, 2007 at 8:53 pm
    A non-text attachment was scrubbed...
    Name: import.c
    Type: text/x-csrc
    Size: 289 bytes
    Desc: not available
    Url : http://mail.python.org/pipermail/python-list/attachments/20071025/18df94c6/attachment.c
  • Graham Dumpleton at Oct 25, 2007 at 10:44 pm

    On Oct 26, 6:53 am, David Abrahams wrote:
    David Abrahams wrote:
    I'm seeing highly surprising (and different!) behaviors of
    PyImport_ImportModule on Linux and Windows when used in a program with
    python embedding.
    On Linux, ...
    <Linux working OK now>

    Unfortunately, nothing you have written below or on the pages you
    reference seems to help in Windows. Here's a simple program that
    demonstrates:

    import.c
    1KDownload

    'import site' failed; use -v for traceback
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    ImportError: __import__ not found
    I can work around the problem by setting PYTHONPATH to point to the
    python library directory:
    set PYTHONPATH=c:\Python25\Lib
    This happens because Python calculates the initial import path by
    looking for an executable file named "python" along PATH.
    C:\Python25, which contains python.exe, is in the PATH. If it's
    really looking for an executable named "python" along PATH, that can
    never succeed on Windows, since (I think) only files ending in .exe,
    .bat, and .cmd are executable there.
    You can
    change this by calling Py_SetProgramName(filename) before calling
    Py_Initialize(). This is documented in API reference manual:
    Uncomment that line in my program above and you'll see it makes
    absolutely no difference.
    http://docs.python.org/api/embedding.html
    That page also describes a few hooks that you can overwrite to
    modify the initial search path. They are described in more detail
    on this page:
    http://docs.python.org/api/initialization.html
    The only thing mentioned there that seems to have any effect at all is

    set PYTHONHOME=C:\Python25

    and even then, it only eliminates the first line of the error, which
    then reads:

    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    ImportError: __import__ not found

    My only known workaround is to set PYTHONPATH. This just doesn't seem
    right; isn't anyone doing embedding under Windows?
    The problem with not finding site.py on Windows will often come up
    when you have gone through a cycle of installing/removing different
    versions of Python and the registry entries get mucked. Also can be a
    problem where Python was not installed as administrator originally and
    now trying to run stuff as user different to what Python was installed
    as. A lot of the time problems go away by deinstalling Python and
    reinstalling as administrator. Also watch out for where you have some
    third party application which provides its own version of Python and
    that version or its DLL is being found first in your PATH.

    Graham

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 21, '07 at 12:02a
activeOct 25, '07 at 10:44p
posts6
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase