import woe

G

gsocks

hello,

i have a problem. i would like to import python files above and below
my current directory.

i'm working on /home/foo/bar/jar.py

i would like to import /home/foo/car.py and
/home/foo/bar/far.py

how can i do this?

thank you,
bob

ps: i want to scale, so i do not want to edit the python path
 
G

Gary Herron

hello,

i have a problem. i would like to import python files above and below
my current directory.

i'm working on /home/foo/bar/jar.py

i would like to import /home/foo/car.py and
/home/foo/bar/far.py

how can i do this?

thank you,
bob

ps: i want to scale, so i do not want to edit the python path
Work out the path name for the directory from which you'd like to
import, and append it to sys.path:

import sys
sys.path.append('..')
sys.path.append('whatever/path/absolute/or/relative')

Then off you go importing as you wish.
 
S

Serge Orlov

hello,

i have a problem. i would like to import python files above and below
my current directory.

i'm working on /home/foo/bar/jar.py

i would like to import /home/foo/car.py and
/home/foo/bar/far.py

how can i do this?

$ cat >>~/.bashrc
export PATH=/home/foo/:$PATH
$ cat >/home/foo/application
#!/usr/bin/env python
import bar.jar
$ chmod +x /home/foo/application
$ cd /home/foo/bar
$ application
..... all imports work fine ...
ps: i want to scale, so i do not want to edit the python path

In what sense do you want to scale, working with multiple projects or
multiple versions of one project at the same time? Anyway you are to
quick to jump to conclusions, if you don't want to edit python path who
will do it for you? Python path won't appear out of thin air if your
file layout is not supported out of the box.
 
V

vaibhav

Hi bob,

1. decide the directory which will be your root folder containing foo
[/home/ROOT/foo/]

2. work out your directory structure relative to this root folder
here it is ->ROOT->foo->car.py
->bar->far.py
->bar->jar.py

3. add __init__.py file inside each folder containing a list variable
__all__ with contents as the name of the directories and classes
so foo folder should contain a file called __init__.py which has the
following contents
__all__ = ['bar','car']
and bar folder should contain a file called __init__.py which has the
following contents
__all__ = ['far','jar']

4. add the root folder to your sys.path
so your jar.py file should have the following entries
from sys import path
path.append('../../../ROOT')

note: i prefer relative paths or make paths using os.getcwd
combinations in such situations, which makes the class more flexible.
you can also do this step where u configure/initialize

5. then you can import the classes you want in jar.py

from foo import car
from foo.bar import far

pls mail if u dont get it working/any doubts.

-
vaibhav
 
T

Terry Hancock

vaibhav said:
4. add the root folder to your sys.path
so your jar.py file should have the following entries
from sys import path
path.append('../../../ROOT')

note: i prefer relative paths
Interesting that that works. I guess you could create
a limited form of Zope acquisition type behavior:

sys.path = ['.', '../', '../../', '../../../'] + sys.path

I'm looking forward to the introduction of relative
imports in 2.5, though.

Cheers,
Terry
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top