473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

import woe

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

May 19 '06 #1
4 2414
gs****@gmail.com wrote:
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.

May 19 '06 #2
gs****@gmail.com wrote:
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.

May 19 '06 #3
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

May 19 '06 #4
vaibhav wrote:
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

--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com
May 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
0
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
0
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
1
by: mark | last post by:
In Access 2000 and 2002, I have created an import specification to import the fixed-width recordset below into an existing table. I am having strange problems with the import of the date and time...
4
by: Bruce W. Roeser | last post by:
All, I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good content but am confused about the difference. From the text: ...
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
7
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
5
by: W. Watson | last post by:
Is there a single source that explains these statements? ------------------------------ from Tkinter import * from Numeric import * import Image import ImageChops import ImageTk import time...
9
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.