problem(s) with import from parent dir: "from ../brave.py import sir_robin"

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

    problem(s) with import from parent dir: "from ../brave.py import sir_robin"

    Dear Black Knight,

    I have no quarrel with you sir Knight, but I must import your parents.


    SHORT VERSION:

    I tried three variants of "from ../brave.py import sir_robin", one
    works. I want to use it in a py-file to execute command-line-style and
    that does not work.

    Can someone please give me, not a bucket with the desert(s) on top, but
    just the thin chocolate?

    /per9000 (per nine thousand at gmail dot com)


    LONG VERSION:

    Since I do not want to use absoute paths I want to import a file from
    two folders up and then one down.

    This problem seems to be discussed in:

    I tried following it but I did not understand and/or try hard enough
    and/or my father smells of elderberrys.

    So...

    ....first I tried this: "from ../brave.py import sir_robin" (and
    variants)

    But all I got was syntax or import error(s).

    ----------

    Then I tried to fool python by walking the path up with os.chdir('..')
    in the line-by-line interpreter.
    This (surprisingly) worked just fine.

    C:\my\holy\grai l\that_old_brid ge>python[color=blue][color=green][color=darkred]
    >>> import os
    >>> os.chdir('../../py_scripts')
    >>> os.getcwd()[/color][/color][/color]
    'C:\\my\\holy\\ py_scripts'[color=blue][color=green][color=darkred]
    >>> from raw2nice_def import raw2nice
    >>> os.listdir('.')[/color][/color][/color]
    ['raw2nice_def.p y', 'raw2nice_def.p yc', 'temp'][color=blue][color=green][color=darkred]
    >>> raw2nice[/color][/color][/color]
    <function raw2nice at 0x00A28230>

    ----------

    When I tried putting this into a program to execute command-line-style:

    the interesting code:

    import os
    cwd = os.getcwd()
    os.chdir('../../py_scripts')
    print os.listdir('.')
    from raw2nice_def.py import raw2nice
    os.chdir(cwd)

    output:
    [color=blue]
    > "C:\another_bri dge\python\pyth on.exe" rawhtml2nicehtm l_template.py[/color]
    ['raw2nice_def.p y', 'raw2nice_def.p yc', 'temp']
    Traceback (most recent call last):
    File "C:my\holy\grai l\that_old_brid ge\rawhtml2nice html_template.p y",
    line 10, in ?
    from raw2nice_def.py import raw2nice
    ImportError: No module named raw2nice_def.py

    The worst part here is that os.listdir('.') returns ['raw2nice_def.p y',
    'raw2nice_def.p yc', 'temp'] - meaning (to me) that Python really should
    "feel" my nice file but somehow still does not see it.

    To me this also means that python::command _line does not concurr with
    python::line_by _line(!?!)

    Can someone please give me, not a bucket with the desert(s) on top, but
    just the thin chocolate?

    /per9000 (per nine thousand at gmail dot com)

  • Kent Johnson

    #2
    Re: problem(s) with import from parent dir: &quot;from ../brave.py importsir_robin &quot;

    per9000 wrote:[color=blue][color=green][color=darkred]
    >>>>from raw2nice_def import raw2nice[/color][/color][/color]
    [color=blue]
    > ----------
    >
    > When I tried putting this into a program to execute command-line-style:
    >
    > from raw2nice_def.py import raw2nice
    >
    > output:
    >[color=green]
    >>"C:\another_b ridge\python\py thon.exe" rawhtml2nicehtm l_template.py[/color]
    >
    > ['raw2nice_def.p y', 'raw2nice_def.p yc', 'temp']
    > Traceback (most recent call last):
    > File "C:my\holy\grai l\that_old_brid ge\rawhtml2nice html_template.p y",
    > line 10, in ?
    > from raw2nice_def.py import raw2nice
    > ImportError: No module named raw2nice_def.py[/color]

    Maybe it will work without the .py extension which is the correct form
    for an import (and what worked from the command line).

    Kent

    Comment

    • Carsten Haese

      #3
      Re: problem(s) with import from parent dir: &quot;from ../brave.pyimport sir_robin&quot;

      On Fri, 2006-02-24 at 08:10, per9000 wrote:[color=blue]
      > Dear Black Knight,
      >
      > I have no quarrel with you sir Knight, but I must import your parents.
      >
      >
      > SHORT VERSION:
      >
      > I tried three variants of "from ../brave.py import sir_robin", one
      > works. I want to use it in a py-file to execute command-line-style and
      > that does not work.
      >
      > Can someone please give me, not a bucket with the desert(s) on top, but
      > just the thin chocolate?[/color]

      1) Read and understand

      2) insert something that prints sys.path at the beginning of your script
      and run the script command-line-style.
      3) print sys.path in an interactive session
      4) note the difference in output from steps 2 and 3

      This should explain why you're getting different behavior between
      running your script from the command line and running it line-by-line in
      a python interpreter session. It should also give you a hint as to how
      to solve your problem.

      -Carsten

      Comment

      • per9000

        #4
        Re: problem(s) with import from parent dir: &quot;from ../brave.py import sir_robin&quot;

        Thanks,

        I added an environment variable PYTHONPATH and added the holy folder
        with my script in. Works just perfectly.

        But still: is there a way around this? (It is a lot easier to add
        "../../" in my code than make everyone else add this variable).

        /per9000

        Comment

        • plahey@alumni.caltech.edu

          #5
          Re: problem(s) with import from parent dir: &quot;from ../brave.py import sir_robin&quot;

          You don't _need_ to go the PYTHONPATH route (although that works).

          Re-read Carsten's post (particularly step 1, see section 6.1.1).

          You can use:

          sys.path.append ('..')
          from brave import sir_robin

          Comment

          • Carsten Haese

            #6
            Re: problem(s) with import from parent dir: &quot;from ../brave.pyimport sir_robin&quot;

            On Fri, 2006-02-24 at 09:10, per9000 wrote:[color=blue]
            > Thanks,
            >
            > I added an environment variable PYTHONPATH and added the holy folder
            > with my script in. Works just perfectly.
            >
            > But still: is there a way around this? (It is a lot easier to add
            > "../../" in my code than make everyone else add this variable).[/color]

            Yes.
            [color=blue]
            >From http://docs.python.org/tut/node8.htm...0000000000000:[/color]
            """
            Actually, modules are searched in the list of directories given by the
            variable sys.path [...]. This allows Python programs that know what
            they're doing to modify or replace the module search path.
            """

            In other words, your script may simply append other module locations
            such as "../.." to sys.path.

            -Carsten.


            Comment

            • per9000

              #7
              Re: problem(s) with import from parent dir: &quot;from ../brave.py import sir_robin&quot;

              ....and there was much rejoicing... Even better, thanks - you guys are
              the best.

              import string, time, sys
              sys.path.append ("../../py_scripts")

              Works just nice, and yes, I removed the env.variable before I tried it
              :-D

              /Per9000

              Comment

              • Magnus Lycka

                #8
                Re: problem(s) with import from parent dir: &quot;from ../brave.py importsir_robin &quot;

                per9000 wrote:[color=blue]
                > ...and there was much rejoicing... Even better, thanks - you guys are
                > the best.
                >
                > import string, time, sys
                > sys.path.append ("../../py_scripts")
                >
                > Works just nice, and yes, I removed the env.variable before I tried it
                > :-D[/color]

                The *right* thing to do might be to install the python libs
                in the correct places. Probably under the site-packages
                directory. Take a look at the distutils package.



                It can even build Windows installers and RPMs.

                These days there are also spiffy things, such as Python eggs,
                ez_setup.py, PYPI and whatever it's called, but I haven't
                tried those things.

                Comment

                Working...