PyImport_ImportModule/embedding: surprising behaviors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Abrahams

    #1

    PyImport_ImportModule/embedding: surprising behaviors


    I'm seeing highly surprising (and different!) behaviors of
    PyImport_Import Module 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:\P ython25\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


  • Aahz

    #2
    Re: PyImport_Import Module/embedding: surprising behaviors

    In article <mailman.5359.1 174435375.32031 .python-list@python.org >,
    David Abrahams <dave@boost-consulting.comw rote:
    >
    >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@pythoncra ft.com) <* http://www.pythoncraft.com/

    "Typing is cheap. Thinking is expensive." --Roy Smith

    Comment

    • Alex Martelli

      #3
      Re: PyImport_Import Module/embedding: surprising behaviors

      Aahz <aahz@pythoncra ft.comwrote:
      In article <mailman.5359.1 174435375.32031 .python-list@python.org >,
      David Abrahams <dave@boost-consulting.comw rote:

      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

      Comment

      • Ziga Seilnacht

        #4
        Re: PyImport_Import Module/embedding: surprising behaviors

        David Abrahams wrote:
        I'm seeing highly surprising (and different!) behaviors of
        PyImport_Import Module 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:

        This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available. Unless explicitly noted oth...

        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:\P ython25\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_SetProgramNa me(filename) before calling
        Py_Initialize() . This is documented in API reference manual:



        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:



        HTH,
        Ziga


        Comment

        • David Abrahams

          #5
          [Windows embedding] sys.path not properly initialized (was:

          Comment

          Working...