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

Discussion in 'Python' started by per9000, Feb 24, 2006.

  1. per9000

    per9000 Guest

    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:
    http://starship.python.net/pipermail/python-au/2005/000543.html
    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\grail\that_old_bridge>python
    >>> import os
    >>> os.chdir('../../py_scripts')
    >>> os.getcwd()

    'C:\\my\\holy\\py_scripts'
    >>> from raw2nice_def import raw2nice
    >>> os.listdir('.')

    ['raw2nice_def.py', 'raw2nice_def.pyc', 'temp']
    >>> raw2nice

    <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:

    > "C:\another_bridge\python\python.exe" rawhtml2nicehtml_template.py

    ['raw2nice_def.py', 'raw2nice_def.pyc', 'temp']
    Traceback (most recent call last):
    File "C:my\holy\grail\that_old_bridge\rawhtml2nicehtml_template.py",
    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.py',
    'raw2nice_def.pyc', '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)
    per9000, Feb 24, 2006
    #1
    1. Advertising

  2. per9000

    Kent Johnson Guest

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

    per9000 wrote:
    >>>>from raw2nice_def import raw2nice


    > ----------
    >
    > When I tried putting this into a program to execute command-line-style:
    >
    > from raw2nice_def.py import raw2nice
    >
    > output:
    >
    >>"C:\another_bridge\python\python.exe" rawhtml2nicehtml_template.py

    >
    > ['raw2nice_def.py', 'raw2nice_def.pyc', 'temp']
    > Traceback (most recent call last):
    > File "C:my\holy\grail\that_old_bridge\rawhtml2nicehtml_template.py",
    > line 10, in ?
    > from raw2nice_def.py import raw2nice
    > ImportError: No module named raw2nice_def.py


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

    Kent
    Kent Johnson, Feb 24, 2006
    #2
    1. Advertising

  3. Re: problem(s) with import from parent dir: "from ../brave.pyimport sir_robin"

    On Fri, 2006-02-24 at 08:10, per9000 wrote:
    > 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?


    1) Read and understand
    http://docs.python.org/tut/node8.html#SECTION008110000000000000000
    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
    Carsten Haese, Feb 24, 2006
    #3
  4. per9000

    per9000 Guest

    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
    per9000, Feb 24, 2006
    #4
  5. per9000

    Guest

    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
    , Feb 24, 2006
    #5
  6. Re: problem(s) with import from parent dir: "from ../brave.pyimport sir_robin"

    On Fri, 2006-02-24 at 09:10, per9000 wrote:
    > 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).


    Yes.

    >From http://docs.python.org/tut/node8.html#SECTION008110000000000000000:

    """
    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.
    Carsten Haese, Feb 24, 2006
    #6
  7. per9000

    per9000 Guest

    ....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
    per9000, Feb 24, 2006
    #7
  8. per9000

    Magnus Lycka Guest

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

    per9000 wrote:
    > ...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


    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.

    http://www.python.org/doc/lib/module-distutils.html

    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.
    Magnus Lycka, Feb 27, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UnVkeQ==?=

    Sub Dir, Virtual dir, what do I use?

    =?Utf-8?B?UnVkeQ==?=, Jun 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    416
    =?Utf-8?B?UnVkeQ==?=
    Jun 12, 2005
  2. Joel Hedlund
    Replies:
    2
    Views:
    493
    Joel Hedlund
    Nov 11, 2006
  3. Joel Hedlund
    Replies:
    0
    Views:
    296
    Joel Hedlund
    Nov 11, 2006
  4. Matthew Denner
    Replies:
    1
    Views:
    167
  5. Kga Agk
    Replies:
    2
    Views:
    140
    Kga Agk
    Jun 29, 2009
Loading...

Share This Page