Not Sure This Can Be Done...

G

gamename

Hi,

I generally have several copies of the same development environment
checked out from cvs at any one time. Each development tree has a
'tools' dir containing python modules. Scattered in different places
in the tree are various python scripts.

What I want to do is force my scripts to always look in the closest
'tools' dir for any custom modules to import. For example:

tree1/tools
tree1/my_scripts/foo.py

tree2/tools
tree2/my_scripts/foo.py

How can I make 'foo.py' always look in '../../tools' for custom
modules? My preference would be to avoid changing the 'foo.py' script
and have some way to resolve it via the environment (like PYTHONPATH,
or .pth files, etc.).

Anyone have any ideas?
TIA,
-T
 
D

Diez B. Roggisch

gamename said:
Hi,

I generally have several copies of the same development environment
checked out from cvs at any one time. Each development tree has a
'tools' dir containing python modules. Scattered in different places
in the tree are various python scripts.

What I want to do is force my scripts to always look in the closest
'tools' dir for any custom modules to import. For example:

tree1/tools
tree1/my_scripts/foo.py

tree2/tools
tree2/my_scripts/foo.py

How can I make 'foo.py' always look in '../../tools' for custom
modules? My preference would be to avoid changing the 'foo.py' script
and have some way to resolve it via the environment (like PYTHONPATH,
or .pth files, etc.).

Anyone have any ideas?


Use virtualenv to create a local python, and activate that when
developing for that branch.

Alternatively, you can of course manipulate the PYTHONPATH yourself -
but why should you if virtualenv takes care of that for you?

Diez
 
G

gamename

Use virtualenv to create a local python, and activate that when
developing for that branch.

Thanks for the suggestion, but that's the problem: having to activate
it.
Isn't there some way to simply have the local script look at a
specified
dir rather than starting a virtual environment?

-T
 
D

Diez B. Roggisch

gamename said:
Thanks for the suggestion, but that's the problem: having to activate
it.
Isn't there some way to simply have the local script look at a
specified
dir rather than starting a virtual environment?

You can only do that yourself - you can write a helper-module that will
inspect the environment, locates the module, and appends it path to
sys.path.

Diez
 
M

MrJean1

In any script upon startup, sys.path[0] contains the full path of the
directory where the script is located. See <http://docs.python.org/
lib/module-sys.html> under 'path'.

it should be straightforward from here (untested though). In each
script, get the sys.path[0] string, split it using os.path, replace
the last item with 'tools' and join again with os.path. If the
resulting string is not in sys.path, insert it after sys.path[0].

/Jean Brouwers
 
G

gamename

Thanks! Good suggestion(s) all.

Maybe I can get this done. :)

-T

In any script upon startup, sys.path[0] contains the full path of the
directory where the script is located. See <http://docs.python.org/
lib/module-sys.html> under 'path'.

it should be straightforward from here (untested though). In each
script, get the sys.path[0] string, split it using os.path, replace
the last item with 'tools' and join again with os.path. If the
resulting string is not in sys.path, insert it after sys.path[0].

/Jean Brouwers

I generally have several copies of the same development environment
checked out from cvs at any one time. Each development tree has a
'tools' dir containing python modules. Scattered in different places
in the tree are various python scripts.
What I want to do is force my scripts to always look in the closest
'tools' dir for any custom modules to import. For example:


How can I make 'foo.py' always look in '../../tools' for custom
modules? My preference would be to avoid changing the 'foo.py' script
and have some way to resolve it via the environment (like PYTHONPATH,
or .pth files, etc.).
Anyone have any ideas?
TIA,
-T
 
G

gamename

Hi,

In hopes it may help someone else, here's what I ended up doing:

1) Add this to to point to your local 'tools' directory:
import site
# the python tools dir is at "../../tools/python"
site.addsitedir('..'+os.sep+'..'+os.sep+'tools'+os.sep
+'python')

2) In the 'tools' dir, there is a file called 'local.pth'. Its
contents:
# the individual modules are at "../../tools/python/modules/
<modulename>"
./modules/pexpect

3) In the script you can now do:
import pexpect

That's it. This works fine, but there are still 2 outstanding issues:

A) How do make the same ".pth" file usable on a win32 environment?
The path separators are different (i.e. "/" vs "\").

B) How to auto-include any subdirectory in 'python/modules' so that
each
new module dir doesn't have to be added manually? That is, if
package
'foo' is added, it would be in 'python/modules/foo'. Is there a
way
to avoid manually putting that in the '.pth'?

FYI,
-T
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top