By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,978 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,978 IT Pros & Developers. It's quick & easy.

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

P: n/a
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...05/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)

Feb 24 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Feb 24 '06 #2

P: n/a
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.htm...00000000000000
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

Feb 24 '06 #3

P: n/a
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

Feb 24 '06 #4

P: n/a
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 '06 #5

P: n/a
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.htm...0000000000000:

"""
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.
Feb 24 '06 #6

P: n/a
....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

Feb 24 '06 #7

P: n/a
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.
Feb 27 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.