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

.pth files in working directory

P: n/a
My goal is to have the top level of a directory tree in the Python
path without touching anything outside the directory. I tried to
create .pth files with the top level path in every subdirectory
but this doesn't work despite working directory being part of the
Python path.

Creating the pth file in .../site-packages works but I prefer to
have everything inside the directory tree so that removing the tree
is sufficient for a complete uninstall. Any hints are appreciated,
thanks.

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------
Aug 31 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Peter Maas wrote:
My goal is to have the top level of a directory tree in the Python
path without touching anything outside the directory. I tried to
create .pth files with the top level path in every subdirectory
but this doesn't work despite working directory being part of the
Python path.

Creating the pth file in .../site-packages works but I prefer to
have everything inside the directory tree so that removing the tree
is sufficient for a complete uninstall. Any hints are appreciated,
thanks.


Not sure from the above description exactly what it is you want, but
generally such non-standard sys.path and .pth manipulations are best
handled by a sitecustomize.py file, possibly which makes its own calls
to site.addsitedir() and such. Try "help(site)" for more.

In this case, I think you'd have a sitecustomize.py which looks for .pth
files in the current directory and calls site.addsitedir(), but I'm not
sure. Maybe if this doesn't work, an example would clarify things.

-Peter
Aug 31 '05 #2

P: n/a
Peter Hansen schrieb:
Not sure from the above description exactly what it is you want,
I want a tree

top/
install.py
sub1/
__init__.py
mod1.py
sub2/
mod2.py

where I can do "from sub1 import mod1" in mod2.py no matter what the
absolute path of top is. To achieve this I start install.py once to
retrieve the absolute dir of itself (= abspath of top/) and creates
..pth files with its absolute dir in every subdirectory.
but
generally such non-standard sys.path and .pth manipulations are best
handled by a sitecustomize.py file, possibly which makes its own calls
to site.addsitedir() and such. Try "help(site)" for more.


But sitecustomize.py changes the Python installation, doesn't it?
This wouldn't be an advantage over putting a .pth file into
..../site-packages.

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------
Aug 31 '05 #3

P: n/a
On Wed, 31 Aug 2005 15:07:41 +0200
Peter Maas <pe***@somewhere.com> wrote:
I want a tree

top/
install.py
sub1/
__init__.py
mod1.py
sub2/
mod2.py

where I can do "from sub1 import mod1" in mod2.py no matter what the
absolute path of top is. To achieve this I start install.py once to
retrieve the absolute dir of itself (= abspath of top/) and creates
.pth files with its absolute dir in every subdirectory.


If top/ is the working directory for your Python interpreter, the
problem is solved automatically. Python puts the current working
directory in the default search path. So, if you run

python sub2/mod2.py

then it would work.

HTH,
-Michael
Aug 31 '05 #4

P: n/a
Peter Maas wrote:
Peter Hansen schrieb:
generally such non-standard sys.path and .pth manipulations are best
handled by a sitecustomize.py file, possibly which makes its own calls
to site.addsitedir() and such. Try "help(site)" for more.


But sitecustomize.py changes the Python installation, doesn't it?
This wouldn't be an advantage over putting a .pth file into
.../site-packages.


You can have a local sitecustomize.py in the current directory, which
wouldn't change the Python installation. Would that help?

As far as I can tell, the current directory isn't added to the sys.path
until after site.py executes, so I don't believe you can rely on .pth
files to do anything like what you want. Sitecustomize.py is it, with
appropriate smarts inside.

-Peter
Aug 31 '05 #5

P: n/a
Michael Ekstrand schrieb:
If top/ is the working directory for your Python interpreter, the
problem is solved automatically. Python puts the current working
directory in the default search path. So, if you run


IIS sets the the site path as working directory. So I would probably
have to change wd at the beginnig of every module. Each module would
read the top location from a .pth file in its directory. Yes that's
possible. Thanks for your help.

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------
Aug 31 '05 #6

P: n/a
Peter Hansen schrieb:
Peter Maas wrote:
But sitecustomize.py changes the Python installation, doesn't it?
This wouldn't be an advantage over putting a .pth file into
.../site-packages.

You can have a local sitecustomize.py in the current directory, which
wouldn't change the Python installation. Would that help?


I think this is what I need. Thanks, Peter.

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------
Aug 31 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.