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

P

per9000

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)
 
K

Kent Johnson

----------

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
 
C

Carsten Haese

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
 
P

per9000

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
 
P

plahey

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
 
C

Carsten Haese

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.
 
P

per9000

....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
 
M

Magnus Lycka

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top